立即执行...捕获记录特定异常

时间:2013-05-29 16:42:49

标签: exception-handling plsql oracle11g execute-immediate

我在Table2的{​​{1}}中插入了一些值。可能存在主键冲突的可能性。我使用Table1EXECUTE IMMEDIATE的值插入Table1

记录可以是百万,而且只有一个提交,即

Table2

有没有办法可以在异常块中记录导致主键冲突的确切行?

我知道我可以使用“批量”插入和“保存异常”方法但由于某些复杂的原因,我现在不允许更改此脚本。

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

根本没有失败,你可以进行部分插入并在错误日志中捕获错误,如本问题的答案中所述

Oracle INSERT INTO SELECT(...) DUP_VAL_ON_INDEX exception behavior

此外,我不确定你为什么要做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                                                                                                                                                                                            

答案 1 :(得分:0)

尝试使用以下内容:

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

分享并享受。