Use exception DUP_VAL_ON_INDEX to handle unique constraint error.
ex.
begin
insert into test_table ('key value', 'values');
exception
when DUP_VAL_ON_INDEX
update test_table set value = 'values' where key = 'key value';
end;
Instead of Using exception we can also use merge feature
ex.
begin
merge into test_table test1
using (select 'key value' name, 'values' value from dual) test2
on (test1.name=test2.name)
when matched then update set test1.value=test2.value
when not matched then insert (name, value) values (test2.name, test2.value);
end;
ex.
begin
insert into test_table ('key value', 'values');
exception
when DUP_VAL_ON_INDEX
update test_table set value = 'values' where key = 'key value';
end;
Instead of Using exception we can also use merge feature
ex.
begin
merge into test_table test1
using (select 'key value' name, 'values' value from dual) test2
on (test1.name=test2.name)
when matched then update set test1.value=test2.value
when not matched then insert (name, value) values (test2.name, test2.value);
end;
Comments
Post a Comment