SELECT ... MySQL 中存储过程的 INTO OUTFILE 语句
SELECT ... INTO OUTFILE statement on Stored Procedure in MySQL
我需要以 .csv 格式导出一个 MySQL table。
为此,我尝试使用 SELECT ... INTO OUTFILE 语句的 Stored Procedure 旨在启用将 table 转储到服务器主机上的 csv 文件。
但是我收到这个错误:
Procedure execution failed 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 'C:\inetpub\ftproot\tm_20200322.csv'
at line 1
你能帮帮我吗?
下面是我的代码。
BEGIN
SET @tbl = DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 1 DAY), '%Y%m%d');
SET @s = CONCAT('C:\inetpub\ftproot\\tm_', @tbl, '.csv');
PREPARE stmt FROM @s;
EXECUTE stmt;
SET @s = CONCAT('SELECT * FROM `dotable` INTO OUTFILE @s FIELDS TERMINATED BY ";" LINES TERMINATED BY "\n"');
PREPARE stmt FROM @s;
EXECUTE stmt;
END
文件名必须用单引号引起来。此外,不清楚为什么需要执行两次。您还需要小心使用转义 \
(有些地方有一对多,有些地方缺少一对)。
这应该可以满足您的要求:
SET @tbl = DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 1 DAY), '%Y%m%d');
SET @s = CONCAT('C:\inetpub\ftproot\tm_', @tbl, '.csv');
SET @s = CONCAT('SELECT * FROM `dotable` INTO OUTFILE ''', @s, ''' FIELDS TERMINATED BY ";" LINES TERMINATED BY "\n"');
PREPARE stmt FROM @s;
EXECUTE stmt;
此代码generates the following query:
SELECT * FROM `dotable` INTO OUTFILE 'C:\inetpub\ftproot\tm_20200322.csv' FIELDS TERMINATED BY ";" LINES TERMINATED BY "\n"
我需要以 .csv 格式导出一个 MySQL table。
为此,我尝试使用 SELECT ... INTO OUTFILE 语句的 Stored Procedure 旨在启用将 table 转储到服务器主机上的 csv 文件。
但是我收到这个错误:
Procedure execution failed 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 'C:\inetpub\ftproot\tm_20200322.csv' at line 1
你能帮帮我吗?
下面是我的代码。
BEGIN
SET @tbl = DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 1 DAY), '%Y%m%d');
SET @s = CONCAT('C:\inetpub\ftproot\\tm_', @tbl, '.csv');
PREPARE stmt FROM @s;
EXECUTE stmt;
SET @s = CONCAT('SELECT * FROM `dotable` INTO OUTFILE @s FIELDS TERMINATED BY ";" LINES TERMINATED BY "\n"');
PREPARE stmt FROM @s;
EXECUTE stmt;
END
文件名必须用单引号引起来。此外,不清楚为什么需要执行两次。您还需要小心使用转义 \
(有些地方有一对多,有些地方缺少一对)。
这应该可以满足您的要求:
SET @tbl = DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 1 DAY), '%Y%m%d');
SET @s = CONCAT('C:\inetpub\ftproot\tm_', @tbl, '.csv');
SET @s = CONCAT('SELECT * FROM `dotable` INTO OUTFILE ''', @s, ''' FIELDS TERMINATED BY ";" LINES TERMINATED BY "\n"');
PREPARE stmt FROM @s;
EXECUTE stmt;
此代码generates the following query:
SELECT * FROM `dotable` INTO OUTFILE 'C:\inetpub\ftproot\tm_20200322.csv' FIELDS TERMINATED BY ";" LINES TERMINATED BY "\n"