如何将存储过程的输出存储到磁盘上

时间:2010-02-25 12:09:33

标签: stored-procedures file-io

我们正在使用SQL Server 2005。 我想创建存储过程输出的文件。

1 个答案:

答案 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