我正在尝试创建一个触发器,它将子表的值(插入或更新后)聚合到父表。子表C
具有字段,id, year, month, value
而父表具有. 当向 C 插入新行或更新现有行时,触发器应将该年的所有月份值求和,并将总和写入. 这是我实现它的方式:P
id, year, value
P
CREATE OR REPLACE
TRIGGER tr__aggregation
AFTER INSERT OR UPDATE ON C
FOR each row
DECLARE
BEGIN
MERGE INTO P o
USING (SELECT id, year, sum(value) as val
FROM C
WHERE id = :NEW.id
AND year = :NEW.year
GROUP BY id, year) n
ON (n.id = o.id AND n.year = o.year)
WHEN NOT MATCHED THEN INSERT (o.id, o.year, o.value)
VALUES (n.id, n.year, n.value)
WHEN MATCHED THEN UPDATE SET o.value = NVL(n.value, o.value);
EXCEPTION WHEN OTHERS THEN
pck_util.pr_exception_info(TRUE);
RAISE;
END;
出于某种原因,我收到此错误:
SQL Error: ORA-04091: table C is mutating, trigger/function may not see it
我不明白为什么,我不想修改C
,只是P
。谢谢!