Audit operations with a trigger

杀马特。学长 韩版系。学妹 提交于 2019-12-11 14:52:54

问题


I have a trigger that fires in certain conditions, and when I update some data in EMPLOYEES table (specifically when inserting, deleting and updating comm_pct and salary) the changes that were made are registered into the following table:

CREATE TABLE "HR"."AUDIT_E" ("USR" VARCHAR2(30 BYTE) DEFAULT USER,
    "DATE" DATE DEFAULT SYSDATE,
    "DML_TYPE"  VARCHAR2), -- UPDATE, INSERT, DELETE
    "OLD_EMPLOYEE_ID" NUMBER, 
"OLD_FIRST_NAME" VARCHAR2,  
...,--more fields
"OLD_JOB_ID" VARCHAR2, 
"OLD_SALARY" NUMBER, 
"OLD_COMMISSION_PCT" NUMBER,  
"NEW_FIRST_NAME" VARCHAR2, 
..., -- more fields!
"NEW_JOB_ID" VARCHAR2, 
"NEW_SALARY" NUMBER, 
"NEW_COMMISSION_PCT" NUMBER)

My question is: How can I do an INSERT in AUDIT_E (Because I must register old and new values into it) when updating rows with another values (as email with comm_pct and other fields, besides only updating comm_pct and salary)? Because my trigger has the following structure:

IF DELETING THEN
--some actions
-- Insert into AUDIT_E(...) values...
ELSIF INSERTING THEN
    --some actions
-- Insert into AUDIT_E(...) values...
ELSIF UPDATING ('a field') THEN --I have two of these
    --some actions
-- Insert into AUDIT_E(...) values...comm_pct/salary

Thank you very much if you can help me and sorry for my english.

EDIT: My trigger runs fine registring changes into audit_e when I am inserting, deleting rows and updating only comm_pct and salary:

AUDIT_E:
ID |Oper|Old_Name|Old_job_id|Old_comm_pct|Old_Salary|New_name|New_job_id|New_comm_pct|New_salary
------------------------------------------------------------------------------------------------
1  |Ins |  NULL  | NULL     |  NULL      |   NULL   |Kappa   | SA_REP   | 0.2        | 4980 
2  |Upd | Kappa  | SA_REP   | 0.2        | 4980     | NULL   | NULL     | 0.3        | NULL
3  |Upd | Kappa  | SA_REP   | 0.3        | 4980     | NULL   | NULL     | NULL       | 5000
4  |Del | Kappa  | SA_REP   | 0.3        | 4980     | NULL   | NULL     | NULL       | NULL

But when I am changing the job_id for example (putting an additional elsif update), the changes are saved into audit_e wrong:

AUDIT_E:
ID |Oper|Old_Name|Old_job_id|Old_comm_pct|Old_Salary|New_name|New_job_id|New_comm_pct|New_salary
------------------------------------------------------------------------------------------------
1  |Upd |Kappa   | SA_REP   | 0.2        | 4980     | NULL   | NULL     | NULL        | NULL 

And I want those changes saved into audit_e table like this:

 AUDIT_E:
ID |Oper|Old_Name|Old_job_id|Old_comm_pct|Old_Salary|New_name|New_job_id|New_comm_pct|New_salary
------------------------------------------------------------------------------------------------
1  |Upd |Kappa   | SA_REP   | 0.2        | 4980     | NULL   | IT_PROG  | NULL       | NULL 

回答1:


I'm still not sure that I understand the question. My guess is that you just want something like

CREATE OR REPLACE TRIGGER trigger_name
  AFTER INSERT OR UPDATE OR DELETE
  ON employees
  FOR EACH ROW
BEGIN
  INSERT INTO audit_e( dml_type, 
                       old_employee_id, new_employee_id,
                       old_first_name, new_first_name,
                       ...
                     )
    VALUES( CASE WHEN deleting 
                 THEN 'D'
                 WHEN inserting 
                 THEN 'I'
                 WHEN updating
                 THEN 'U'
                 ELSE 'X'
             END,
            :old.employee_id, :new.employee_id,
            :old.first_name, :new.first_name,
            ...
           );
END;

That said, it's not clear to me why you would bother storing the old and new data in your audit table each time. The old_* values are always going to be identical to the new_ values from the prior row. It generally makes sense to just store the new_ values in the audit table.



来源:https://stackoverflow.com/questions/26950413/audit-operations-with-a-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!