Skip to Main Content

Rollback to Savepoint

A simple example of how to use rollback inside a loop, to catch & handle all exceptions but still process all the 'good' iterations.
The code below does not do anything really useful, but is shows the principle. For i=1/3/5 processing will continue, for i=2/4/6/ the forced (1/0) exception is caught and handled gracefully.

Running the demo procedure will result in the following exception:
ORA-20000: v_errors= i=2 i=4 i=6
ORA-06512: at "TEDSTRUIK.TED_P1092", line 40

And the dbms_output will be:
i=1
savepoint_01

i=2
savepoint_01
1/0
exception
rollback
v_errors= i=2

i=3
savepoint_01

i=4
savepoint_01
1/0
exception
rollback
v_errors= i=2 i=4

i=5
savepoint_01

i=6
savepoint_01
1/0
exception
rollback
v_errors= i=2 i=4 i=6
You can change the dbms_output to some real log entries, and/or choose not to raise the exception but log that as well.

Code

Package

Package
Name
Source

Package Body

Package Body
Name
Source