2013-05-29 38 views
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; 

我有上面的代码。我可以创建程序。但是,当我把它称为 呼叫级联它显示存储过程级联mysql

"SQLSyntaxError (1064, "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 ': pcu_rtc_minute: pcu_rtc_secound) FROM site_values where site_id =1' at line 1")"

请告诉我错我的代码?

+0

是后'“''字符选择CONCAT'是否需要? – ramonovski

+0

为了节省时间,您可以打印出代码运行时@t的样子,并更新您的文章? – Tom

+0

pcu_rtc_year等字段名称? – Tom

回答

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); 
+0

谢谢汤姆...它的工作.. – Anusha

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...) 
0

你错过报价在那里,两个concats混错:

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); 
+0

非常感谢你回答:) – Anusha