Is it possible to use the same table twice in the update statement?

I am trying to write a query in the following lines:

UPDATE Table i2
SET value = 0
WHERE EXISTS (SELECT 1 FROM Table i1 WHERE i2.ID = i1.ID+1)

The problem is that I get "Wrong syntax near" i2 "." I suppose this is due to the fact that he does not like to give an alias to the table being updated, but if so, how should I refer to it?

+3
source share
2 answers

The update should not only be directly in the table, you can use an alias from the table specified in the sentence From.

UPDATE i2
SET value = 0
FROM Table i2
WHERE EXISTS (SELECT 1 FROM Table i1 WHERE i2.ID = i1.ID+1)
+6
source

"" "SET" - SQL. , , , "" , ?

0

Source: https://habr.com/ru/post/1779920/


All Articles