调用存储过程中的游标循环,不会触发继续处理程序(Calling a Stored Procedure

2019-09-17 06:32发布

我想游标的循环中调用存储过程在MySQL。 在做循环的INSERT当光标表现正常; 但如果我尝试调用存储过程中,继续处理程序“做套= 1”,并且要提前退出循环,第一条记录被处理后。 如何解决这个有什么想法? 谢谢。

declare test_cursor cursor for 
       select projectid, projectdesc
         from tblProjects
        order by projectdesc;          

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

set done = 0;  
open test_cursor;

repeat

  fetch test_cursor into wprojectid, wprojectdesc;

  if not done then  

    insert into tblTest (a, b) values (wprojectid, wprojectdesc);   <--this would work
    call spTest(wprojectid, wprojectdesc, @retrn);                                    <--this trips the Handler after first loop

  end if;      

until done end repeat;

close test_cursor; 

Answer 1:

我不知道这件事,但尝试看看,如果这个代码的工作或没有?

declare test_cursor cursor for 
       select projectid, projectdesc
         from tblProjects
        order by projectdesc;          

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
DECLARE done_holder INT;


set done = 0;
open test_cursor;

repeat

  fetch trade_cursor into wprojectid, wprojectdesc;

  if not done then  

    set done_holder = done;
    insert into tblTest (a, b) values wprojectid, wprojectdesc;
    call spTest(a, b, @retrn);
    set done = done_holder;

  end if;      

until done end repeat;

close test_cursor; 


Answer 2:

我认为这个问题是在这里 - “叫spTest(A,B,@retrn);”,尝试用这个来改变它 -

CALL spTest(wprojectid, wprojectdesc, @retrn);

所以,你的代码可以是这样的 -

  DECLARE done INT DEFAULT 0;
  DECLARE wprojectid, wprojectdesc  INT;
  DECLARE test_cursor CURSOR FOR SELECT projectid, projectdesc FROM tblProjects ORDER BY projectdesc;
  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

  SET done = 0;
  OPEN test_cursor;

  REPEAT
  FETCH test_cursor INTO wprojectid, wprojectdesc;
    IF NOT done THEN
      INSERT INTO tblTest (a, b) VALUES (wprojectid, wprojectdesc);
      CALL spTest(wprojectid, wprojectdesc, @retrn);
    END IF;
  UNTIL done
  END REPEAT;
  CLOSE test_cursor;


文章来源: Calling a Stored Procedure Within a Cursor Loop, Without Tripping the Continue Handler