How to avoid “table mutating” errors

后端 未结 2 1063
一向
一向 2020-12-22 03:15

I have an trigger that needs to read from a table after deleting a row. Essentially, I need to count up the remaining rows that are similar to the current row, and if that c

2条回答
  •  心在旅途
    2020-12-22 03:56

    You can create a COMPOUND trigger for such cases:

    create or replace TRIGGER Di_PatMustBeWell
    FOR DELETE ON Diagnosis
    COMPOUND TRIGGER
    
        TYPE Di_Patient_Table_type IS TABLE OF DiagnosisCount.Di_Patient%TYPE;
        Di_Patient_Table Di_Patient_Table_type;
    
         BEFORE STATEMENT IS
          BEGIN
            Di_Patient_Table := Di_Patient_Table_type();
         END BEFORE STATEMENT;
    
         BEFORE EACH ROW IS
         BEGIN
            Di_Patient_Table.EXTEND;
            Di_Patient_Table(Di_Patient_Table.LAST) := :OLD.Di_Patient;
         END BEFORE EACH ROW;
    
        AFTER STATEMENT IS
        BEGIN
           FOR i IN Di_Patient_Table.FIRST..Di_Patient_Table.LAST LOOP
              SELECT NUMDISEASES INTO Numdiseases
              FROM DiagnosisCount
              where Di_Patient = Di_Patient_Table(i);
    
              IF NumDiseases !=  1 THEN
                UPDATE Patient SET Pat_Sick = 0 WHERE Pat_Person = Di_Patient_Table(i);
              END IF;
           END LOOP;
           Di_Patient_Table.DELETE;
         END AFTER STATEMENT;
       END;
    /
    

提交回复
热议问题