سؤال

I am insertin some values in Table2 from Table1. There could be chances that there are Primary Key collision. I am using EXECUTE IMMEDIATE to insert value from Table1 to Table2.

The records could be in million and there is only 1 commit i.e.,

    execute immediate 'insert into table 2 (select * from table 1)';
    delete from table1;
    commit;
EXCEPTION
  WHEN DUP_VAL_ON_INDEX THEN
     ROLLBACK;
     --LOGGING

Is there a way that i can log the exact row which caused primary key collision in exception block ?

I know i can use "bulk" insert and "save exception" method but for some complex reasons,I am not allowed to change the script for this thing right now.

Any suggestions ?

هل كانت مفيدة؟

المحلول

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                                                                                                                                                                                            

نصائح أخرى

Try using the following:

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

Share and enjoy.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top