title: Error when using the MERGE command in DB2
body: <p>I'm trying to update or insert data into a table using the <code>MERGE</code> command in DB2 v11. However I keep getting the error: </p>
  
  <blockquote>
    <p>SQL0723N  An error occurred in a triggered SQL statement in trigger "TRACE_AFTE".<br>
    Information returned for the error includes SQLCODE "-746", SQLSTATE "57053" and message tokens 
    "CUSTOM_AFTER_UPDAT|SP_CUSTOM_AFTER_U".  SQLSTATE=09000"</p>
  </blockquote>
  
  <p>I looked up the error and it said that the routine conflicted with the use of the table..that the table is already in use.</p>
  
  <p>I've tried changing the query to update based on <code>VALUES</code> instead of table but get an error on an unexpected token </p>
  
  <blockquote>
    <p>SQL0104N  An unexpected token "INSERT" was found following "MATCHED        THEN".
      Expected tokens may include:  "".  SQLSTATE=42601
    "</p>
  </blockquote>
  
  <pre><code>MERGE INTO TRACE target 
  USING IMPORT_RAW source
       ON target.DETAIL_NUMBER = source.DETAIL_LINE_ID
            AND target.DESC = source.TRACE_TYPE
  WHEN MATCHED THEN
     UPDATE SET NUMBER = source.PRO
  </code></pre>
  
  <p>With   <code>VALUES</code></p>
  
  <pre><code>MERGE TRACE AS T
  USING IMPORT_RAW AS S
        ON (T.DETAIL_NUMBER= S.DETAIL_LINE_ID) AND T.DESC = S.TRACE_TYPE
  WHEN NOT MATCHED BY TARGET     
  THEN INSERT(DETAIL_NUMBER, TRACE_NUMBER) VALUES(S.DETAIL_LINE_ID, S.CARRIER_PRO)
  WHEN MATCHED 
       THEN UPDATE SET T.NUMBER= S.PRO;
  </code></pre>
  
  <p>The query should update multiple records from the <code>Import_raw</code> table where the record exists and import where it doesn't.   </p>
  
  <p>What am I doing wrong?</p>