Skip to Main Content
  • Questions
  • how to trap unique id of record with error using <SQL%BULK_EXCEPTIONS/>

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, krishna.

Asked: October 11, 2016 - 2:27 pm UTC

Last updated: October 11, 2016 - 3:18 pm UTC

Version: 12.0.0.1

Viewed 1000+ times

You Asked

From <SQL%BULK_EXCEPTIONS/> we can find out the error_index. This does not allow us to identify the the particular record with error. We should be able to trap the unique id (primary key or whatever) of the record. Only this will allow us to pinpoint the record causing the error with ease.



and Chris said...

You can use the sql%bulk_exceptions.error_index to find the record that caused the issue:

create table t (
  x varchar2(10)
);
declare
  arr dbms_sql.varchar2a;
  
  dml_errors exception;
  pragma exception_init(dml_errors, -24381);
begin
  arr(1) := 'a';
  arr(2) := 'bbbbbbbbbbbb';
  arr(3) := 'c';
  
  forall i in 1 .. arr.count save exceptions
    insert into t values (arr(i));

exception
  when dml_errors then
    for i in 1 .. sql%bulk_exceptions.count loop
      dbms_output.put_line('Error: ' || arr(sql%bulk_exceptions(i).error_index));
    end loop;
end;
/

Error: bbbbbbbbbbbb


If it's an array of records you can return the values from it as you need. See:

https://oracle-base.com/articles/9i/bulk-binds-and-record-processing-9i#save_exceptions

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Dml_Error_logging

Rajeshwaran Jeyabal, October 12, 2016 - 10:28 am UTC

or else, you could erase your plsql code and stick with SQL approach DML_ERROR_LOGGING.

Less code = Less bugs !

demo@ORA12C> create table t(x varchar2(10));

Table created.

demo@ORA12C> exec dbms_errlog.create_error_log('T');

PL/SQL procedure successfully completed.

demo@ORA12C> declare
  2     l_array sys.odcivarchar2list := sys.odcivarchar2list();
  3  begin
  4     l_array.extend(3);
  5     l_array(1) := 'a';
  6     l_array(3) := 'c';
  7     l_array(2) := 'bbbbbbbbbbbb';
  8     insert into t(x)
  9     select column_value from table(l_array)
 10     log errors into err$_T reject limit unlimited;
 11  end;
 12  /

PL/SQL procedure successfully completed.

demo@ORA12C> select * from t;

X
----------
a
c

2 rows selected.

demo@ORA12C> @printtbl "select * from err$_t"
ORA_ERR_NUMBER$               : "12899"
ORA_ERR_MESG$                 : "ORA-12899: value too large for column "DEMO"."T"."X" (actual: 12, maximum: 10)
"
ORA_ERR_ROWID$                : ""
ORA_ERR_OPTYP$                : "I"
ORA_ERR_TAG$                  : ""
X                             : "bbbbbbbbbbbb"
-----------------

PL/SQL procedure successfully completed.

demo@ORA12C>

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library