Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Microsoft released <a href="http://msdn.microsoft.com/en-us/library/ms162843.aspx" rel="nofollow noreferrer">a tool to compare data</a> between SQL tables, this might a good option in certain situations.</p> <p>Edit: Forgot to mention, it also generates a script to insert/update missing or different rows.</p> <p>For completeness, I hacked up this query which does what you want, it updates existing table2 records, and adds those that are missing, based off the email address. </p> <p>The 'updating' and 'insert missing' queries below are the ones you want.</p> <pre><code>BEGIN TRAN create table #table1 (id int, fname varchar(20), email varchar(20)) insert into #table1 values (1, 'name_1_updated', 'email_1') insert into #table1 values (3, 'name_3_updated', 'email_3') insert into #table1 values (100, 'name_100', 'email_100') create table #table2 (id int, fname varchar(20), email varchar(20)) insert into #table2 values (1, 'name_1', 'email_1') insert into #table2 values (2, 'name_2', 'email_2') insert into #table2 values (3, 'name_3', 'email_3') insert into #table2 values (4, 'name_4', 'email_4') print 'before update' select * from #table2 print 'updating' update #table2 set #table2.fname = t1.fname from #table1 t1 where t1.email = #table2.email print 'insert missing' insert into #table2 select * from #table1 where #table1.email not in (select email from #table2 where email = #table1.email) print 'after update' select * from #table2 drop table #table1 drop table #table2 ROLLBACK </code></pre>
    singulars
    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.
    1. VO
      singulars
      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