How to Solve Mutating Trigger Using Compound trigger
How to Solve Mutating
Trigger Using Compound trigger
Mutating ERROR CODE:-
CREATE OR REPLACE TRIGGER APPS.XX_EMP_B_U_TRIG
BEFORE
UPDATE
--OF SAL
ON APPS.XX_EMP
--REFERENCING OLD
AS OLD NEW AS NEW
FOR EACH ROW
DECLARE
V_PRESIDENT_SAL NUMBER;
BEGIN
dbms_output.put_line ('============
Row Level - Before Update Trigger Fired
============');
select SAL INTO
V_PRESIDENT_SAL from xx_emp where JOB= 'PRESIDENT';
IF :NEW.SAL >=
V_PRESIDENT_SAL THEN
dbms_output.put_line ('***** You can not assign this salary to anyone *****');
RAISE_APPLICATION_ERROR(-20100,'You
can not assign this salary to anyone');
ELSE
dbms_output.put_line ('*****
You Can assign this Salary *****');
END IF;
END;
---===========================----
Solution
Compound
Trigger Code:-
CREATE OR REPLACE TRIGGER XX_COMP_EMP_MUT_TRIG
FOR INSERT OR DELETE OR UPDATE
OF SAL
ON XX_EMP
COMPOUND TRIGGER
-- Declarative Section (optional)
for variables and constants
V_PRESIDENT_SAL NUMBER;
--Section 1 --
sequence 1 -
BEFORE STATEMENT IS
BEGIN
dbms_output.put_line ('************ Compound Statement Level - BEFORE Trigger
*************' );
IF INSERTING THEN
dbms_output.put_line ('INSERT');
ELSIF UPDATING THEN
dbms_output.put_line ('UPDATE');
select SAL INTO
V_PRESIDENT_SAL from xx_emp where JOB= 'PRESIDENT';
dbms_output.put_line ('PRESIDENT
Salary Fetched from System : '||V_PRESIDENT_SAL);
ELSIF
DELETING THEN
dbms_output.put_line ('DELETE');
END IF;
END BEFORE STATEMENT;
--Section 2 --
sequence 2 -
BEFORE EACH ROW IS
BEGIN
dbms_output.put_line ('************ Compound Row Level - BEFORE Trigger
*************' );
IF INSERTING THEN
dbms_output.put_line ('INSERT');
ELSIF UPDATING THEN
dbms_output.put_line ('UPDATE');
IF :NEW.SAL >=
V_PRESIDENT_SAL THEN
dbms_output.put_line
('***** You can not assign this salary to anyone *****');
RAISE_APPLICATION_ERROR(-20100,'You
can not assign this salary to anyone');
ELSE
dbms_output.put_line
('*****
You Can assign this Salary *****');
END IF;
ELSIF
DELETING THEN
dbms_output.put_line ('DELETE');
END IF;
END BEFORE EACH ROW;
--Section 3 --
sequence 3 -
AFTER EACH ROW IS
BEGIN
dbms_output.put_line ('************ Compound Row Level - AFTER Trigger
*************' );
IF INSERTING THEN
dbms_output.put_line ('INSERT');
ELSIF UPDATING THEN
dbms_output.put_line ('UPDATE');
ELSIF
DELETING THEN
dbms_output.put_line ('DELETE');
END IF;
END AFTER EACH ROW;
--Section 4 --
sequence 4 -
AFTER STATEMENT IS
BEGIN
dbms_output.put_line ('************
Compound Statement Level - AFTER Trigger
*************' );
IF INSERTING THEN
dbms_output.put_line ('INSERT');
ELSIF UPDATING THEN
dbms_output.put_line ('UPDATE');
ELSIF
DELETING THEN
dbms_output.put_line ('DELETE');
END IF;
END AFTER STATEMENT;
END;
Comments
Post a Comment