需要UPDATE
与ORDER BY
. 我正在尝试使用游标,但出现错误:
cursor "cursupd" doesn't specify a line, SQL state: 24000
代码:
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;
如何正确执行?
更新 1
没有光标,当我这样做时:
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"
我得到了独特的错误。所以,需要从头更新,而不是从头更新。
更新 2
桌子:
id|CableLine|sequence
10| 2 | 1
11| 2 | 2
12| 2 | 3
13| 2 | 4
14| 2 | 5
需要更新(增加)字段“序列”。“序列”有“索引”类型,所以不能这样做:
UPDATE "table" SET "sequence" = "sequence" + 1 WHERE "CableLine" = 2
当行中的“序列”id = 10
增加时,1
我收到另一行"sequence" = 2
已经存在的错误。