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)
 Will this do what I am hoping it will?

Author  Topic 

caelwind
Starting Member

3 Posts

Posted - 2008-06-12 : 15:59:47
Will this work???

Update [table1]
Set [table1].[field1] = [table2].[field3]
Where [table1].[field1] = [table2].[field2]

I have a temp table [table2] with the corrected value [field3] in it
I need to update every row in [table1] where [field1] matches the incorrect [field2] in [table2]

If not, please point me in the right direction

Thanks,
Caelwind

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2008-06-12 : 16:07:46
It should work.. are you getting any errors?

Chirag

http://www.chirikworld.com
Go to Top of Page

Van
Constraint Violating Yak Guru

462 Posts

Posted - 2008-06-12 : 16:09:36
Will probably need a FROM clause in there...
Go to Top of Page

caelwind
Starting Member

3 Posts

Posted - 2008-06-12 : 16:10:45
No errors, havent even put it on the SQL server yet, just thought I'd run it by everyone before I went forward.
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2008-06-12 : 16:13:18
[code]
Aha..ya it requires an From clause ..
Update t1
Set t1.[field1] = t2.[field3]
From [table1] t1 Inner Join [table2] t2
Where t1.[field1] = t2.[field2]
[/code]

Chirag

http://www.chirikworld.com
Go to Top of Page

caelwind
Starting Member

3 Posts

Posted - 2008-06-13 : 10:24:48
Got it working. . .
Here is the working query for reference.

UPDATE [table1]
SET [table1].[Field1] = [table2].[field2]
FROM [table1] INNER JOIN [table2]
ON [table1].[Field1] = [table2].[field3]
WHERE [table1].[Field1] = [table2].[field3]


Not sure if I needed the Where statement, but I just wanted to be sure it was only hitting the data I wanted.

Thanks everyone for all your help.
Caelwind
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-13 : 10:50:53
quote:
Originally posted by caelwind

Got it working. . .
Here is the working query for reference.

UPDATE [table1]
SET [table1].[Field1] = [table2].[field2]
FROM [table1] INNER JOIN [table2]
ON [table1].[Field1] = [table2].[field3]
WHERE [table1].[Field1] = [table2].[field3]


Not sure if I needed the Where statement, but I just wanted to be sure it was only hitting the data I wanted.

Thanks everyone for all your help.
Caelwind


WHERE not needed because the join is already matching based on same criteria.
Go to Top of Page
   

- Advertisement -