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 2 columns from select

Author  Topic 

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-04-28 : 19:28:07
How do i update 2 columns from a select?

update table
set column1 = (select value from table where criteria)

How do i do this when i want to update two columns?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-28 : 19:35:10
Here is an example with a join, not exactly sure what your situation is:

UPDATE t1
SET Column1 = t2.ColumnM, Column2 = t2.ColumnZ
FROM Table1 t1
JOIN Table2 t2
ON t1.ColumnA = t2.ColumnA

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

Subscribe to my blog
Go to Top of Page

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-04-28 : 19:45:09
thanks -- i found an example where the from statement was a select of the columns

is the join method faster?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-28 : 19:46:23
Well it depends on what you are trying to do. The example that you found sounds like it is using a derived table.

We can't say which method is better unless we know your exact business requirement.

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

Subscribe to my blog
Go to Top of Page

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-04-28 : 19:53:55
I have about 3M records.

I need to update 2 values in table 2 from table 1 -- common column is rowID



Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-28 : 19:54:50
Then the join method should be used like I posted.

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

Subscribe to my blog
Go to Top of Page
   

- Advertisement -