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 2005 Forums
 Transact-SQL (2005)
 Update table based on another table

Author  Topic 

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2008-07-16 : 13:17:24
I've done this once and I can't find my own sample, so here I am.

I have 2 tables.

First table (tbl1) to read from. tbl1Fld1 and tbl1Fld2
Second table (tbl2) to update. tbl2Fld1 and tbl2Fld2

Go through each row in tbl1. Read tble1Fld1 and find it's match
in tbl2Fld1. Then set tbl2Fld2 = tbl1Fld2 for that match.

Seems simple enough and I've been trying it with a cursor with difficulty and I know there has to be an easier way.

Thanks for anyone who can point me in the right direction.

Zath

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-16 : 13:24:48
You don't need a cursor or any kind of loop for this.

UPDATE t2
SET Fld2 = t1.Fld2
FROM Table2 t2
INNER JOIN Table1 t1
ON t2.Fld1 = t1.Fld1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2008-07-16 : 13:34:40
thanks. I knew ther was an easier way and it's been a while since I came across this scenario.

Tim
Go to Top of Page
   

- Advertisement -