2017-06-28 21 views
-1

新規または古い列をすべて監査するトリガーを作成しようとしています。ここにテーブルがありますoracleトリガーを使用して監査証跡を作成します。

ID NUMBER(10,0) No  1 
ENTITY_ID NUMBER(10,0) No  2 
LOCATION_ID NUMBER(10,0) Yes  3 
NAME VARCHAR2(128 CHAR) No  4 
CREATED_BY VARCHAR2(255 CHAR) No 'unknown' 5 
UPDATED_BY VARCHAR2(255 CHAR) No 'unknown' 6 
TS_CREATED TIMESTAMP(6) No "SYSTIMESTAMP 
    " 7 

私はトリガーを書く方法がわかりません。

CREATE OR REPLACE TRIGGER audit_security_zones 
    BEFORE DELETE OR INSERT OR UPDATE ON security_zones 
    FOR EACH ROW 
DECLARE 

BEGIN 

END; 

また、アプリケーションからクライアント識別子senbtを使用したいと考えています。ここに私の監査証跡表があります。

ID NUMBER(10,0) No  1 
ACTION VARCHAR2(20 BYTE) Yes  2 
TABLE_ID VARCHAR2(100 BYTE) Yes  3 
OLD_VALUE VARCHAR2(1000 BYTE) Yes  4 
NEW_VALUE VARCHAR2(1000 BYTE) Yes  5 
USERNAME VARCHAR2(100 BYTE) Yes  6 
TS_UPDATED TIMESTAMP(6) No systimestamp 7 

これは良い方法ですか?次に、各テーブルに基づいてユーザーのビューをレンダリングして、変更内容を表示します。私は、次の

CREATE OR REPLACE TRIGGER audit_security_zones 
    BEFORE DELETE OR INSERT OR UPDATE ON security_zones 
    FOR EACH ROW 
DECLARE 
    var_action CHAR(1); 
BEGIN 
    IF INSERTING THEN var_action := 'INSERT'; 
    ELSIF UPDATING THEN var_action := 'UPDATE'; 
    ELSE    var_action := 'DELETE'; 
    END IF; 

    /* loop eahc column in the update or insert or delete statement executed */ 
    for each col in stmt/row{ 
    INSERT INTO audit_table 
    (
    ACTION, 
    TABLE_ID, 
    OLD_VALUE, 
    NEW_VALUE, 
    USERNAME, 
    TS_CREATED 
    ) VALUES (
    var_action, 
    :OLD.ID, 
    :OLD.columnVALUE, 
    :new.COLUMNVALUE, 
    ociidentifier, 
    systimestamp 
    ) 
    } 
END; 

に似た何かを達成しようとしています

UPDATE

アップデート2

CREATE OR REPLACE TRIGGER audit_security_zones 
    BEFORE DELETE OR INSERT OR UPDATE ON security_zones 
    FOR EACH ROW 
DECLARE 
    var_action CHAR(6); 
BEGIN 
    IF INSERTING THEN var_action := 'INSERT'; 
    ELSIF UPDATING THEN var_action := 'UPDATE'; 
    ELSE    var_action := 'DELETE'; 
    END IF; 

    IF var_action = 'DELETE' THEN 
     INSERT INTO audit_table 
     (
      ACTION, 
      USERNAME, 
      ID, 
      O_ENTITY_ID, 
      O_LOCATION_ID, 
      O_NAME, 
      O_CREATED_BY, 
      O_UPDATED_BY, 
      O_TS_CREATED    
     ) VALUES (
      var_action, 
      USERNAME, 
      :OLD.ID, 
      :OLD.ENTITY_ID, 
      :OLD.LOCATION_ID, 
      :OLD.NAME, 
      :OLD.CREATED_BY, 
      :OLD.UPDATED_BY, 
      :OLD.TS_CREATED   
     ); 
    ELSEIF var_action = 'INSERT' THEN 
     INSERT INTO audit_table 
     (
      ACTION, 
      USERNAME, 
      ID,   
      N_ENTITY_ID, 
      N_LOCATION_ID, 
      N_NAME, 
      N_CREATED_BY, 
      N_UPDATED_BY 
      N_TS_CREATED 
     ) VALUES (
      var_action, 
      USERNAME, 
      :NEW.ID,    
      :NEW.ENTITY_ID, 
      :NEW.LOCATION_ID, 
      :NEW.NAME, 
      :NEW.CREATED_BY, 
      :NEW.UPDATED_BY, 
      :NEW.TS_CREATED 
     ); 
    ELSEIF var_action = 'UPDATE' THEN 
     INSERT INTO audit_table 
     (
      ACTION, 
      USERNAME, 
      ID, 
      O_ENTITY_ID, 
      O_LOCATION_ID, 
      O_NAME, 
      O_CREATED_BY, 
      O_UPDATED_BY, 
      O_TS_CREATED, 
      N_ENTITY_ID, 
      N_LOCATION_ID, 
      N_NAME, 
      N_CREATED_BY, 
      N_UPDATED_BY 
      N_TS_CREATED 
     ) VALUES (
      var_action, 
      USERNAME, 
      :OLD.ID, 
      :OLD.ENTITY_ID, 
      :OLD.LOCATION_ID, 
      :OLD.NAME, 
      :OLD.CREATED_BY, 
      :OLD.UPDATED_BY, 
      :OLD.TS_CREATED, 
      :NEW.ENTITY_ID, 
      :NEW.LOCATION_ID, 
      :NEW.NAME, 
      :NEW.CREATED_BY, 
      :NEW.UPDATED_BY, 
      :NEW.TS_CREATED 
     ); 
    END IF; 
