我有一个带有两个嵌套游标的 sp。外部光标是客户,内部光标是句点。当内部光标发生错误时,我想回滚为特定客户所做的事情并继续处理下一个客户。但是,当为下一个客户执行内部光标时(发生异常后),我收到“光标已打开消息”
代码如下所示:
DECLARE customers CURSOR FOR
select * from customers_table;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_customers = 1;
OPEN customers;
customers_cursor:
REPEAT
FETCH customers
INTO ....
IF no_more_customers = 1
THEN
close customers;
LEAVE customers_cursor;
END IF;
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
rollback;
END;
....... //do some stuff
BEGIN
DECLARE no_more_periods INT(1) DEFAULT 0;
DECLARE periods CURSOR FOR
SELECT ...
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_periods = 1;
OPEN periods;
periods_cursor:
REPEAT
FETCH periods INTO...
IF no_more_periods = 1
THEN
close periods;
LEAVE periods_cursor;
END IF;
..... //do some stuff point 1
UNTIL no_more_periods = 1
END REPEAT periods_cursor;
end;
END;
UNTIL no_more_customers = 1
END REPEAT customers_cursor;
END;
客户 1 运行并且异常发生在//do some stuff point 1
. 然后客户 2 运行,直到open periods
达到声明,那时我得到"Cursor is already open"。
非常感谢你的帮助。