Is it possible to use the same table twice in an update statement?
I'm trying to write a query along these lines:
UPDATE Table i2
SET value = 0
WHERE EXISTS (SELECT 1 FROM Table i1 WHERE i2.ID = 开发者_StackOverflow中文版i1.ID+1)
The problem is that I'm getting "Incorrect syntax near 'i2'." I'm guessing this is because it doesn't like giving a nickname to a table being updated, but if that's the case, how am I supposed to reference it?
An update does not only have to be on a table directly you can use an alias from a table referenced in the From
clause.
UPDATE i2
SET value = 0
FROM Table i2
WHERE EXISTS (SELECT 1 FROM Table i1 WHERE i2.ID = i1.ID+1)
The "value" within the "SET" clause is a keyword of SQL.Further, your logic seems to be clearing all the "value"s except the first row, is it your goal?
精彩评论