下午好,
我使用INTO OUTFILE创建一个带有SELECT信息的CSV文件。 我需要使用变量来个性化CSV的名称。
例如,我想添加日期NOW()。
这是一个想法(但它不起作用):
run
Anyonw知道它是否可能? 感谢
答案 0 :(得分:0)
唯一的方法是使用PREPARE STATEMENT
// You can set your VARS or use it direcly in the CONCAT
SET @outpath = "out/";
SET @outfile = (SELECT NOW());
SET @outextension = "_EMAIL_WithoutEmail.csv";
// GENERATE your QUERY with CONCAT into @SQL
SELECT CONCAT("SELECT * FROM `vn_db_tmp` INTO OUTFILE ",@outpath,@outfile,@outextension,'....') INTO @SQL;
PREPARE stmt FROM @SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
答案 1 :(得分:0)
一种选择是使用13.5 Prepared SQL Statement Syntax。
示例:
...
SET @`outfull` := CONCAT('out/', NOW(), '_EMAIL_WithoutEmail.csv');
SET @`qry` := CONCAT('SELECT *
INTO OUTFILE \'', @`outfull`, '\'
FIELDS TERMINATED BY \';\'
ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
FROM `vn_db_tmp`');
PREPARE `stmt` FROM @`qry`;
SET @`qry` := NULL;
EXECUTE `stmt`;
DEALLOCATE PREPARE `stmt`;
...
答案 2 :(得分:0)
把它留在那里。为避免 outfile 出现问题,您必须使用“文件路径”。 ' 特殊符号使用反斜杠+'。
SET @outfull = CONCAT('\'/path/to/file/', date_format (NOW(),'%Y-%m-%d_%H:%i:%s'), '_app.log\'');
SET @SQL = CONCAT("SELECT * FROM some_table INTO OUTFILE ",@outfull);
PREPARE stmt FROM @SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;