mysql存储过程outfile

时间:2014-08-30 05:11:16

标签: mysql stored-procedures file-io

我正在尝试将SP查询结果写入文件nad获取一致的错误代码:1086文件已存在。尽管文件名必须是唯一的,因为它包含一个随机生成数字。 这是我的代码的一部分:

SET fullOutputPath = CONCAT(user,'_',FLOOR(1000+RAND()*9999),'.txt');

SELECT fullOutputPath;
-- write the resultset to the file
SELECT node_concat 
    INTO OUTFILE ",fullOutputPath,"
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY ''
    LINES TERMINATED BY '\n'
  FROM sn_neighbour_tmp;

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您需要使用13.5 SQL Syntax for Prepared Statements

示例:

...
SET `fullOutputPath` := CONCAT(USER, '_', FLOOR(1000 + RAND() * 9999), '.txt');
SET @`qry` := CONCAT('SELECT `node_concat` 
                          INTO OUTFILE ', `fullOutputPath`, ' 
                          FIELDS TERMINATED BY \',\' 
                          OPTIONALLY ENCLOSED BY \'\' 
                          LINES TERMINATED BY \'\n\' 
                      FROM `sn_neighbour_tmp`');
PREPARE `stmt` FROM @`qry`;
SET @`qry` := NULL;
EXECUTE `stmt`;
DEALLOCATE PREPARE `stmt`;
...