现在需求是这样 用游标循环 一个表 把表里的数据 insert到另一张表。有的插入可能有主键重复的错误,这样的异常 捕获输出后,继续插入其它的数据,怎么做呢?
用Exception捕获,放到循环里编译报错;
放到存储过程的end里,出现异常,就直接退出存储过程了,不继续执行了。

解决方案 »

  1.   

    test和test2两张表的id都是主键
    SQL> select * from test;        ID NAME
    ---------- ----------
             1 a
             2 b
             3 c
             4 d
             5 eSQL> select * from test2;        ID NAME
    ---------- ----------
             2 bSQL> begin
      2          for x in (select * from test) loop
      3          begin
      4                  insert into test2 values(x.id,x.name);
      5          exception
      6                  when others then
      7                          dbms_output.put_line('duplicate insert ' || x.id || ' , ' || x.name);
      8          end;
      9          end loop;
     10          commit;
     11  end;
     12  /
    duplicate insert 2 , bPL/SQL procedure successfully completed.SQL> select * from test2;        ID NAME
    ---------- ----------
             2 b
             1 a
             3 c
             4 d
             5 eSQL>