我们正在使用SQL Server 2005。 我想创建存储过程输出的文件。
答案 0 :(得分:2)
如果您使用的是MySQL,可能需要从以下内容开始:
DELIMITER $$
CREATE PROCEDURE export_dynamic(IN file_path char(64))
BEGIN
SET @sql = CONCAT('SELECT * INTO OUTFILE ',
"'", file_path, "'", ' FROM Table1');
PREPARE stmt1 FROM @sql;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END $$
DELIMITER ;
借鉴:MySQL Forums :: General :: SELECT .. INTO OUTFILE not possible with stored procedure parameters?
如果要在存储过程中对文件名进行硬编码,则无需使用预准备语句:
DELIMITER $$
CREATE PROCEDURE export_static()
BEGIN
SELECT * INTO OUTFILE '/tmp/file_name.txt' FROM Table1;
END $$
DELIMITER ;
如果您收到Can't create/write to file
错误,请务必查看以下参考:MySQL 5.1 Reference Manual :: Can't create/write to file。