Skip to content

Commit

Permalink
Formatted the example and edited the content
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhibhammar committed Oct 10, 2024
1 parent 90a9792 commit 80abc29
Showing 1 changed file with 10 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ FORALL <index> IN <lower_bound> .. <upper_bound>

Optionally, `SAVE EXCEPTIONS` allows a FORALL statement to continue even if any of the DML statements fail. When a DML statement fails, SPL doesn't raise an exception, it saves information about the failure. After the FORALL statement completes, SPL raises a single exception for the FORALL statement. The error information is stored in the collection of records called `SQL%BULK_EXCEPTIONS`, where:

- `SQL%BULK_EXCEPTIONS(i).ERROR_INDEX` records the number of the DML statement that failed.
- `SQL%BULK_EXCEPTIONS(i).ERROR_INDEX` records the nth number of the DML statement that failed.
- `SQL%BULK_EXCEPTIONS(i).ERROR_CODE` records the database error code for the failure.
- `SQL%BULK_EXCEPTIONS.COUNT` records the total number of the DML statements that failed.

Expand Down Expand Up @@ -177,7 +177,7 @@ a | b

## Using FORALL with SAVE EXCEPTIONS

This example merges (inserts and updates) the records of test_table2 using `FORALL` statement:
This example shows how to use the `SAVE EXCEPTIONS` clause with the `FORALL` statement:

```sql
CREATE TABLE foo(id NUMBER(6) not null, name VARCHAR2(20));
Expand All @@ -186,49 +186,22 @@ INSERT INTO foo values(1, 'Peter');

```sql
DECLARE

TYPE namelist_t IS TABLE OF VARCHAR2 (5000);

names_with_errors namelist_t

:= namelist_t (RPAD ('ABCD', 1000, 'ABC'),

'George',

RPAD ('ABCD', 3000, 'ABC'),

'Max');



names_with_errors namelist_t := namelist_t (RPAD ('ABCD', 1000, 'ABC'),'George',RPAD ('ABCD', 3000, 'ABC'),'Max');
ex_dml_errors EXCEPTION;

PRAGMA EXCEPTION_INIT(ex_dml_errors, -24381);


PRAGMA EXCEPTION_INIT(ex_dml_errors, -24381);

BEGIN

FORALL indx IN 1 .. names_with_errors.COUNT SAVE EXCEPTIONS

UPDATE foo SET name = names_with_errors (indx);


UPDATE foo SET name = names_with_errors (indx);

EXCEPTION

WHEN ex_dml_errors THEN

WHEN ex_dml_errors THEN
-- Handling exceptions

FOR i IN 1..SQL%BULK_EXCEPTIONS.COUNT LOOP

DBMS_OUTPUT.PUT_LINE('SAVE EXCEPTIONS: The Error at ' || SQL%BULK_EXCEPTIONS(i).ERROR_INDEX ||

' Error Code ' || SQL%BULK_EXCEPTIONS(i).ERROR_CODE);

END LOOP;

FOR i IN 1..SQL%BULK_EXCEPTIONS.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('SAVE EXCEPTIONS: The Error at ' || SQL%BULK_EXCEPTIONS(i).ERROR_INDEX ||
' Error Code ' || SQL%BULK_EXCEPTIONS(i).ERROR_CODE);
END LOOP;
END;

__OUTPUT__
Expand Down

0 comments on commit 80abc29

Please sign in to comment.