2013-05-29 88 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