pl sql trigger determine if insert or update or delete

Solutions on MaxInterview for pl sql trigger determine if insert or update or delete by the best coders in the world

showing results for - "pl sql trigger determine if insert or update or delete"
Erik
07 Jun 2020
1CREATE OR REPLACE TRIGGER Audit_emp
2   AFTER INSERT OR UPDATE OR DELETE ON Emp_tab
3   FOR EACH ROW
4   DECLARE
5      Time_now DATE;
6      Terminal CHAR(10);
7   BEGIN
8      -- get current time, and the terminal of the user:
9      Time_now := SYSDATE;
10      Terminal := USERENV('TERMINAL');
11      -- record new employee primary key
12      IF INSERTING THEN 
13         INSERT INTO Audit_table
14            VALUES (Audit_seq.NEXTVAL, User, Time_now,
15               Terminal, 'Emp_tab', 'INSERT', :new.Empno);
16   -- record primary key of the deleted row:
17      ELSIF DELETING THEN                           
18         INSERT INTO Audit_table
19            VALUES (Audit_seq.NEXTVAL, User, Time_now,
20               Terminal, 'Emp_tab', 'DELETE', :old.Empno);
21   -- for updates, record the primary key
22   -- of the row being updated:
23      ELSE 
24         INSERT INTO Audit_table
25            VALUES (audit_seq.NEXTVAL, User, Time_now,
26               Terminal, 'Emp_tab', 'UPDATE', :old.Empno);
27      -- and for SAL and DEPTNO, record old and new values:
28         IF UPDATING ('SAL') THEN
29            INSERT INTO Audit_table_values
30               VALUES (Audit_seq.CURRVAL, 'SAL',
31                  :old.Sal, :new.Sal);
32
33         ELSIF UPDATING ('DEPTNO') THEN
34            INSERT INTO Audit_table_values
35               VALUES (Audit_seq.CURRVAL, 'DEPTNO',
36                  :old.Deptno, :new.DEPTNO);
37         END IF;
38      END IF;
39END;