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>
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>