DBAsupport.com Forums - Powered by vBulletin
Results 1 to 4 of 4

Thread: table mutating problem (trigger)

  1. #1
    Join Date
    Oct 2011
    Posts
    1

    Question table mutating problem (trigger)

    I have my trigger that run correctly without errors - but when I want to test it with : delete emp where deptno = 20;
    it is giving me an error :
    table xxx.EMP is mutating, trigger/function may not see it

    the trigger does : when given a deptno it delete the dept of this emp (records of emp with deptno)

    Help please.... Thanks!

    here is the code:

    show errors;

    create or replace trigger deleteDEPT
    after delete on EMP
    for each row
    declare
    v_deptno EMP.deptno%Type;
    counter EMP.deptno%Type;
    begin
    select count(*) into counter from EMP
    where EMP.deptno = :OLD.deptno;
    if counter = 0 then
    delete DEPT;
    --delete_dept(v_deptno);
    end if;
    end;
    /


    I tried to use this to resolve the problem, but nothing.


    ALTER TABLE emp DROP CONSTRAINT fk_dept_deptno;
    ALTER TABLE emp ADD (
    CONSTRAINT fk_dept_deptno
    FOREIGN KEY (DEPTNO)
    REFERENCES DEPT(DEPTNO) ON DELETE CASCADE
    );
    Last edited by thom; 10-29-2011 at 05:23 AM.

  2. #2
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    This is happening because trigger is attempting to access OLD.deptno after delete.
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  3. #3
    Join Date
    Nov 2000
    Location
    Pittsburgh, PA
    Posts
    4,166
    Quote Originally Posted by PAVB View Post
    This is happening because trigger is attempting to access OLD.deptno after delete.
    No its happening because he is causing a mutating table error by selecting on the same table that this trigger is written on.

    Code:
    select count(*) into counter from EMP
    where EMP.deptno = :OLD.deptno;

  4. #4
    Join Date
    Nov 2000
    Location
    Pittsburgh, PA
    Posts
    4,166
    Thom,

    You need the row level trigger to insert rows into a temporary table based on a transaction id. Then write an after delete statement trigger to query the rows deleted and delete any departments that no longer have any employees. You might also considered just marking the departments as defunct because you may have other records that are dependent on the department even though no one works in that department.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width