加载数据时mysql concat错误

xqkwcwgp  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(295)

我正在开发一个加载 .csv 将存储在s3存储桶中的文件存储到rds数据库中。这个 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)set的正确语法customer=@col1, firstname=@col2'第6行

myzjeezk

myzjeezk1#

您需要对字符串中的反斜杠进行转义,以便将其转换为串联的结果。

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;
');

相关问题