日期:2014-05-16  浏览次数:20854 次

mysql中declare的问题
如题,我在SQLyog Enterprise中执行如下代码,但是每次都是declare报错;在mysql命令行中也是如此,请问该怎么用declare呢?
代码如下:
CREATE PROCEDURE p8() 
BEGIN 
DECLARE a INT; 
DECLARE b INT; 
SET a = 5; 
SET b = 5; 
select pkid,name,userGroup_desc,parent_id,group_state from T_VSM_SECPOLICY_USERGROUP; 
END;
错误信息:
Error Code : 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 '' at line 3
(0 ms taken)

Error Code : 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 b INT' at line 1
(0 ms taken)

Error Code : 1193
Unknown system variable 'a'
(0 ms taken)

Error Code : 1193
Unknown system variable 'b'
(0 ms taken)

(35 row(s)returned)
(0 ms taken)

Error Code : 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 'END' at line 1
(0 ms taken)

求解,谢谢

------解决方案--------------------
declare cur0 cursor for select pkid from T_VSM_SECPOLICY_USERGROUP; --这里为什么报错?

declare 必须在其它语句之前。
------解决方案--------------------
delimiter //
CREATE PROCEDURE p8()
BEGIN
declare cur0 cursor for select pkid from T_VSM_SECPOLICY_USERGROUP;
DECLARE a INT;
DECLARE b INT;
SET a = 5;
SET b = 5;
END//