title: Add and delete values in MANY-TO-MANY relationship with MERGE SQL-Statement
body: <p>I have 3 Tables:</p>
  
  <pre><code>A:
  -------------
  PK_A
  
  B:
  -------------
  PK_B
  
  A_B:
  -------------
  K_A K_B PK_AB
  </code></pre>
  
  <p>A_B is a table that "Joined" A and B tables. From my application I get 3 lists: </p>
  
  <pre><code>A-List: (PK_A1, PK_A2 ....)
  
  Add-List: (PK_B1, PK_B2 ...)
  
  Delete-List: (PK_B11, PK_B12 ...)
  </code></pre>
  
  <p>For every PK_A from A-List a link should be made between every PK_B from Add-List (added to A_B table). </p>
  
  <p>For every PK_A from A-List a link should be deleted between every PK_B from Delete-List (deleted from A_B table).</p>
  
  <p>K_A K_B Pairs must be unique in A_B table. This is my attempt. It's ok for add-list but I don't know how to use delete-list.</p>
  
  <pre><code>MERGE INTO A_B sz
   USING (SELECT *
            FROM (SELECT t.COLUMN_VALUE AS pk_a
                    FROM TABLE (sys.ku$_objnumset (358456, 367346, 367348)) t),
                 (SELECT t.COLUMN_VALUE AS pk_b
                    FROM TABLE (sys.ku$_objnumset (338, 339)) t)
      ON (sz.K_B = b.K_B AND sz.K_A = b.K_A)                
  WHEN NOT MATCHED
  THEN
  INSERT     (sz.K_A, sz.K_B, sz.PK_AB)
     VALUES (
               b.K_A,
               b.K_B,
               pk_A_B_seq.NEXTVAL)
  </code></pre>
  
  <p>Related questions I asked before:
  <a href="https://stackoverflow.com/q/22327992/2023524">SQL-Statement to use predefined values list as an SQL-table</a></p>