title: Merge data into two destination tables
body: <p>I need to merge data from a donor table into two destination tables. The structure is as below. If a projid is not found in the trace table, i need to create new component in the component table and use the new id to insert into the trace table. Also, for those items that no longer exist in the donor table, the trace table 'active' column should be marked 0. Can i achieve this in a single merge statement?</p>
  
  <p><strong>Donor Table</strong></p>
  
  <pre><code>projid      | datestamp    | Ownerid
  -------------------------------------------------
  c_abc        1-jan-2013      name1
  c_def        2-jan-2013      name3
  c_ghi        3-jan-2013      name4
  </code></pre>
  
  <p><strong>trace table</strong> </p>
  
  <pre><code>compid      |projid     |active | ... 
  -----------------------------------------------
  123           c_abc      1
  124           c_xyz      1
  125           c_def      1
  </code></pre>
  
  <p><strong>component table</strong></p>
  
  <pre><code>compid      |ownerid
  -------------------------
  123      name1
  124      name2
  125      name3
  </code></pre>
  
  <p><em>OUTPUT TABLES AFTER MERGE:</em></p>
  
  <p><strong>component table</strong></p>
  
  <pre><code>compid      |ownerid
  -------------------------
  123      name1
  124      name2
  125      name3
  126      name4
  </code></pre>
  
  <p><strong>trace table</strong> </p>
  
  <pre><code>compid      |projid     |active | ... 
  -----------------------------------------------
  123          c_abc       1
  124          c_xyz       0
  125          c_def       1
  126          c_ghi       1
  </code></pre>