如何在循环中回滚到下一个迭代?

时间:2015-11-25 03:34:46

标签: sql database oracle exception plsql

为了更容易解释我的问题,我只需粘贴整个代码:

drop table tst;

create table tst
(t1 number(2));

set serveroutput on
DECLARE
  TYPE vltp IS TABLE OF NUMBER(3);
  vl vltp := vltp(2,12,33,344,55,66,7,555,4);
  NUMBER_TO_BIG EXCEPTION;
  PRAGMA EXCEPTION_INIT(NUMBER_TO_BIG, -01438);
BEGIN
  FOR i IN vl.FIRST .. vl.LAST LOOP
    INSERT INTO tst VALUES (vl(i));
    SAVEPOINT ONE;
    END LOOP;
EXCEPTION
  WHEN NUMBER_TO_BIG THEN
    ROLLBACK TO SAVEPOINT ONE;
END;
/

select * from tst;

基本上,当我在表中插入344时,我得到一个异常(NUMBER_TO_BIG),我希望它回滚到循环但跳过该数字。

预期产出:

tst
-----
2
12
33
55
66
7
4

实际输出:

no rows selected

它正在回滚所有更改,而不仅仅是那一个数字。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

您应该在循环内部处理异常。一旦处理了异常,它将继续循环。

SQL> DECLARE
  TYPE vltp IS TABLE OF NUMBER(3);
  vl vltp := vltp(2,12,33,344,55,66,7,555,4);

  NUMBER_TO_BIG EXCEPTION;
  PRAGMA EXCEPTION_INIT(NUMBER_TO_BIG, -01438);

BEGIN
  FOR i IN vl.FIRST .. vl.LAST LOOP
    BEGIN
        INSERT INTO tst VALUES (vl(i));
    EXCEPTION
        WHEN NUMBER_TO_BIG THEN
        NULL;
    END;

    END LOOP;
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/

PL/SQL procedure successfully completed.


SQL> SELECT * FROM tst;

        T1
----------
         2
        12
        33
        55
        66
         7
         4

7 rows selected.

答案 1 :(得分:1)

你应该试试这个......

drop table tst;
--create table
create table tst
(t1 number(2));

--start of code
DECLARE
  TYPE vltp IS TABLE OF NUMBER(3);
  vl vltp := vltp(2, 12, 33, 344, 55, 66, 7, 555, 4);
  NUMBER_TO_BIG EXCEPTION;
  PRAGMA EXCEPTION_INIT(NUMBER_TO_BIG, -01438);
BEGIN
  FOR i IN vl.FIRST .. vl.LAST LOOP
    begin
      INSERT INTO tst VALUES (vl(i));
    exception
      when NUMBER_TO_BIG then
         --log exeption into log table here
        dbms_output.put_line(sqlerrm);
    end;
  END LOOP;
  commit;
exception
  when others then
    --log exeption into log table here
    dbms_output.put_line(sqlerrm);
END;

答案 2 :(得分:0)

Hello i have illustrtaed a small snippet to replicate your scenario. Let me know if this helps.

--Check for existing table with same name and dropping if already exists
DROP TABLE tst;


--Create Table
CREATE TABLE tst
  (t1 NUMBER(2)
  );


--Anonymous block to perform the task 
SET serveroutput ON
DECLARE
TYPE vltp
IS
  TABLE OF NUMBER
  (
    3
  )
  ;
  vl vltp := vltp(2,12,33,344,55,66,7,555,4);
  NUMBER_TO_BIG EXCEPTION;
  PRAGMA EXCEPTION_INIT(NUMBER_TO_BIG, -01438);
BEGIN
  FOR i IN vl.FIRST .. vl.LAST
  LOOP
  BEGIN
    INSERT INTO tst VALUES
      (vl(i)
      );
  EXCEPTION
  WHEN NUMBER_TO_BIG THEN
  NULL;
  dbms_output.put_line('skipping the value');
  END;
  END LOOP;
  COMMIT;
END;