To update every row in a table, I use this query (and it's working):
I wonder if I can use a similar notation for DELETE?
Something like:
DELETE from test2
from generate_series(1, 1000) as idx
WHERE id = idx;
Won't work as the second FROM is invalid, of course. Any idea how to fix that?
What is the best practice for this kind of operation?
Use USING:
DELETE from test2
using generate_series(1, 1000) idx
WHERE id = idx
What is the best practice for this kind of operation?
You can use generate_series() like Gordon demonstrates. This might even make sense for non-integer types or with an increment <> 1.
For simple cases, the simple query is superior, though:
DELETE FROM test2
WHERE id BETWEEN 1 AND 1000;