0
votes

after i insert this code, i got an error telling that

ORA-04091: table (schema).EMPLOYEES is mutating, trigger/function may not see it

ORA-06512: at "HR.TRGADDEMP_1215034", line 7

ORA-04088: error during execution of trigger 'HR.TRGADDEMP_1215034'

What should i do?

CREATE OR REPLACE TRIGGER TrgAddEmp_1215034
        AFTER INSERT ON Employees
        FOR EACH ROW

        DECLARE
        v_name varchar2(35);
        v_managerName varchar2(20);
        v_dept varchar2(35);

        BEGIN
        Select first_name||' '||last_name
        INTO v_name
        FROM Employees where employee_id = :new.employee_id;

        Select last_name 
        into v_managerName
        from employees where employee_id = :new.manager_id;

        Select department_name
        into v_dept
        from departments where department_id = :new.department_id;

        IF v_managerName is NULL THEN
        DBMS_OUTPUT.PUT_LINE('There is a new employee '|| v_name );
        DBMS_OUTPUT.PUT_LINE('The supervisor for this employee has not been decided');
        DBMS_OUTPUT.PUT_LINE('This employee is assigned to '||v_dept|| 'Department');
        ELSIF v_dept is NULL THEN
        DBMS_OUTPUT.PUT_LINE('There is a new employee '|| v_name );
        DBMS_OUTPUT.PUT_LINE('This employee is supervised by ' || v_managerName);
        DBMS_OUTPUT.PUT_LINE('The department for this employee has not been decided');
        ELSE
        DBMS_OUTPUT.PUT_LINE('There is a new employee '|| v_name );
        DBMS_OUTPUT.PUT_LINE('This employee is supervised by ' || v_managerName);
        DBMS_OUTPUT.PUT_LINE('This employee is assigned to '||v_dept|| 'Department');
        END IF;
        END;
1
replace AFTER INSERT ON Employees with `before INSERT ON Employees' - Moudiz

1 Answers

1
votes

You should insert a correct code instead of this :)

ORA-4091 means that your triggers tried to select data from it's own table. Just don't do it. Never. Row-level triggers will not allow that.

This code tries to select a data which you already have.

Why you're trying to select first_name & last_name when you have :new.first_name and :new.last_name? And so on...