Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
nguyenl
Posting Yak Master
128 Posts |
Posted - 2009-05-29 : 17:48:34
|
| Hi,I have table 1 which is located both in my production and test database. Due to a mistake, some users have been updating table 1 in production and others have been updating table 1 in test. How do I find out which records are different between the 2 tables and then merge them into the table in production?Thanks |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-05-29 : 22:56:09
|
| if both tables having same noofcolumns with same datatype the use union select col1,col2 from table1union select col1,col2 from table2 -- having same datatype |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-29 : 23:26:54
|
this will list the records in prod not in test and in test not in prod or any col that is diffselect *from [prod].dbo.table1 p full outer join [test].dbo.table1 t on p.pk = t.pkwhere p.pk is null or t.pk is nullor p.col1 <> t.col1 or (p.col1 is null and t.col1 is not null)or p.col2 <> t.col2 or (p.col2 is null and t.col2 is not null) to insert records in test not in prodinsert into [prod].dbo.table1 (pk, col1, col2, col3 . . .)select pk, col1, col2, col3 . . .from [test].dbo.table1 twhere not exists ( select * from [prod].dbo.table1 p where p.pk = t.pk ) for records existed in both DB but with col value difference, you have to decide to take prod or test,to take prod, and ignore test, you don't have do anything. To take test and update prod dbupdate pset col1 = t.col1, col2 = t.col2from [prod].dbo.table1 p inner join [test].dbo.table1 t on p.pk = t.pk and any where clause to it if necessary KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-30 : 02:32:36
|
quote: Originally posted by nguyenl Hi,I have table 1 which is located both in my production and test database. Due to a mistake, some users have been updating table 1 in production and others have been updating table 1 in test. How do I find out which records are different between the 2 tables and then merge them into the table in production?Thanks
if you're using sql 2008, you can use MERGE statement for this. it would be something like:-MERGE prod.dbo.table1 as t1USING test.dbo.table1 as t2ON t2.pk=t1.pkWHEN MATCHED THEN UPDATE SET col1=t2.col1,col2=t2.col2,..WHEN NOT MATCHED BY TARGET THEN INSERT (pk,col1,col2,col3,..) VALUES (t2.pk,t2.col1,t2.col2,...)WHEN NOT MATCHED BY SOURCE THEN DELETEif sql 2005 or less you've do this in three stepsINSERT INTO prod.dbo.table1SELECT pk,col1,col2,... FROM test.dbo.table1 t1LEFT JOIN prod.dbo.table1 t2on t2.pk=t1.pkWHERE t2.pk IS NULLUPDATE t2SET t2.col1=t1.col1,t2.col2=t1.col2,..FROM test.dbo.table1 t1INNER JOIN prod.dbo.table1 t2on t2.pk=t1.pkDELETE t2FROM prod.dbo.table1 t2LEFT JOIN test.dbo.table1 t1on t2.pk=t1.pkWHERE t1.pk IS NULL |
 |
|
|
|
|
|
|
|