2012-03-23 88 views
0

我很新的MySQL的存儲過程,並得到了以下錯誤:MySQL存儲過程不起作用

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 'DECLARE v_lang TINYINT(1) DEFAULT '1'; DECLARE cursor_lang CURSOR FOR SELECT `l' at line 7

當試圖建立這個存儲過程:

DELIMITER // 
CREATE PROCEDURE UpdateUser(IN p_uid INT(11)) 
BEGIN 

DECLARE v_last_login TIMESTAMP DEFAULT '2012-01-01 00:00:00' ; 
SELECT `last_login` INTO v_last_login FROM `user` WHERE `id`= p_uid; 

DECLARE v_lang TINYINT(1) DEFAULT '1'; 

DECLARE cursor_lang CURSOR FOR SELECT `l_id` INTO v_lang FROM `user_lang` WHERE `user_id` = p_uid LIMIT 0 , 5; 

DECLARE no_more_l TINYINT(1) DEFAULT 0; 
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_l = 1; 

OPEN cursor_lang; 

FETCH cursor_lang INTO v_lang_str; 
REPEAT 

UPDATE user SET `last_login`=CURRENT_TIMESTAMP() WHERE `id`=p_uid AND `l_id` = v_lang_str; 

UNTIL no_more_l = 1 
END REPEAT; 
CLOSE cursor_lang; 

END // 
DELIMITER ; 

做什麼,我做錯了?

+0

你爲什麼使用遊標? – 2012-03-23 13:32:32

回答

2

您確定可以使用字符串作爲數字變量的默認值嗎?

嘗試

DECLARE v_lang TINYINT(1) DEFAULT 1; 
+0

問題在於您必須先聲明每個變量,然後才能執行任何查詢 – 2012-04-21 14:43:34

2

如果你不這樣做

DECLARE v_lang TINYINT(1) DEFAULT 1; 

代替

DECLARE v_lang TINYINT(1) DEFAULT '1'; 
0

謝謝您的建議。問題在於你必須先聲明每個變量,然後才能進行任何查詢。當然,我也不應該把數字放在引號中。

+1

感謝您的更新,Dualthlon。這應該是對GregHNZ或Frankline的答案的評論 - 因爲他們正確回答了這個問題。請記住用蜱和/或upvotes獎勵他們! – halfer 2012-04-21 14:54:45