在表tChild的FOR EACH ROW触发器中不允许对表tChild执行DML操作。

解决方案 »

  1.   

    CREATE OR REPLACE TRIGGER TRIG_SUM_tParent_tChild
    AFTER INSERT 
      OR UPDATE 
      OR DELETE 
    ON tChild
    FOR EACH ROW
    declare
      i number;
    BEGIN
      if deleting or updating then
         if :old.child_value is not null then
           UPDATE tParent
                SET tParent.sum_child = tParent.sum_child-:OLD.child_value,
                    tParent.count_child = tParent.count_child-1
                WHERE tParent.parent_id = :new.parent_id; 
         end if;
      end if;
      if inserting or updating then
         if :new.child_value is not null then
           UPDATE tParent
                SET tParent.sum_child = NVL(tParent.sum_child,0)+:NEW.child_value,
                    tParent.count_child = NVL(tParent.count_child,0)+1
                WHERE tParent.parent_id = :new.parent_id; 
         end if;
      end if;
    END;
    /
      

  2.   

    谢谢!实现了计数。不能按组进行SUM吗?如果是想把子表的多行一个例合计入父表的一行一个例,就没办法了?
      

  3.   

    有一点小问题,改过就可以了。
    CREATE OR REPLACE TRIGGER TRIG_SUM_tParent_tChild
    AFTER INSERT 
      OR UPDATE 
      OR DELETE 
    ON tChild
    FOR EACH ROW
    declare
      i number;
    BEGIN
      if deleting or updating then
         if :old.child_value is not null then
           UPDATE tParent
                SET tParent.sum_child = tParent.sum_child - :OLD.child_value,
                    tParent.count_child = tParent.count_child-1
                WHERE tParent.parent_id = :OLD.parent_id; 
         end if;
      end if;
      if inserting or updating then
         if :new.child_value is not null then
           UPDATE tParent
                SET tParent.sum_child = NVL(tParent.sum_child,0) + :NEW.child_value,
                    tParent.count_child = NVL(tParent.count_child,0)+1
                WHERE tParent.parent_id = :new.parent_id; 
         end if;
      end if;
    END;