Tuesday, 5 March 2013

Trigger With The When Condition


create table Emp_tab as select * from emp where 1=2;
Table created.

CREATE OR REPLACE TRIGGER Print_salary_changes
BEFORE
DELETE OR INSERT OR UPDATE ON Emp_tab
      FOR EACH ROW
    WHEN (new.Empno > 0)
    DECLARE
        sal_diff number;
    BEGIN
        sal_diff  := :new.sal  - :old.sal;
        dbms_output.put('Old salary: ' || :old.sal);
       dbms_output.put('  New salary: ' || :new.sal);
       dbms_output.put_line('  Difference ' || sal_diff);
 END;

insert into Emp_tab ( select * from emp where deptno=10);

Old salary:   New salary: 2450  Difference
Old salary:   New salary: 5000  Difference
Old salary:   New salary: 1300  Difference
3 rows created.

update Emp_tab set sal=9000;
Old salary: 2450  New salary: 9000  Difference 6550
Old salary: 5000  New salary: 9000  Difference 4000
Old salary: 1300  New salary: 9000  Difference 7700
3 rows updated.

delete Emp_tab;
3 rows deleted.

No comments:

Post a Comment