6

How can i rename the filename with a concat function ? i want my filename with a date,, can anyone help me on this.

SET @OUTFILE = CONCAT( CONCAT('/xampp/htdocs/mysite/reports-', NOW()), '.csv');


SELECT * FROM `tbl_user` INTO OUTFILE  @OUTFILE FIELDS TERMINATED BY ','ENCLOSED BY '"' LINES TERMINATED BY '\n'
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
avien
  • 177
  • 5
  • 14
  • what is the problem with your query – juergen d Jul 05 '12 at 05:48
  • i always get this error : #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@OUTFILE – avien Jul 05 '12 at 05:50
  • actually im not so familiar in concatination in mysql, anyway i just want my filename to be dynamic, so everytime i run this query it gives me a unique filename with a date ;( – avien Jul 05 '12 at 05:53

2 Answers2

10

you can do something like:

SET @sql_text = 
   CONCAT (
       "SELECT * FROM `tbl_user` into outfile '/xampp/htdocs/mysite/reports-"
       , DATE_FORMAT( NOW(), '%Y%m%d')
       , ".csv'"
    );

PREPARE s1 FROM @sql_text;
EXECUTE s1;
DROP PREPARE s1;
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
1

You should use prepared statements. Build a query string, then execute it.

Devart
  • 119,203
  • 23
  • 166
  • 186