我正在使用 -
导出表格SELECT * INTO OUTFILE 'd:\\result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM result;
现在我想通过将其放入程序中来每天运行此脚本。并且我想使用文件名“结果”与日期或其他一些我可以每天增加的动态数字。否则它会给我重复文件名的错误。
我应该像 -
declare var_result varchar(100);
set var_result = 'result01012014';
SELECT * INTO OUTFILE var_result
................................
有可能吗?
答案 0 :(得分:1)
而不是创建Stored Procedure
您可以为同一个
shell or php
脚本
我经常使用的shell脚本示例
#!/bin/bash
location="/home/abdul/Desktop"
user="root"
pass="root"
table="City"
database="world_innodb"
mysql -u$user -p$pass --column-names -e "SELECT * FROM $database.$table;" | sed 's/\t/"|"/g;s/^/"/;s/$/"/;s/\n//g' > $location/$table-$(date +"%Y-%m-%d").csv
这将创建类似City-2014-02-20.csv
执行相同的存储过程
DROP PROCEDURE IF EXISTS ExportToCSV;
DELIMITER $$
CREATE PROCEDURE `ExportToCSV`()
BEGIN
SET @cur_date = CURRENT_DATE();
SET @path = "/tmp/";
SET @table_name = "City";
SET @VarSQL = CONCAT('SELECT * INTO OUTFILE \'' ,@path,@table_name,'-',@cur_date,'.csv\' FIELDS TERMINATED BY ''\,','\' OPTIONALLY ENCLOSED BY \'','"' ,'\' LINES TERMINATED BY \'','\\n','\' FROM ',@table_name,';' );
PREPARE stmt FROM @VarSQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;