How to catch a unique constraint error in a PL/SQL block?

Thiago Arrais picture Thiago Arrais · Jan 13, 2009 · Viewed 118.8k times · Source

Say I have an Oracle PL/SQL block that inserts a record into a table and need to recover from a unique constraint error, like this:

begin
    insert into some_table ('some', 'values');
exception
    when ...
        update some_table set value = 'values' where key = 'some';
end;

Is it possible to replace the ellipsis for something in order to catch an unique constraint error?

Answer

Ricardo Villamil picture Ricardo Villamil · Jan 13, 2009
EXCEPTION
      WHEN DUP_VAL_ON_INDEX
      THEN
         UPDATE