END; 

私は次のエラー

を取得

Update3と私は今取得

CREATE OR REPLACE TRIGGER audit_security_zones 
    BEFORE DELETE OR INSERT OR UPDATE ON security_zones 
    FOR EACH ROW 
DECLARE 
    var_action CHAR(6); 
BEGIN 
    IF INSERTING THEN var_action := 'INSERT'; 
    ELSIF UPDATING THEN var_action := 'UPDATE'; 
    ELSE    var_action := 'DELETE'; 
    END IF; 

    IF var_action = 'DELETE' THEN 
     INSERT INTO audit_table 
     (
      ACTION, 
      USERNAME, 
      ID, 
      O_ENTITY_ID, 
      O_LOCATION_ID, 
      O_NAME, 
      O_CREATED_BY, 
      O_UPDATED_BY, 
      O_TS_CREATED    
     ) VALUES (
      var_action, 
      USERNAME, 
      :OLD.ID, 
      :OLD.ENTITY_ID, 
      :OLD.LOCATION_ID, 
      :OLD.NAME, 
      :OLD.CREATED_BY, 
      :OLD.UPDATED_BY, 
      :OLD.TS_CREATED   
     ); 
    ELSE IF var_action = 'INSERT' THEN 
     INSERT INTO audit_table 
     (
      ACTION, 
      USERNAME, 
      ID,   
      N_ENTITY_ID, 
      N_LOCATION_ID, 
      N_NAME, 
      N_CREATED_BY, 
      N_UPDATED_BY, 
      N_TS_CREATED 
     ) VALUES (
      var_action, 
      USERNAME, 
      :NEW.ID,    
      :NEW.ENTITY_ID, 
      :NEW.LOCATION_ID, 
      :NEW.NAME, 
      :NEW.CREATED_BY, 
      :NEW.UPDATED_BY, 
      :NEW.TS_CREATED 
     ); 
    ELSE IF var_action = 'UPDATE' THEN 
     INSERT INTO audit_table 
     (
      ACTION, 
      USERNAME, 
      ID, 
      O_ENTITY_ID, 
      O_LOCATION_ID, 
      O_NAME, 
      O_CREATED_BY, 
      O_UPDATED_BY, 
      O_TS_CREATED, 
      N_ENTITY_ID, 
      N_LOCATION_ID, 
      N_NAME, 
      N_CREATED_BY, 
      N_UPDATED_BY, 
      N_TS_CREATED 
     ) VALUES (
      var_action, 
      USERNAME, 
      :OLD.ID, 
      :OLD.ENTITY_ID, 
      :OLD.LOCATION_ID, 
      :OLD.NAME, 
      :OLD.CREATED_BY, 
      :OLD.UPDATED_BY, 
      :OLD.TS_CREATED, 
      :NEW.ENTITY_ID, 
      :NEW.LOCATION_ID, 
      :NEW.NAME, 
      :NEW.CREATED_BY, 
      :NEW.UPDATED_BY, 
      :NEW.TS_CREATED 
     ); 
    END IF; 
END; 

エラーは、私が手

CREATE OR REPLACE TRIGGER audit_security_zones 
    BEFORE DELETE OR INSERT OR UPDATE ON security_zones 
    FOR EACH ROW 
DECLARE 
    var_action CHAR(6); 
