2

我试图在游标的循环中调用 MySQL 中的存储过程。在循环中执行 INSERT 时光标行为正常;但是如果我尝试调用存储过程,则 continue Handler '设置完成 = 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; 
4

2 回答 2

2

我不确定,但试着看看这段代码是否有效?

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; 
于 2012-05-16T18:03:17.557 回答
0

我认为问题出在这里 - 'call 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;
于 2012-05-17T07:01:00.223 回答