Exception handling 立即执行。。。捕获特定于记录的异常

Exception handling 立即执行。。。捕获特定于记录的异常,exception-handling,plsql,oracle11g,execute-immediate,Exception Handling,Plsql,Oracle11g,Execute Immediate,我在表1的表2中插入了一些值。可能存在主键冲突的可能性。我正在使用executeimmediate将值从Table1插入Table2 记录可能以百万为单位,但只有一次提交,即 execute immediate 'insert into table 2 (select * from table 1)'; delete from table1; commit; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN ROLLBACK;

我在
表1
表2中插入了一些值。可能存在主键冲突的可能性。我正在使用
executeimmediate
将值从
Table1
插入
Table2

记录可能以百万为单位,但只有一次提交,即

    execute immediate 'insert into table 2 (select * from table 1)';
    delete from table1;
    commit;
EXCEPTION
  WHEN DUP_VAL_ON_INDEX THEN
     ROLLBACK;
     --LOGGING
是否有一种方法可以记录导致异常块中主键冲突的确切行

我知道我可以使用“bulk”insert和“saveexception”方法,但是由于一些复杂的原因,我现在不允许更改这个脚本


有什么建议吗

尝试使用以下方法:

DBMS_OUTPUT.PUT_LINE('SQLCODE=' || to_char(SQLCODE) ||
                     ' Error=''' || DBMS_UTILITY.FORMAT_ERROR_STACK ||
                     ''' Backtrace=''' || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE ||
                     '''');

共享和享受。

您可以按照此问题的答案中所述,进行部分插入并在错误日志中捕获错误,而不必失败

另外,我也不确定您为什么要执行
立即执行
。但也许上面只是一个例子,你真的想动态地这样做。你可以从我的实验中得出:

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                                                                                                                                                                                            

尝试使用以下命令:
DBMS\u OUTPUT.PUT_LINE('SQLCODE='| | | to_char(SQLCODE)| | Error='''| | | DBMS_实用工具.FORMAT| | Error| | |'
。你能把它作为一个答案,让我接受吗?