如何在MySQL中使用声明的变量作为路径?

如何在MySQL中使用声明的变量作为路径?,mysql,sql,stored-procedures,Mysql,Sql,Stored Procedures,我正在开发一个存储过程,它在文本文件中插入一些数据 该程序具有以下结构: delimiter !! drop procedure if exists insertIntoFile !! create procedure insertIntoFile () begin declare path varchar(255); set path = concat("C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/", curdate(),

我正在开发一个存储过程,它在文本文件中插入一些数据

该程序具有以下结构:

delimiter !!
drop procedure if exists insertIntoFile !!
create procedure insertIntoFile ()

begin

    declare path varchar(255);
    set path = concat("C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/", curdate(), ".txt");

    select * from movie_modification where modified = true
    into outfile path;   //<-- This return an error

end !!
delimiter ;
分隔符!!
如果存在插入文件,则删除过程!!
创建过程插入文件()
开始
声明路径varchar(255);
设置path=concat(“C:/ProgramData/MySQL/MySQL服务器5.7/Uploads/”,curdate(),“.txt”);
选择*from movie_modification,其中modified=true

进入出文件路径// 你有两个小问题

首先,您的路径必须是字符串,因为您使用双引号,在路径的开头和结尾添加单引号

另一件事是使用prepared语句进行查询

DELIMITER $$
DROP PROCEDURE IF EXISTS insertIntoFile§§
CREATE DEFINER=`root`@`localhost` PROCEDURE `insertIntoFile`()
BEGIN

    DECLARE path VARCHAR(255);
    SET path = CONCAT("'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/", curdate(), ".txt;'");

    SET @sql = CONCAT('select * from movie_modification where modified = true
    into outfile ',path);  

PREPARE test FROM @sql;
EXECUTE test;

end$$
DELIMITER ;

我不知道准备好的声明是什么,现在我知道了,谢谢!