更新子记录的顺序

时间:2015-03-19 09:41:17

标签: sql oracle

我有两张桌子:

家长

| id |

儿童

| id |老板| ordernr |

owner是引用Parent的id的外键。 (owner,ordernr)

有唯一性约束

现在,订单中存在一些空白,我正在尝试修复它们,如下所示:

CREATE OR REPLACE VIEW myView AS 
(SELECT childid, ordernr, n 
  FROM (SELECT child.id as childid, ordernr, ROW_NUMBER() OVER ( PARTITION BY parent.id ORDER BY ordernr) AS n 
        FROM Parent, Child WHERE owner = parent.id)
WHERE ordernr <> n)

UPDATE 
(SELECT c.ordernr, n
FROM Child c, myView WHERE childid = c.id) t 
SET t.ordernr = t.n

但是我得到:ORA-01779:无法修改映射到非密钥保留表的列

1 个答案:

答案 0 :(得分:1)

  

ORA-01779:无法修改映射到非密钥保留表的列

当您尝试INSERT或UPDATE连接视图中映射到非密钥保留表的列时,会发生此错误。

您可以使用 MERGE

例如,

MERGE INTO child c
USING (SELECT n
       FROM   myview) t
ON (t.childid = c.id)
WHEN matched THEN
  UPDATE SET c.ordernr = t.n

/