0

I want ot update one table(TA) from another(TB) where TB has some data which is not present in TA.

Example TA has ID and Description columns and TB has ID and Description columns.

But some description which is present in TB is not present in TA so I want to update only that description.

I do not think the following will work...

update TB
set TB.Description = TA.Description
from TA
where TB.ID = TA.ID

Please correct me and tell how can I write this?

bapi
  • 1,903
  • 10
  • 34
  • 59
  • possible duplicate of [SQL update from one Table to another based on a ID match](http://stackoverflow.com/questions/224732/sql-update-from-one-table-to-another-based-on-a-id-match) – huMpty duMpty Apr 07 '14 at 09:50
  • Why does `TB` have a `Description` column if it should, in fact, always match the description in `TA`? `TA` already stores this information. Duplication leads to the possibility of "duplicates" getting out of date. – Damien_The_Unbeliever Apr 07 '14 at 09:51
  • You can achieve same thing by using MERGE. Please check below link for same. http://stackoverflow.com/questions/19803257/sql-server-complicated-inserts – Jayesh Goyani Apr 07 '14 at 09:53
  • @huMptyduMpty, Thanks for catching this.I have updated my question. – bapi Apr 07 '14 at 09:58

2 Answers2

0

Try this,

If you want to update where Description of B is NULL

update TB
set TB.Description = TA.Description
from TA
where TB.ID = TA.ID
and TB.Description is null

Or If you want to update where Description of B don't matches Description of A then,

update TB
set TB.Description = TA.Description
from TA
where TB.ID = TA.ID
and TB.Description <> TA.Description
AK47
  • 3,707
  • 3
  • 17
  • 36
0

Try this If you want to update

update TB set TB.Description=TA.Description 
from  TA 
      inner join TB on TA.ID= TA.ID and  TB.Description is null
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99