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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Update new date from another table

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 table

Thank 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.
Go to Top of Page

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 Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-09 : 10:56:39
[code]INSERT INTO Table2 (fields)
SELECT t1.fields
FROM Table1 t1
LEFT JOIN Table2 t2
ON t2.PK=t1.PK
WHERE t2.PK IS NULL
[/code]

this will insert only newly created records in table1 which are not in table2 in table2.

[code]UPDATE t2
SET t2.fields=t1.fields
FROM Table2 t2
INNER JOIN Table1 t1
ON 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.
Go to Top of Page
   

- Advertisement -