gpt4 book ai didi

Mysql加载数据时CONCAT错误

转载 作者:行者123 更新时间:2023-11-29 10:08:29 25 4
gpt4 key购买 nike

我正在开发一个 SQL 脚本,用于将存储在 S3 存储桶中的 .csv 文件加载到 RDS 数据库中。当不使用 CONCAT 时,LOAD 命令可以正常工作。我使用 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 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '" (@col1, @col2 ) SET customer=@col1, firstname=@col2' at line 6

最佳答案

您需要转义字符串中的反斜杠,以便它们将其纳入连接结果中。

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

关于Mysql加载数据时CONCAT错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51445947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com