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

后端 未结 4 835
情深已故
情深已故 2021-02-01 01:39

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_ta         


        
4条回答
  •  有刺的猬
    2021-02-01 02:19

    I'm sure you have your reasons, but just in case... you should also consider using a "merge" query instead:

    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;
    

    (modified the above to be in the begin/end block; obviously you can run it independantly of the procedure too).

提交回复
热议问题