Avoid the mutating table error. : Trigger Firing « Trigger « Oracle PL / SQL






Avoid the mutating table error.

 
SQL>
SQL> CREATE TABLE lecturer (
  2    id               NUMBER(5) PRIMARY KEY,
  3    first_name       VARCHAR2(20),
  4    last_name        VARCHAR2(20),
  5    major            VARCHAR2(30),
  6    current_credits  NUMBER(3)
  7    );

Table created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10001, 'Scott', 'Lawson','Computer Science', 11);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major, current_credits)
  2                VALUES (10002, 'Mar', 'Wells','History', 4);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10003, 'Jone', 'Bliss','Computer Science', 8);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10004, 'Man', 'Kyte','Economics', 8);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10005, 'Pat', 'Poll','History', 4);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10006, 'Tim', 'Viper','History', 4);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10007, 'Barbara', 'Blues','Economics', 7);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10008, 'David', 'Large','Music', 4);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10009, 'Chris', 'Elegant','Nutrition', 8);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10010, 'Rose', 'Bond','Music', 7);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10011, 'Rita', 'Johnson','Nutrition', 8);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10012, 'Sharon', 'Clear','Computer Science', 3);

1 row created.

SQL>
SQL>
SQL> CREATE OR REPLACE PACKAGE StudentData AS
  2    TYPE t_Majors IS TABLE OF lecturer.major%TYPE
  3      INDEX BY BINARY_INTEGER;
  4    TYPE t_IDs IS TABLE OF lecturer.ID%TYPE
  5      INDEX BY BINARY_INTEGER;
  6
  7    myLecturerMajors t_Majors;
  8    myLecturerIDs    t_IDs;
  9    v_NumEntries    BINARY_INTEGER := 0;
 10  END StudentData;
 11  /

Package created.

SQL>
SQL> CREATE OR REPLACE TRIGGER RLimitMajors
  2    BEFORE INSERT OR UPDATE OF major ON lecturer
  3    FOR EACH ROW
  4  BEGIN
  5    StudentData.v_NumEntries := StudentData.v_NumEntries + 1;
  6    StudentData.myLecturerMajors(StudentData.v_NumEntries) :=
  7      :new.major;
  8    StudentData.myLecturerIDs(StudentData.v_NumEntries) := :new.id;
  9  END RLimitMajors;
 10  /

Trigger created.

SQL>
SQL> CREATE OR REPLACE TRIGGER SLimitMajors
  2    AFTER INSERT OR UPDATE OF major ON lecturer
  3  DECLARE
  4    studentMax     CONSTANT NUMBER := 5;
  5    studentCount      NUMBER;
  6    myLecturerID       lecturer.ID%TYPE;
  7    v_Major           lecturer.major%TYPE;
  8  BEGIN
  9    FOR v_LoopIndex IN 1..StudentData.v_NumEntries LOOP
 10      myLecturerID := StudentData.myLecturerIDs(v_LoopIndex);
 11      v_Major := StudentData.myLecturerMajors(v_LoopIndex);
 12
 13      SELECT COUNT(*)
 14        INTO studentCount
 15        FROM lecturer
 16        WHERE major = v_Major;
 17
 18      IF studentCount > studentMax THEN
 19        RAISE_APPLICATION_ERROR(-20000,
 20          'Too many lecturer for major ' || v_Major ||
 21          ' because of student ' || myLecturerID);
 22      END IF;
 23    END LOOP;
 24
 25    StudentData.v_NumEntries := 0;
 26  END SLimitMajors;
 27  /

Trigger created.

SQL>
SQL> UPDATE lecturer
  2    SET major = 'History'
  3    WHERE ID = 10003;

1 row updated.

SQL>
SQL> UPDATE lecturer
  2    SET major = 'History'
  3    WHERE ID = 10002;

1 row updated.

SQL>
SQL> UPDATE lecturer
  2    SET major = 'History'
  3    WHERE ID = 10009;

1 row updated.

SQL>
SQL>
SQL> drop table lecturer;

Table dropped.

SQL>

 








Related examples in the same category

1.Order of trigger firing.