PostgreSQL error code
PostgreSQL error 24000
24000 is the SQLSTATE PostgreSQL reports for invalid cursor state, in class 24, Invalid Cursor State.
| SQLSTATE | 24000 |
|---|---|
| Condition name | invalid_cursor_state |
| Class | 24 — Invalid Cursor State |
Reproduce it, then fix it
WHERE CURRENT OF updates the row the cursor is on, and a freshly declared cursor isn’t on any row yet. FETCH one first. Run the broken query to see the error, then switch to the fixed one. Edit either freely; every run is rolled back.
DECLARE c CURSOR FOR SELECT * FROM products ORDER BY id FOR UPDATE; UPDATE products SET price = price * 0.9 WHERE CURRENT OF c;
DECLARE c CURSOR FOR SELECT * FROM products ORDER BY id FOR UPDATE; FETCH c; UPDATE products SET price = price * 0.9 WHERE CURRENT OF c RETURNING id, name, price;