Instead of failing at all you could do a partial insert and capture the errors in an error log as described in the answer to this question
Oracle INSERT INTO SELECT(...) DUP_VAL_ON_INDEX exception behavior
Also I am not sure why you want to do an execute immediate
. But maybe above is just an example and you really want to do this dynamically. You can work it out from my experiment:
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;
Output
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