假设我有一个Oracle PL/SQL块,它将记录插入表中,需要从唯一约束错误中恢复,如下所示:
begin insert into some_table ('some', 'values'); exception when ... update some_table set value = 'values' where key = 'some'; end;
为了捕获唯一约束错误,是否可以替换省略号?
EXCEPTION WHEN DUP_VAL_ON_INDEX THEN UPDATE
我确定你有理由,但为了以防万一......你还应该考虑使用"合并"查询:
begin merge into some_table st using (select 'some' name, 'values' value from dual) v on (st.name=v.name) when matched then update set st.value=v.value when not matched then insert (name, value) values (v.name, v.value); end;
(将上面的内容修改为开始/结束块;显然你也可以独立于程序运行它).
我怀疑你正在寻找的条件是 DUP_VAL_ON_INDEX
EXCEPTION WHEN DUP_VAL_ON_INDEX THEN DBMS_OUTPUT.PUT_LINE('OH DEAR. I THINK IT IS TIME TO PANIC!')