BEGIN 
    IF INSERTING THEN var_action := 'INSERT'; 
    ELSIF UPDATING THEN var_action := 'UPDATE'; 
    ELSE    var_action := 'DELETE'; 
    END IF; 

    IF var_action = 'DELETE' THEN 
     INSERT INTO audit_table 
     (
      ACTION, 
      USERNAME, 
      ID, 
      O_ENTITY_ID, 
      O_LOCATION_ID, 
      O_NAME, 
      O_CREATED_BY, 
      O_UPDATED_BY, 
      O_TS_CREATED    
     ) VALUES (
      var_action, 
      'test', 
      :OLD.ID, 
      :OLD.ENTITY_ID, 
      :OLD.LOCATION_ID, 
      :OLD.NAME, 
      :OLD.CREATED_BY, 
      :OLD.UPDATED_BY, 
      :OLD.TS_CREATED   
     ); 
    ELSIF var_action = 'INSERT' THEN 
     INSERT INTO audit_table 
     (
      ACTION, 
      USERNAME, 
      ID,   
      N_ENTITY_ID, 
      N_LOCATION_ID, 
      N_NAME, 
      N_CREATED_BY, 
      N_UPDATED_BY, 
      N_TS_CREATED 
     ) VALUES (
      var_action, 
      'test', 
      :NEW.ID,    
      :NEW.ENTITY_ID, 
      :NEW.LOCATION_ID, 
      :NEW.NAME, 
      :NEW.CREATED_BY, 
      :NEW.UPDATED_BY, 
      :NEW.TS_CREATED 
     ); 
    ELSIF var_action = 'UPDATE' THEN 
     INSERT INTO audit_table 
     (
      ACTION, 
      USERNAME, 
      ID, 
      O_ENTITY_ID, 
      O_LOCATION_ID, 
      O_NAME, 
      O_CREATED_BY, 
      O_UPDATED_BY, 
      O_TS_CREATED, 
      N_ENTITY_ID, 
      N_LOCATION_ID, 
      N_NAME, 
      N_CREATED_BY, 
      N_UPDATED_BY, 
      N_TS_CREATED 
     ) VALUES (
      var_action, 
      'test', 
      :OLD.ID, 
      :OLD.ENTITY_ID, 
      :OLD.LOCATION_ID, 
      :OLD.NAME, 
      :OLD.CREATED_BY, 
      :OLD.UPDATED_BY, 
      :OLD.TS_CREATED, 
      :NEW.ENTITY_ID, 
      :NEW.LOCATION_ID, 
      :NEW.NAME, 
      :NEW.CREATED_BY, 
      :NEW.UPDATED_BY, 
      :NEW.TS_CREATED 
     ); 
    END IF; 
END; 

新しいエラーUPDATE4

Error(90,4): PLS-00103: Encountered the symbol ";" when expecting one of the following:  if 

ある

Error(9,3): PL/SQL: SQL Statement ignored 
Error(19,4): PL/SQL: ORA-00904: "O_TS_CREATED": invalid identifier 
Error(32,3): PL/SQL: SQL Statement ignored 
Error(42,4): PL/SQL: ORA-00904: "N_TS_CREATED": invalid identifier 
Error(55,9): PL/SQL: SQL Statement ignored 
Error(71,4): PL/SQL: ORA-00904: "N_TS_CREATED": invalid identifier 
+0

監査トリガーの例については、[この回答](https://stackoverflow.com/questions/20020020/basic-oracle-trigger-audit-table)を参照してください。 –

+0

私の質問を更新しました – shorif2000

+1

UPDATE2で間違っていることが分かりましたが、N_UPDATED_BYとN_TS_CREATEDの間の最後のINSERTにカンマがありません –

答えて

1

あなたは「ループ」OLDとNEWの値によって、あなたはあなたがINSERT文をラップするための手順を記述することで、多少のコード​​を簡素化することができるかもしれ明示すなわち

INSERT INTO audit_table 
    (
    ACTION, 
    TABLE_ID, 
    OLD_VALUE, 
    NEW_VALUE, 
    USERNAME, 
    TS_CREATED 
    ) VALUES (
    var_action, 
    :OLD.ID, 
    :OLD.column1VALUE, 
    :new.COLUMN1VALUE, 
    ociidentifier, 
    systimestamp 
    ); 
INSERT INTO audit_table 
    (
    ACTION, 
    TABLE_ID, 
    OLD_VALUE, 
    NEW_VALUE, 
    USERNAME, 
    TS_CREATED 
    ) VALUES (
    var_action, 
    :OLD.ID, 
    :OLD.column2VALUE, 
    :new.COLUMN2VALUE, 
    ociidentifier, 
    systimestamp 
    ); 
... etc. 

各1を参照することはできませんそれをINSERTステートメント自体の代わりに何度も呼び出すことです。

+0

update2 – shorif2000