错误代码:1064。您的 SQL 语法有误;在第 2 行的 '' 附近
Error Code: 1064. You have an error in your SQL syntax; near '' at line 2
有人知道错误在哪里吗?整个下午都在处理这个问题。 Workbench 在指出错误方面不是很有用。
我的代码出现了下一个错误
CALL pCityMean('Spain') 错误代码:1064。您的 SQL 语法有误;查看与您的 MariaDB 服务器版本对应的手册,了解在第 2 行的 '' 附近使用的正确语法。
DELIMITER $$
DROP PROCEDURE IF EXISTS pCityMean $$
CREATE PROCEDURE pCityMean (IN vPais VARCHAR(30))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE vID INT;
DECLARE cur1 CURSOR FOR SELECT distinct id_city FROM world_temp_stats.temp_by_city where id_country=(SELECT id_country FROM world_temp_stats.country where Name=vPais);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
bucle1: LOOP
FETCH cur1 INTO vID;
IF done = 1 THEN
LEAVE bucle1;
END IF;
SET @nomCiutat = (SELECT name FROM world_temp_stats.city WHERE id = vID);
SET @tablaCiutat = CONCAT ("SELECT year(dt), count(AverageTemperature), ROUND(avg(AverageTemperature), 2) INTO OUTFILE '",UCASE(vPais),"_",UCASE(@nomCiutat),"_temp_analisis.csv' FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n' FROM world_temp_stats.temp_by_city where id_country=(SELECT id_country FROM world_temp_stats.country where Name='",vPais,"' and id_city=",vID," group by year(dt)");
PREPARE execucio FROM @tablaCiutat;
EXECUTE execucio;
DEALLOCATE PREPARE execucio;
END LOOP bucle1;
CLOSE cur1;
END $$
DELIMITER ;
CALL pCityMean('Spain');
我可以观察您的查询,发现您忘记了右括号。
SET @tablaCiutat = CONCAT ("SELECT ...
where id_country=(
SELECT id_country FROM world_temp_stats.country
where Name='",vPais,"' and id_city=",vID," group by year(dt)
");
我认为您混淆了,因为 year(dt)
以 right-paren 结尾,但您还需要一个来关闭子查询。类似于以下内容:
SET @tablaCiutat = CONCAT ("SELECT ...
where id_country=(
SELECT id_country FROM world_temp_stats.country
where Name='",vPais,"' and id_city=",vID," group by year(dt)
)
");
您还应该在准备好的查询中使用动态值的查询参数,而不是 string-concatenation。
但如果是我,我会更改此代码的很多内容。为什么要使用 INTO OUTFILE
,为什么要使用存储过程?根据我的经验,MySQL 存储过程比用几乎任何客户端编程语言编写代码都困难。我几乎从不使用 MySQL 存储过程。
有人知道错误在哪里吗?整个下午都在处理这个问题。 Workbench 在指出错误方面不是很有用。
我的代码出现了下一个错误
CALL pCityMean('Spain') 错误代码:1064。您的 SQL 语法有误;查看与您的 MariaDB 服务器版本对应的手册,了解在第 2 行的 '' 附近使用的正确语法。
DELIMITER $$
DROP PROCEDURE IF EXISTS pCityMean $$
CREATE PROCEDURE pCityMean (IN vPais VARCHAR(30))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE vID INT;
DECLARE cur1 CURSOR FOR SELECT distinct id_city FROM world_temp_stats.temp_by_city where id_country=(SELECT id_country FROM world_temp_stats.country where Name=vPais);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
bucle1: LOOP
FETCH cur1 INTO vID;
IF done = 1 THEN
LEAVE bucle1;
END IF;
SET @nomCiutat = (SELECT name FROM world_temp_stats.city WHERE id = vID);
SET @tablaCiutat = CONCAT ("SELECT year(dt), count(AverageTemperature), ROUND(avg(AverageTemperature), 2) INTO OUTFILE '",UCASE(vPais),"_",UCASE(@nomCiutat),"_temp_analisis.csv' FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n' FROM world_temp_stats.temp_by_city where id_country=(SELECT id_country FROM world_temp_stats.country where Name='",vPais,"' and id_city=",vID," group by year(dt)");
PREPARE execucio FROM @tablaCiutat;
EXECUTE execucio;
DEALLOCATE PREPARE execucio;
END LOOP bucle1;
CLOSE cur1;
END $$
DELIMITER ;
CALL pCityMean('Spain');
我可以观察您的查询,发现您忘记了右括号。
SET @tablaCiutat = CONCAT ("SELECT ...
where id_country=(
SELECT id_country FROM world_temp_stats.country
where Name='",vPais,"' and id_city=",vID," group by year(dt)
");
我认为您混淆了,因为 year(dt)
以 right-paren 结尾,但您还需要一个来关闭子查询。类似于以下内容:
SET @tablaCiutat = CONCAT ("SELECT ...
where id_country=(
SELECT id_country FROM world_temp_stats.country
where Name='",vPais,"' and id_city=",vID," group by year(dt)
)
");
您还应该在准备好的查询中使用动态值的查询参数,而不是 string-concatenation。
但如果是我,我会更改此代码的很多内容。为什么要使用 INTO OUTFILE
,为什么要使用存储过程?根据我的经验,MySQL 存储过程比用几乎任何客户端编程语言编写代码都困难。我几乎从不使用 MySQL 存储过程。