您可以执行部分插入并在错误日志中捕获错误,而不是完全失败,如该问题的答案中所述
Oracle INSERT INTO SELECT(...) DUP_VAL_ON_INDEX 异常行为
另外我不确定你为什么要做一个execute immediate
. 但也许上面只是一个例子,你真的想动态地做到这一点。你可以从我的实验中得出:
create table table1 (mycolumn varchar2(200));
create table table2 (mycolumn varchar2(200));
exec DBMS_ERRLOG.create_error_log (dml_table_name => 'table2');
create unique index table2_i1 on table2 (mycolumn);
insert into table1 values ('one thing');
insert into table1 values ('another thing');
insert into table1 values ('another thing');
INSERT INTO table2
SELECT * FROM table1
LOG ERRORS INTO err$_table2 ('INSERT') REJECT LIMIT UNLIMITED;
commit;
select * from err$_table2;
select * from table2;
输出
table TABLE1 created.
table TABLE2 created.
anonymous block completed
unique index TABLE2_I1 created.
1 rows inserted.
1 rows inserted.
1 rows inserted.
2 rows inserted.
committed.
ORA_ERR_NUMBER$ ORA_ERR_MESG$ ORA_ERR_ROWID$ ORA_ERR_OPTYP$ ORA_ERR_TAG$ MYCOLUMN
--------------- --------------------------------------------------------------------------
1 ORA-00001: unique constraint (SYS.TABLE2_I1) violated I INSERT another thing
MYCOLUMN
--------------
one thing
another thing