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
 General SQL Server Forums
 New to SQL Server Programming
 Update if data is different

Author  Topic 

Elvira
Starting Member

4 Posts

Posted - 2010-04-30 : 02:56:18
Hello,
If some one can suggest me something good about:
How to update records in table by using other table only if the data values in the tables are different (where table1.id=table2.id)?

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-04-30 : 03:10:49
[code]
UPDATE T
SET data = t1.data
FROM Table_1 T
JOIN Table_2 T1
ON t.id = t1.id
AND t.data <> t1.data
[/code]
Go to Top of Page

Elvira
Starting Member

4 Posts

Posted - 2010-04-30 : 03:30:29
What if i have 3d table (t3.importID=t2.importID). I need to update t3 also if data in t1 and t3 are different. Tables connected by: t1.id=t2.id, t3.importid=t2.importID (t1-source table and it has some column from t2 and some from t3).
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-04-30 : 03:30:46
Might need to also test if either T.data or T1.data, but not both!, are NULL
Go to Top of Page

tosscrosby
Aged Yak Warrior

676 Posts

Posted - 2010-04-30 : 10:26:49
where (t.data is null and t1.data is not null) or (t1.data is null and t.data is not null)

Terry

-- The problem with socialism is that you eventually run out of other people’s money. -- Margaret Thatcher
Go to Top of Page
   

- Advertisement -