加载数据时出现MySQL CONCAT错误

时间:2018-07-20 15:32:41

标签: mysql sql variables concat

我正在开发一个SQL脚本,它将存储在S3存储桶中的.csv文件加载到RDS DB中。不使用LOAD时,CONCAT命令可以完美地工作。我正在使用CONCAT在语句中包含变量。我认为问题在于一个或多个条款。我大大缩短了字段以进行故障排除。我尝试了几种不同的方法,但似乎无法获得正确的语法。

set @s3intotemp = CONCAT('
LOAD DATA FROM S3 PREFIX "`',@workingdir, '`"
INTO TABLE `',@rndtable, '` 
FIELDS TERMINATED BY ","
ESCAPED BY ""
ENCLOSED BY "\""
LINES TERMINATED BY "\n"
(@col1,
@col2) 
SET customer=@col1,
firstname=@col2;
');
PREPARE stmt from @s3intotemp;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

我收到以下错误:

  

SQL错误:您的SQL语法有错误。检查手册   对应于您的MySQL服务器版本以使用正确的语法   行附近'“(@ col1,@ col2)设置客户= @ col1,名字= @ col2'   6

1 个答案:

答案 0 :(得分:1)

您需要对字符串中的反斜杠进行转义,以使它们进入串联的结果。

set @s3intotemp = CONCAT('
LOAD DATA FROM S3 PREFIX "`',@workingdir, '`"
INTO TABLE `',@rndtable, '` 
FIELDS TERMINATED BY ","
ESCAPED BY ""
ENCLOSED BY "\\\""
LINES TERMINATED BY "\\n"
(@col1,
@col2) 
SET customer=@col1,
firstname=@col2;
');