Capture values that trigger DUP_VAL_ON_INDEX

后端 未结 2 647
不思量自难忘°
不思量自难忘° 2021-01-06 17:31

Given this example (DUP_VAL_ON_INDEX Exception), is it possible to capture the values that violated the constraint so they may be logged?

Would the approach be the s

相关标签:
2条回答
  • 2021-01-06 17:47

    Ideally, I would suggest using DML error logging. For example

    Create the error log table

    begin
      dbms_errlog.create_error_log( dml_table_name => 'EMPLOYEE',
                                    err_log_table_name => 'EMPLOYEE_ERR' );
    end;
    

    Use DML error logging

    BEGIN
      insert into employee( id )
        select id 
          from (select '01' id from dual
                union all
                select '02' from dual) 
        log errors into employee_err
            reject limit unlimited;
    END;
    

    For every row that fails, this will log the data for the row into the EMPLOYEE_ERR table along with the exception. You can then query the error log table to see all the errors rather than getting just the first row that failed.

    If creating the error log table isn't an option, you could move from SQL to PL/SQL with bulk operations. That will be slower but you could use the SAVE EXCEPTIONS clause of the FORALL statement to create a nested table of exceptions that you could then iterate over.

    0 讨论(0)
  • 2021-01-06 17:58

    For people who would be interested to know more about this, please go through this link.

    0 讨论(0)
提交回复
热议问题