Torin Smith
Torin Smith

Reputation: 33

MYSQL How to use a variable for the OutFile

I can't find how to use a variable as an an Outfile.

DECLARE wOutFile CHAR(256);
SET wOutFile = CONCAT('c/data/',wDateStr,'.csv');
SELECT * FROM sometable
INTO OUTFILE wOutFile;

The above renders an error on the last line. The syntax parser in MySQL Workbench doesn't like the wOutFile on the last line.

Is there a way to tell the parser to use the contents of the variable?

I tried to search for a while but I must not be phrasing my question well.

Upvotes: 1

Views: 4443

Answers (1)

Devart
Devart

Reputation: 121922

If you want to make new statement from the strings, then you should use prepared statements -

DECLARE wOutFile CHAR(256);
SET wOutFile = CONCAT('c/data/',wDateStr,'.csv');

SET @var = CONCAT('SELECT * FROM sometable INTO OUTFILE ', wOutFile);
PREPARE stmt FROM @var;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Upvotes: 4

Related Questions