Sub topic 15
Bulk DML Errors with `FORALL SAVE EXCEPTIONS`
This chapter assumes that you know basic SQL, PL/SQL blocks, collections, and transaction control. You will learn how to let a FORALL statement continue after individual DML errors and how to inspect the failures afterward.
Why `FORALL` needs special error handling
FORALL sends many similar INSERT, UPDATE, or DELETE operations to SQL efficiently. Without SAVE EXCEPTIONS, one failed iteration stops the statement immediately.
With SAVE EXCEPTIONS, Oracle:
- Attempts every iteration.
- Saves information about failed iterations.
- Raises one exception, usually
ORA-24381, after all iterations finish. - Makes the failures available through
SQL%BULK_EXCEPTIONS.
The successful DML remains part of the current transaction. It is not automatically rolled back when ORA-24381 is raised.
Handling the bulk exception
Declare and name the bulk exception with PRAGMA EXCEPTION_INIT. The ERROR_INDEX value identifies the collection iteration that failed. ERROR_CODE contains the Oracle error number without its minus sign, so pass its negative value to SQLERRM.
DECLARE
TYPE t_divisors IS TABLE OF NUMBER;
l_divisors t_divisors := t_divisors(2, 0, 5);
e_bulk_errors EXCEPTION;
PRAGMA EXCEPTION_INIT(e_bulk_errors, -24381);
BEGIN
FORALL i IN 1 .. l_divisors.COUNT SAVE EXCEPTIONS
UPDATE employees
SET salary = salary / l_divisors(i)
WHERE department_id = 50;
DBMS_OUTPUT.PUT_LINE('All iterations succeeded');
EXCEPTION
WHEN e_bulk_errors THEN
DBMS_OUTPUT.PUT_LINE(
'Failed iterations: ' || SQL%BULK_EXCEPTIONS.COUNT
);
FOR j IN 1 .. SQL%BULK_EXCEPTIONS.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(
'Collection index ' ||
SQL%BULK_EXCEPTIONS(j).ERROR_INDEX ||
', error: ' ||
SQLERRM(-SQL%BULK_EXCEPTIONS(j).ERROR_CODE)
);
END LOOP;
ROLLBACK;
END;
/In this example, the second divisor is zero. Oracle still attempts the other iterations, then transfers control to the handler. The ROLLBACK is an explicit policy choice. Remove it if successful changes should remain pending for later commit.
| Attribute | Meaning |
|---|---|
| `SQL%BULK_EXCEPTIONS.COUNT` | Number of failed iterations |
| `ERROR_INDEX` | Collection index used by the failed iteration |
| `ERROR_CODE` | Oracle error number, stored as a positive value |
Common mistakes
- Omitting
SAVE EXCEPTIONS, which causes the first error to stop processing. - Treating
ERROR_INDEXas a table row identifier. It identifies the collection iteration, not necessarily a database key. - Assuming that
FORALLautomatically rolls back successful iterations. - Using
WHEN OTHERSwithout examiningSQL%BULK_EXCEPTIONS. - Committing inside the handler before deciding whether partial work is acceptable.
Exercise
If three of ten FORALL iterations fail with SAVE EXCEPTIONS, how many iterations does Oracle attempt, and what exception does the block receive afterward?
Show answer
Oracle attempts all ten iterations. Afterward, the block receives the bulk error, normally ORA-24381, and the three failures can be read from SQL%BULK_EXCEPTIONS.