Saturday, 13 April 2024

Bulk Exception with FORALL .. SAVE EXCEPTION

 The Bulk Exception are used to save the exception information and continue processing .

In order to  bulk collect exception information, we use FORALL clause with SAVE EXCEPTIONS keyword.

All exceptions raised during execution as saved in %BULK_Exception attribute.

SQL%BULK_EXCEPTION(i).ERROR_CODE  hold the corresponding Oracle error code.

SQL%BULK_EXCEPTION(i).ERROR_INDEX  holds the iteration number of the FORALL statement.

SQL%BUK_EXCEPTION.COUNT  holds the total number of exceptions encountered.


Select * BULK COLLECT INTO  v_collections
From TestTable;

FORALL  idx  IN  v_collections.FIRST .. v_collections.LAST  SAVE EXCEPTIONs
.....
.....

EXCEPTION

 WHEN OTHERS THEN

 FOR idx IN 1..  SQL%BULK_EXCEPTIONS.COUNT
 LOOP
   v_ind := 
SQL%BULK_EXCEPTIONS(idx).Error_index ;   

 Dbms_output.Put_Line('Error encountered at '||SQL%BULK_EXCEPTIONS(idx).Error_index);

 Dbms_output.Put_Line('Values '||v_collections(v_ind).Emp_id
                                || v_collections(v_ind).First_name
                                || v_collections(v_ind).Last_name );

 Dbms_output.Put_Line('Oracle Error Ora-Code '||SQL%BULK_EXCEPTIONS(idx).Error_code);

 End Loop;

End;


No comments:

Post a Comment