Continue MERGE after EXCEPTION

后端 未结 2 803
悲&欢浪女
悲&欢浪女 2021-01-22 19:05

Is it possible to continue a MERGE after an EXCEPTION ?

MERGE INTO copy_emp c
     USING employees e
     ON (c.employee_id = e.employe         


        
相关标签:
2条回答
  • 2021-01-22 19:12

    There are several ways to avoid termination. I have been mostly using FORALL SAVE EXCEPTIONS clause.

    In Oracle Database 10g and higher, PL/SQL offers three options for “continuing past an exception,”which really means avoiding termination of the execution of the current block. Please read this excellent article by Steven Feuerstein, http://www.oracle.com/technetwork/issue-archive/2009/09-mar/o29plsql-085126.html

    0 讨论(0)
  • 2021-01-22 19:29

    You can do this with the error_logging_clause. (The link is for the insert because in the documentation of MERGE it says that it has the same behavior as an insert.

    For your case :

    -- You create your Log Table
    EXECUTE DBMS_ERRLOG.CREATE_ERROR_LOG('copy_emp', 'TAB_ERR_COPY_EMP');
    
    
        MERGE INTO copy_emp c
         USING employees e
         ON (c.employee_id = e.employee_id)
       WHEN MATCHED THEN
         UPDATE SET
           c.first_name     = e.first_name,
           c.last_name      = e.last_name,
           c.email          = e.email,
           c.phone_number   = e.phone_number,
           c.hire_date      = e.hire_date,
           c.job_id         = e.job_id,
           c.salary         = e.salary,
           c.commission_pct = e.commission_pct,
           c.manager_id     = e.manager_id,
           c.department_id  = e.department_id
       WHEN NOT MATCHED THEN
         INSERT VALUES(e.employee_id, e.first_name, e.last_name,
              e.email, e.phone_number, e.hire_date, e.job_id,
              e.salary, e.commission_pct, e.manager_id,
              e.department_id)
        LOG ERRORS INTO TAB_ERR_COPY_EMP('TAG_STATEMENT') REJECT LIMIT 100;
    

    Please note that there are some limitations for the error_logging_clause. From the documentation :

    1. The following conditions cause the statement to fail and roll back without invoking the error logging capability:

      • Violated deferred constraints.

      • Any direct-path INSERT or MERGE operation that raises a unique constraint or index violation.

      • Any update operation UPDATE or MERGE that raises a unique constraint or index violation).

    2. You cannot track errors in the error logging table for LONG, LOB, or object type columns. However, the table that is the target of the DML operation can contain these types of columns.

      • If you create or modify the corresponding error logging table so that it contains a column of an unsupported type, and if the name of that column corresponds to an unsupported column in the target DML table, then the DML statement fails at parse time.

      • If the error logging table does not contain any unsupported column types, then all DML errors are logged until the reject limit of errors is reached. For rows on which errors occur, column values with corresponding columns in the error logging table are logged along with the control information.

    0 讨论(0)
提交回复
热议问题