3

如果一个或多个迭代崩溃,我需要帮助来完成迭代回滚,如果它们成功则提交所有其他迭代。如果崩溃,它将回滚整个事务。认为这可以通过 Savepoints 完成,但我对它们不是很熟悉。这是我试图实现的一个基本示例。

    DECLARE 
      ...
    BEGIN
      FOR i IN 1 .. 10
      LOOP
        BEGIN
          -- Some DML and stored procs with DML
          INSERT INTO a .. .;
          INSERT INTO b .. .;
          INSERT INTO a .. .;
          DELETE FROM a .. .;
          UPDATE INTO c .. .;
          m_package.some_proc_with_dml;
        EXCEPTION
          WHEN OTHERS THEN
            merror   := merror + || ', ' || + sqlerrm;
            miserror := TRUE;
        END;
      END LOOP;

      COMMIT;

      IF miserror THEN
        raise_application_error(-20000, merror);
      END IF;

    END;

提前致谢。

4

2 回答 2

6

您可以设置 a SAVEPOINT,然后回滚到该 SAVEPOINT 例如:

    DECLARE 
      ...
    BEGIN
      FOR i IN 1 .. 10
      LOOP
        BEGIN

        SAVEPOINT my_savepoint_name;

-- Some DML and stored procs with DML
          INSERT INTO a .. .;
          INSERT INTO b .. .;
          INSERT INTO a .. .;
          DELETE FROM a .. .;
          UPDATE INTO c .. .;
m_package.some_proc_with_dml;
        EXCEPTION
          WHEN OTHERS THEN
            merror   := merror + || ', ' || + sqlerrm;
            miserror := TRUE;

            ROLLBACK TO my_savepoint_name;

        END;
      END LOOP;
于 2013-08-26T09:08:05.543 回答
1

另一种选择是使用PRAGMA AUTONOMOUS_TRANSACTION将循环语句包装在单独存储过程的嵌套调用中。

于 2014-11-29T12:37:02.040 回答