Wednesday, March 7, 2012

Multiple column update statement

Hi,

I'm new to SQL Server but not new to SQL because I used it with Oracle. I wonder if it is possible to do this kind of statement on SQL Server:

UPDATE TableX M
SET (M.column1, M.column2, M.column3)=
(SELECT T.column1, T.column2, G.column3)
FROM Table_Y T,
Table_Z G
WHERE join condition))
WHERE EXISTS (join condition for TableX)

Basically, I'd like to update multiple columns in one statement but for some reason I can not get it to work.

What would be the equivalent on SQL Server?

Thanks for the help,

Laszlo M

SQL Server does not support row value constructors yet. So you will have to use the TSQL UPDATE statement extension which has a FROM clause like:

UPDATE TableX

SET column1 = T.column1, column2 = T.column2, column3 = G.column3

FROM TableX as M, Table_Y as T, Table_Z as G

WHERE <join condition>

and <condition from exists>

See Books Online UPDATE statement topic for more examples and details on the syntax.

|||

Hi Laszlo,

UPDATE TableX
SET column1 = T.column1, column2 = T.column2, column3 = G.column3
FROM TableX M INNER JOIN
(TableY T INNER JOIN TableZ G ON T.column4 = G.column1)
ON M.column2 = G.column1

Regards,

Mike H.

|||

Thanks for the quick response. All of you were very helpful.

Laszlo

No comments:

Post a Comment