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

后端 未结 4 879
情深已故
情深已故 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:40

    As an alternative to explicitly catching and handling the exception you could tell Oracle to catch and automatically ignore the exception by including a /*+ hint */ in the insert statement. This is a little faster than explicitly catching the exception and then articulating how it should be handled. It is also easier to setup. The downside is that you do not get any feedback from Oracle that an exception was caught.

    Here is an example where we would be selecting from another table, or perhaps an inner query, and inserting the results into a table called TABLE_NAME which has a unique constraint on a column called IDX_COL_NAME.

    INSERT /*+ ignore_row_on_dupkey_index(TABLE_NAME(IDX_COL_NAME)) */ 
    INTO TABLE_NAME(
        INDEX_COL_NAME
      , col_1
      , col_2
      , col_3
      , ...
      , col_n)
    SELECT 
        INDEX_COL_NAME
      , col_1
      , col_2
      , col_3
      , ...
      , col_n);
    

    This is not a great solution if your goal it to catch and handle (i.e. print out or update the row that is violating the constraint). But if you just wanted to catch it and ignore the violating row then then this should do the job.

提交回复
热议问题