2012年5月3日星期四

ERROR 1337: Variable or condition declaration after cursor or handler declaration

FROM: Error 1337 VARIABLE OR CONDITION DECLARATION AFTER CURSOR OR HANDLER DECLARATION


[sql]
create database testdb;
use testdb;
CREATE TABLE test1 (
ID INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
a INTEGER UNSIGNED NULL,
PRIMARY KEY(ID)
);

CREATE TABLE test2 (
ID INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
a INTEGER UNSIGNED NULL,
PRIMARY KEY(ID)
);

Insert into test1 set a = 2;
Insert into test1 set a = 4;
Insert into test1 set a = 6;
Insert into test1 set a = 8;
Insert into test1 set a = 10;

delimiter $$
CREATE PROCEDURE testdb.curdemo()
BEGIN
Declare cur1 Cursor For select a from testdb.test1;
Declare temp INT;
declare done INT Default 0;
Declare continue handler for Sqlstate '02000' set done = 1;

open cur1;
Repeat
fetch cur1 into temp;
IF Not done then
Insert into test2 set a = temp;
End if;
Until done END Repeat;
close cur1;
End $$
delimiter ;
[/sql]

Now, if I would add the procedure, MySQL requests a error: Variable or condition declaration after cursor or handler declaration at line 22.

根据错误的提示信息,游标定义需在变量/条件后,HANDLER 前。
将定义存储过程的 SQL 修改为如下即可。
[sql]
delimiter $$
CREATE PROCEDURE testdb.curdemo()
BEGIN
Declare temp INT;
Declare done INT Default 0;

Declare cur1 Cursor For select a from testdb.test1;
Declare continue handler for Sqlstate '02000' set done = 1;

open cur1;
Repeat
fetch cur1 into temp;
IF Not done then
Insert into test2 set a = temp;
End if;
Until done END Repeat;
close cur1;
End $$
delimiter ;
[/sql]

没有评论 :

发表评论