编辑:将数据(例如表格)保存到文件中而不使用变量(仅常量值)
-- folder_path could could be like => c:/users/sami
-- choose the directory/folder already available in system
-- and make sure you have access to write the file there
SELECT * INTO OUTFILE 'folder_path/filename.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
FROM database.tableName;
现在使用变量
无论何时必须在 sql 中使用变量名,都需要动态 sql(仅适用于存储过程,既不适用于简单的 sql 查询,也不适用于触发器或函数)
SET @OutputPath := 'Users/jo/Documents'; //or any folder_path
SET @fullOutputPath := CONCAT(@OutputPath,'/','filename.csv');
SET @fullOutputPath2 := CONCAT(@OutputPath,'/','filename2.csv');
set @q1 := concat("SELECT * INTO OUTFILE ",@fullOutputPath,
" FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
FROM database.tableName");
set @q2 := concat("SELECT * INTO OUTFILE ",@fullOutputPath2,
" FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
FROM database.tableName2");
prepare s1 from @q1;
execute s1;deallocate prepare s1;
prepare s1 from @q2;
execute s1;deallocate prepare s1;
正如您已经在查询中拥有两者一样'
,"
因此我使用并使用 \ 来连接您的查询"
以转义您的原始"
字符,以确保其用作文字字符而不用于连接
我刚才讲了variable
in sql的使用。首先,您应该确保您的查询是否像顶部的示例一样工作(不使用变量)
结论:如果您的上述查询工作正常,那么考虑到您在某些存储过程中使用它,我告诉的动态 sql 也可以正常工作。