Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server 2008 MERGE with Target as table with no values
    primarykey
    data
    text
    <p>I want to do MERGE from a source to a Target where I insert rows if they don't exist and update the ones that do. When I do this, I get only inserts... no updates. I read this post (http://stackoverflow.com/questions/5015623/merge-query-in-sql-server-2008), which is a bit confusing for me to follow, but it seems that I need a duplicate source to make this happen. Can someone explain why a DUPLICATE source would be required? I just don't get it, especially which keys I should be using (duplicate source or regular ones)-- Maybe the duplicate SourceTable is just not needed?</p> <p>here my (simplified) SourceTable :</p> <pre><code> row userid placervalue placerDt --- ------- ---------- ---------- 1 abc a1 1/1/12 2 xyz b1 1/1/12 3 abc b2 1/20/12 etc. </code></pre> <p>and my target TargetTable that is same, but currently with no rows in it.</p> <pre><code> row userid placerId --- ------- ---------- (nothing loaded yet) </code></pre> <p>what I want is to INSERT rows that are NOT in the target, and UPDATE rows that are.</p> <pre><code> row userid placervalue placerDt --- ------- ---------- ---------- 1 abc a1 1/20/12 *** note change here (row 3 updates row 1) 2 xyz b1 1/1/12 </code></pre> <p>When I run this code, I get only inserts (would also like to know why)</p> <pre><code>MERGE TargetTable as t USING SourceTable as s ON s.userid = t.usrid AND s.placervalue = t.placervalue WHEN MATCHED THEN UPDATE SET t.placerDt = s.placerDt WHEN NOT MATCHED THEN INSERT ( userid , placerid , placerDt ) VALUES ( s.userid , s.placerid , s.placerDt ); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload