I am trying to perform an update and select ... basically, an index-based update, and then select the updated row id.
This is simple using the OUTPUT clause:
UPDATE Foo SET Bar = 1 OUTPUT INSERTED.Id WHERE Baz = 2
But now, how do I get this in a variable?
DECLARE @id INT
These three do not work:
UPDATE Foo SET Bar = 1 OUTPUT @id = INSERTED.Id WHERE Baz = 2 SET @id = (UPDATE Foo SET Bar = 1 OUTPUT INSERTED.Id WHERE Baz = 2) SET @id = (SELECT Id FROM (UPDATE Foo SET Bar = 1 OUTPUT INSERTED.Id Id WHERE Baz = 2) z)
This last one turned on because it temporarily turned me on when all the red squigglies went to Management Studio. Alas, I get this error:
A nested INSERT, UPDATE, DELETE, or MERGE statement is not allowed in a SELECT statement that is not the immediate source of rows for an INSERT statement.
sql-server
Cory Nelson May 30 '13 at 23:07 2013-05-30 23:07
source share