MySQL如何不断创建输出的唯一文件?

时间:2012-03-13 17:45:17

标签: mysql file-io

我有以下存储过程

DELIMITER ##
CREATE PROCEDURE exportFile()
BEGIN

Select * from Sample
INTO OUTFILE '~/Sample.csv'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';

END ##
DELIMITER ;
  • 首次调用存储过程后,您将获得ERROR 1086 (HY000): File '~/Sample.csv' already exists - 理所当然。
  • 我需要不断创建新文件。 (即Sample1.csv,Sample2.csv,Sample3.csv ......等)每次通话。

问题:我该怎么做?

2 个答案:

答案 0 :(得分:6)

为什么不在文件中附加时间戳?

这是一个向文件追加时间戳的示例:

DELIMITER ##
CREATE PROCEDURE exportFile()
BEGIN

Select * from Sample
INTO OUTFILE CONCAT('~/Sample', CONCAT(DATE_FORMAT( your_date_field, '%d%m%Y') ,'.csv'))
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';

END ##
DELIMITER ;

答案 1 :(得分:3)

我达到了调试递归存储过程的类似要求。

这对我有用:

SET @s = CONCAT('SELECT * INTO OUTFILE ',"'", CONCAT('Result',FLOOR(1000+RAND()*9999)),'.txt',"'",' FROM TempTable112');
PREPARE stmt2 FROM @s;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;

我使用xampp并在C:\ xampp \ mysql \ data \ myfolder获得输出。