2015-12-22 45 views
-2

SQL查詢:MySQL的Prosedural語言1個

CREATE PROCEDURE tree_add_node(IN id int(11), IN name varchar(50), IN label varchar(50)) 
BEGIN 
DECLARE lft_value int(11), rht_value int(11), lvl_value int(11); 
START TRANSACTION; 
SELECT `lft` INTO lft_value FROM trees WHERE `tree_id`=id; 
SELECT `rht` INTO rht_value FROM trees WHERE `tree_id`=id; 
SELECT `lvl` INTO lvl_value FROM trees WHERE `tree_id`=id; 
UPDATE `trees` SET `tree_id`=`tree_id` +1,`description`='null',`lft`=`lft`+2,`rht`=`rht`+2 WHERE `tree_id`>= id; 
INSERT INTO `trees`(`tree_id`, `name`, `label`, `description`, `lft`, `rht`, `lvl`) VALUES (id,'name','label',NULL,lft_value,rht_value,lvl_value) 
COMMIT; 
END; 

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 ' rht_value int(11), lvl_value int(11); START TRANSACTION; SELECT 
`lft` INTO lf' at line 3 
+2

外觀正常。這是一個語法錯誤。並且簡單地複製粘貼代碼而沒有解釋任何東西都不會幫助你。 –

+0

如果我能找到我的錯誤,那麼我不會發布它 –

+0

http://dev.mysql.com/doc/refman/5.7/en/declare-local-variable.html – Shadow

回答

0

有在你的程序中的一些錯誤

1)您在聲明3個變量,定義數據類型所有一個聲明是錯誤的聲明。
[溶膠]定義一個數據類型來一個聲明語句

Use this :- DECLARE lft_value , rht_value , lvl_value int(11); 

2)忘記分號插入查詢後,你的代碼

[sol] INSERT INTO `trees`(`tree_id`, `name`, `label`, `description`, `lft`, `rht`, `lvl`) VALUES (id,'name','label',NULL,lft_value,rht_value,lvl_value); 

CREATE PROCEDURE tree_add_node(IN id int(11), IN name varchar(50), IN label varchar(50)) 
BEGIN 
DECLARE lft_value , rht_value , lvl_value int(11); 
START TRANSACTION; 
SELECT `lft` INTO lft_value FROM trees WHERE `tree_id`=id; 
SELECT `rht` INTO rht_value FROM trees WHERE `tree_id`=id; 
SELECT `lvl` INTO lvl_value FROM trees WHERE `tree_id`=id; 
UPDATE `trees` SET `tree_id`=`tree_id` +1,`description`='null',`lft`=`lft`+2,`rht`=`rht`+2 WHERE `tree_id`>= id; 
INSERT INTO `trees`(`tree_id`, `name`, `label`, `description`, `lft`, `rht`, `lvl`) VALUES (id,'name','label',NULL,lft_value,rht_value,lvl_value); 
COMMIT; 
END; 
+0

感謝vipin,但受此過程影響的0行。 –