0

我有一个带有两个嵌套游标的 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"

非常感谢你的帮助。

4

2 回答 2

0

thanks a lot for your help @JodyT .I am initiating a new block for each loop of the customer cursor (begin after the "if no_more_customer=1" block". Also i am doing "commit" before next customer. Unfortunately i cannot close periods cursor in the exception handler of this block (cursor periods not defined in this scope-compile time error).What i did to overcome the issue was to define an exception handler inside the inner loop (exactly after the continue handler of the periods cursor).In this handler i close periods and then raise exception to perform rollback in the outter exception handler

于 2013-03-10T16:27:46.760 回答
0

我在您的代码中看不到任何事务或提交。我会更改代码以包含以下内容:

  • 为每个客户开始新的交易
  • 在开始处理下一个客户之前提交此事务。
  • 在执行回滚之前关闭句点光标。
于 2013-03-09T23:44:13.297 回答