View Single Post

   
  #2 (permalink)  
Old 05-10-2008, 03:12 PM
Dan Guzman
 
Posts: n/a
Default Re: updating multiple columns with subselect

> This is not supported in MSsql.
>
> update T1 set (theUpdatedValue, theOtherValue) =
> (select theTop, theValue from T2 where T2.theKey = T1.theID)
>
> Is there a workaround?


You can use the proprietary UPDATE...FROM syntax:

UPDATE T1
SET
theUpdatedValue = T2.theTop,
theOtherValue = T2.theValue
FROM T2
WHERE
T2.theKey = T1.theID

Or with an alias:

UPDATE a
SET
theUpdatedValue = b.theTop,
theOtherValue = b.theValue
FROM T1 a
JOIN T2 b ON
b.theKey = a.theID

--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

"Leif Neland" <leif@neland.dk> wrote in message
news:4822d7e6$0$56781$edfadb0f@dtext02.news.tele.d k...
> This is not supported in MSsql.
>
> update T1 set (theUpdatedValue, theOtherValue) =
> (select theTop, theValue from T2 where T2.theKey = T1.theID)
>
> Is there a workaround?
>
> Other than doing it in a loop from eg asp, over either fields (one
> statement
> per field), or over records (a query with a loop which for each row does a
> select from one table, update other table with the selected values.)
>
>


Reply With Quote