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 |
raxbat
Yak Posting Veteran
52 Posts |
Posted - 2008-05-08 : 08:05:34
|
Hello all!I have 2 tables with similar structure!I need to update 1 table with only new data from 2 tableThank You |
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-05-08 : 08:19:36
|
Example?Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-05-08 : 08:40:36
|
What do you mean by new data? new rows? changed rows which exist in Table2 but not in Table1?Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-09 : 10:56:39
|
[code]INSERT INTO Table2 (fields)SELECT t1.fieldsFROM Table1 t1LEFT JOIN Table2 t2ON t2.PK=t1.PKWHERE t2.PK IS NULL[/code]this will insert only newly created records in table1 which are not in table2 in table2.[code]UPDATE t2SET t2.fields=t1.fieldsFROM Table2 t2INNER JOIN Table1 t1ON t1.PK=t2.PK[/code]will pick up changes in data of existing records in table2 and update them. So based on your requirement you can use any one of above or both. |
 |
|
|
|
|