Need to "tie" UPDATE
with ORDER BY
. I'm trying to use cursors, but get the error:
cursor "cursupd" doesn't specify a line,
SQL state: 24000
Code:
BEGIN;
DECLARE cursUpd CURSOR FOR SELECT * FROM "table" WHERE "field" = 5760 AND "sequence" >= 0 AND "sequence" < 9 ORDER BY "sequence" DESC;
UPDATE "table" SET "sequence" = "sequence" + 2 WHERE CURRENT OF cursUpd;
CLOSE cursUpd;
COMMIT;
How to do it correctly?
UPDATE 1
Without cursor, when I do like this:
UPDATE "CableLinePoint" AS "t"
SET "sequence" = t."sequence" + 2
from (
select max("sequence") "sequence", "id"
from "CableLinePoint"
where
"CableLine" = 5760
group by "id"
ORDER BY "sequence" DESC
) "s"
where "t"."id" = "s"."id" and "t"."sequence" = "s"."sequence"
I get the unique error. So, need to update from the end rather than from the beginning.
UPDATE 2
Table:
id|CableLine|sequence
10| 2 | 1
11| 2 | 2
12| 2 | 3
13| 2 | 4
14| 2 | 5
Need to update (increase) the field "sequence". "sequence" have "index" type, so cannot be done:
UPDATE "table" SET "sequence" = "sequence" + 1 WHERE "CableLine" = 2
When "sequence" in the row with id = 10
is incremented by 1
I receive an error that another row with "sequence" = 2
already exists.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…