在存储过程中声明 2 个游标 | SQL 错误 [1338] [42000]:处理程序声明之后的游标声明

问题描述 投票:0回答:1

我试图在存储过程中声明两个游标,但它显示

SQL 错误 [1338] [42000]:处理程序声明后的游标声明 处理程序声明后的游标声明 处理程序声明后的游标声明

我尝试在单独的存储过程中单独运行两个游标,它工作正常,但当我将它们组合起来时,它会说另外的情况。请介意帮我解决这个问题。预先感谢!

我的示例查询如下

BEGIN
 DECLARE is_done INT DEFAULT 0;
 DECLARE is_done2 INT DEFAULT 0;

 DECLARE variables VARCHAR(16383) DEFAULT "";
 DECLARE variables2 VARCHAR(255) ;

 DECLARE cursor1 CURSOR FOR SELECT (statement);
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET is_done = 1;

 DECLARE cursor2 CURSOR FOR SELECT (statement);
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET is_done2 = 1;

 OPEN cursor1;
 loop1: LOOP
   FETCH cursor1 INTO variables;
   IF is_done = 1 THEN
     LEAVE loop1;
   END IF;
   (statement)
 END LOOP loop1;
 CLOSE cursor1;

 OPEN cursor2;
 loop2: LOOP
   FETCH cursor2 INTO variables2;
   IF is_done2 = 1 THEN
     LEAVE loop2;
   END IF;
   (statement)
 END LOOP loop2;
 CLOSE cursor2;

END;
mysql stored-procedures database-cursor
1个回答
0
投票

不知何故,我以这种方式编辑并且成功了。

BEGIN
 DECLARE is_done INT DEFAULT 0;

 DECLARE variables VARCHAR(16383) DEFAULT "";
 DECLARE variables2 VARCHAR(255) ;

 DECLARE cursor1 CURSOR FOR SELECT (statement);

 DECLARE cursor2 CURSOR FOR SELECT (statement);

 DECLARE CONTINUE HANDLER FOR NOT FOUND SET is_done = 1;

 OPEN cursor1;
 loop1: LOOP
   FETCH cursor1 INTO variables;
   IF is_done = 1 THEN
     LEAVE loop1;
   END IF;
   (statement)
 END LOOP loop1;
 CLOSE cursor1;

 SET is_done = 0;

 OPEN cursor2;
 loop2: LOOP
   FETCH cursor2 INTO variables2;
   IF is_done = 1 THEN
     LEAVE loop2;
   END IF;
   (statement)
 END LOOP loop2;
 CLOSE cursor2;

END;
© www.soinside.com 2019 - 2024. All rights reserved.