0
CREATE PROCEDURE concatenation()
      BEGIN
              DECLARE i  INT default 1;
              declare t varchar(50);
              repeat 
              set @t =  concat("INSERT ignore INTO `site_values_" , i , "` (report_time) SELECT  CONCAT(", "pcu_rtc_year" , "-" , "pcu_rtc_month" , "-" , "pcu_rtc_day" , "  " , "pcu_rtc_hour" , ":" , " pcu_rtc_minute" , ":" , " pcu_rtc_secound",")" ,
                   " FROM site_values where site_id =" , i);
    PREPARE stmt FROM @t;
    EXECUTE stmt; 
    DEALLOCATE PREPARE stmt;
    set i = i+1;
    until i =1001
    end repeat;
    END;

我有上面的代码。我可以创建程序。但是当我称它为调用连接时,它显示

"SQLSyntaxError (1064, "您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以了解在第 1 行的 ': pcu_rtc_minute: pcu_rtc_secound) FROM site_values where site_id =1' 附近使用的正确语法") "

我的代码有什么问题?

4

3 回答 3

1

您不需要在字段名称周围加上引号。它应该看起来像这样:

concat("INSERT ignore INTO `site_values_" , i , "` (report_time) SELECT  CONCAT(pcu_rtc_year , "-" , pcu_rtc_month , "-" , pcu_rtc_day , "  " , pcu_rtc_hour, ":" , pcu_rtc_minute, ":" , pcu_rtc_secound) FROM site_values where site_id = " , i);

此外,由于您使用的是 2 CONCAT 语句,因此引号可能是个问题。你可以试试这个:

concat("INSERT ignore INTO `site_values_" , i , "` (report_time) SELECT  CONCAT(pcu_rtc_year , '-' , pcu_rtc_month , '-' , pcu_rtc_day , '  ' , pcu_rtc_hour, ':' , pcu_rtc_minute, ':' , pcu_rtc_secound) FROM site_values where site_id = " , i);
于 2013-05-29T19:30:31.327 回答
0

您的 SQL 是否没有构造语法错误:

"...SELECT  CONCAT(", "pcu_rtc_year" , "-" , "pcu_rtc_month" , "-" , "pcu_rtc_day" , "  " , "pcu_rtc_hour" , ":" , " pcu_

将导致

SELECT CONCAT(pcu_rtc_year-pcu_rtc_month-pcu_rtc_day pcu_rtc_hour:pcu...)

看来你在那里缺少逗号。

"...SELECT  CONCAT(", "pcu_rtc_year," , "'-'," , "pcu_rtc_month," , "'-'," , "pcu_rtc_day," , "'  '," , "pcu_rtc_hour," , "':'," , " pcu_...)

结果:

SELECT CONCAT(pcu_rtc_year,'-',pcu_rtc_month,'-',pcu_rtc_day,' ',pcu_rtc_hour,':',pcu...)
于 2013-05-29T19:30:50.110 回答
0

您在那里缺少引号,并且在两个连接中混淆了:

set @t =  concat("INSERT ignore INTO `site_values_" , i , "` (report_time)",
     "SELECT CONCAT(pcu_rtc_year, '-', pcu_rtc_month, '-', pcu_rtc_day, '  ',
                    pcu_rtc_hour, ':', pcu_rtc_minute, ':', pcu_rtc_secound)",
     "FROM site_values where site_id =" , i);
于 2013-05-29T19:34:57.737 回答