Home » RDBMS Server » Server Administration » Audit all schema's tables, in one trigger/table...
Audit all schema's tables, in one trigger/table... [message #149635] Fri, 02 December 2005 09:40 Go to next message
Tafer
Messages: 64
Registered: July 2005
Location: Here!
Member
Beautiful things of life... I have to implement a small audit system for a production system.

The problem:
- Can't use Oracle audit capabilities. (DBA issues)
- Can't create an audit table for each relevant table.

So, I'm looking for a simple table with this info:
- User
- Date
- Action(Insert/delete/update)
- Modified Table
- Operation (Insert into.../ Delete ... / Update ...)

Please, help me identify a proper trigger and the views/tables I can use to get that info...

Thanks in advance.

[Updated on: Fri, 02 December 2005 11:47]

Report message to a moderator

Re: Audit every scheme table, in one trigger... [message #149641 is a reply to message #149635] Fri, 02 December 2005 10:04 Go to previous messageGo to next message
wpaillan
Messages: 35
Registered: March 2005
Location: Santiago - Chile
Member
trigger and the views/tables/procedure :
***************************

DROP TABLE AUDIT_VALFASEALTA;
CREATE TABLE AUDIT_VALFASEALTA
(
ID_SOCIEDAD VARCHAR(2) CONSTRAINT NN_AUDIT_VALFASEALTA_IDSOC NOT NULL,
ID_EMPLEADO VARCHAR(10) CONSTRAINT NN_AUDIT_VALFASEALTA_IDEMP NOT NULL,
FEC_ALTA_EMPLEADO DATE CONSTRAINT NN_AUDIT_VALFASEALTA_FALTA NOT NULL,
ID_CONCEPTO NUMBER(4) CONSTRAINT NN_AUDIT_VALFASEALTA_IDHAB NOT NULL,
FEC_INICIO DATE CONSTRAINT NN_AUDIT_VALFASEALTA_FEINI NOT NULL,
FEC_PAGO DATE CONSTRAINT NN_AUDIT_VALFASEALTA_FPAGO NOT NULL,
ID_TIPOAUDI VARCHAR(10) NOT NULL,
ID_ORDINAL NUMBER(10) NULL,
FEC_FIN DATE NULL,
VALOR NUMBER(14) NULL,
ID_TIPO_APLICACION NUMBER(2) NULL,
ID_USUARIO VARCHAR(40) DEFAULT USER NULL,
FEC_ULT_M DATE DEFAULT SYSDATE NULL,
COMENT_SYS VARCHAR(2000) NULL,
CONSTRAINT PK_AUDIT_VALFASEALTA_ID PRIMARY KEY (ID_SOCIEDAD,ID_EMPLEADO,FEC_ALTA_EMPLEADO,ID_CONCEPTO,FEC_INICIO,FEC_PAGO,ID_TIPOAUDI,ID_ORDINAL)
);

DROP PROCEDURE INS_AUDIT_VALFASEALTA;
CREATE OR REPLACE PROCEDURE M4PR_INS_AUDIT_VALFASEALTA (
IN_SOCIEDAD IN VARCHAR,
IN_EMPLEADO IN VARCHAR,
IN_FECALTA IN DATE,
IN_CONCEPTO IN NUMBER,
IN_FECNICIO IN DATE,
IN_TIPMOV IN VARCHAR,
IN_FECFIN IN DATE,
IN_VALOR IN NUMBER,
IN_TIPOAPLI IN NUMBER,
IN_COMENTSYS IN VARCHAR
) IS
BEGIN
DECLARE
/* Working storage Procedure */
V_FECPAGO DATE;
BEGIN

/* Construimos Siempre la Fecha de Pago representativa del Mes "### 25/mes/año ###"*/
SELECT TO_DATE(SYSDATE,'DD/MM/YY') INTO V_FECPAGO FROM DUAL;
V_FECPAGO := TO_DATE('25/'||TO_CHAR(V_FECPAGO,'MM/YYYY'),'DD/MM/YYYY');
BEGIN
INSERT INTO AUDIT_VALFASEALTA (ID_SOCIEDAD,ID_EMPLEADO, FEC_ALTA_EMPLEADO,ID_CONCEPTO,FEC_INICIO,FEC_PAGO,ID_TIPOAUDI
,FEC_FIN,VALOR,ID_TIPO_APLICACION,COMENT_SYS)
VALUES (IN_SOCIEDAD,IN_EMPLEADO,IN_FECALTA,IN_CONCEPTO,IN_FECNICIO,V_FECPAGO,IN_TIPMOV,IN_FECFIN,IN_VALOR,IN_TIPOAPLI,IN_COMENTSYS);
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR (-20303, '..........UPPSSS, caramba no audite registro........');
END;

END;
END INS_AUDIT_VALFASEALTA;
/

CREATE OR REPLACE TRIGGER TG_UPD_tablaname
BEFORE INSERT OR UPDATE
ON tablename_original
FOR EACH ROW
DECLARE
VCOMENT VARCHAR(100):= NULL;
VOFECFIN DATE := NULL;
VNFECFIN DATE := NULL;
BEGIN
IF INSERTING THEN
INS_AUDIT_VALFASEALTA(:NEW.ID_SOCIEDAD,:NEW.ID_EMPLEADO,:NEW.FEC_ALTA_EMPLEADO,:NEW.ID_CONCEPTO,:NEW.FEC_INICIO,'Insert'
,:NEW.FEC_FIN,:NEW.VALOR,:NEW.ID_TIPO_APLICACION,'');
END IF;

VOFECFIN := NVL(:OLD.FEC_FIN,TO_DATE('01/01/1990','DD/MM/YYYY'));
VNFECFIN := NVL(:NEW.FEC_FIN,TO_DATE('01/01/1990','DD/MM/YYYY'));

IF UPDATING AND (VOFECFIN <> VNFECFIN OR :OLD.VALOR <> :NEW.VALOR OR :OLD.ID_TIPO_APLICACION <> :NEW.ID_TIPO_APLICACION)THEN
IF VOFECFIN <> VNFECFIN THEN
VCOMENT := VCOMENT||' -Se Movio ** Fecha de Fin ** '||to_char(:old.fec_fin,'dd/mm/yyyy')||' to '||to_char(:new.fec_fin,'dd/mm/yyyy');
END IF;
IF :OLD.VALOR <> :NEW.VALOR THEN
VCOMENT := VCOMENT||' -Se Movio ** el Valor ** $'||to_char(:old.VALOR)||' to $'||to_char(:new.VALOR);
END IF;
IF :OLD.ID_TIPO_APLICACION <> :NEW.ID_TIPO_APLICACION THEN
VCOMENT := VCOMENT||' -Se Movio ** Tipo Aplicacion ** '||TO_CHAR(:old.ID_TIPO_APLICACION)||' to '||TO_CHAR(:new.ID_TIPO_APLICACION);
END IF;
INS_AUDIT_VALFASEALTA(:NEW.ID_SOCIEDAD,:NEW.ID_EMPLEADO,:NEW.FEC_ALTA_EMPLEADO,:NEW.ID_CONCEPTO,:NEW.FEC_INICIO,'Update'
,:NEW.FEC_FIN,:NEW.VALOR,:NEW.ID_TIPO_APLICACION,VCOMENT);
END IF;
:NEW.FEC_ULT_ACTUALIZACION := SYSDATE;
:NEW.ID_USUARIO := USER;
END;
/

CREATE OR REPLACE TRIGGER TG_DEL_VAL_FASES_ALTA
BEFORE DELETE
ON tablename_original
FOR EACH ROW
BEGIN
IF DELETING THEN
INS_AUDIT_VALFASEALTA(:OLD.ID_SOCIEDAD,:OLD.ID_EMPLEADO,:OLD.FEC_ALTA_EMPLEADO,:OLD.ID_CONCEPTO,:OLD.FEC_INICIO,'Delete'
,:OLD.FEC_FIN,:OLD.VALOR,:OLD.ID_TIPO_APLICACION,'');
END IF;
END;
/
Re: Audit every scheme table, in one trigger/table... [message #149647 is a reply to message #149635] Fri, 02 December 2005 10:49 Go to previous messageGo to next message
Tafer
Messages: 64
Registered: July 2005
Location: Here!
Member
Hmm...

Thanks for the quick answer!

Now, "tablename_original" is the schema, right? (then the trigger I must use is a schema based trigger)
But, I can't see where/when you are setting this values:
:NEW.id_sociedad,
:NEW.id_empleado,
:NEW.fec_alta_empleado,
:NEW.id_concepto,
:NEW.fec_inicio,
:NEW.fec_fin,
:NEW.valor,
:NEW.id_tipo_aplicacion



Re: Audit every scheme table, in one trigger/table... [message #149653 is a reply to message #149647] Fri, 02 December 2005 11:32 Go to previous messageGo to next message
wpaillan
Messages: 35
Registered: March 2005
Location: Santiago - Chile
Member
hi

1.- "tablename_original" is the schema, right? = yes

2.- The fields Come From the tablename_original:
id_sociedad,
id_empleado,
fec_alta_empleado,
id_concepto,
fec_inicio,
fec_fin,
valor,
id_tipo_aplicacion

3.- The trigger sees these values
references Trigger values is =
:new.fields table (insert or Update)
:old.fields table (Delete or Update)


salu2
williams
  • Attachment: AUDIT.txt
    (Size: 7.16KB, Downloaded 1082 times)
Re: Audit all schema's tables, in one trigger/table... [message #149655 is a reply to message #149635] Fri, 02 December 2005 11:50 Go to previous messageGo to next message
Tafer
Messages: 64
Registered: July 2005
Location: Here!
Member
I get it:
CREATE OR REPLACE TRIGGER m4tg_del_val_fases_alta
   BEFORE DELETE
   ON m4t_val_fase_alta
   FOR EACH ROW
BEGIN

The trigger is based on the table m4t_val_fase_alta

But I was looking for a trigger over a schema, and I can't assume any column/table name. (In other words, super generic trigger)
Re: Audit every schema's table, in one trigger/table... [message #149657 is a reply to message #149635] Fri, 02 December 2005 11:51 Go to previous messageGo to next message
prs100
Messages: 3
Registered: September 2005
Location: mysore
Junior Member
Use Autonomous Transaction feature inside the Trigger body.
This logs the Changes even if the Transaction is rolled back.
Thanks,

Prashanth Subbakrishna.
Mysore.
Re: Audit all schema's tables, in one trigger/table... [message #149676 is a reply to message #149635] Fri, 02 December 2005 17:19 Go to previous message
Tafer
Messages: 64
Registered: July 2005
Location: Here!
Member
This autonomous transactions are quite interesting. But it aren't what I'm looking for...

Let me put this in a more "realistic situation":
CREATE TABLE schema_audit (
   user_responsible VARCHAR(20),
   date_commited DATE,
   table_name VARCHAR2(20),
   operation VARCHAR2(1),
   operation_code VARCHAR2(2000)                                 -- or a long?
);

CREATE OR REPLACE TRIGGER schema_audit_trg
   AFTER INSERT OR DELETE OR UPDATE
   ON schema_name                                         -- Is this possible?
   FOR EACH ROW
DECLARE
   v_operation   schema_audit.operation%TYPE;
BEGIN
   IF INSERTING
   THEN
      v_operation := 'I';
   ELSIF UPDATING
   THEN
      v_operation := 'U';
   ELSE
      v_operation := 'D';
   END IF;

   BEGIN
      INSERT INTO schema_audit
                  (user_responsible, date_commited, table_name, operation,
                   operation_code)
         SELECT USER, SYSDATE, v1.table_name, v_operation, v2.code
           FROM unknown_view1 v1, unknown_view2 v2    -- This is what i need!
          WHERE 1 = 1;
   EXCEPTION
      WHEN OTHERS
      THEN
         raise_application_error
                          (-20001,
                           'Sweet baby Jesus, the audit registration failed!'
                          );
   END;
END;

[Updated on: Fri, 02 December 2005 17:20]

Report message to a moderator

Previous Topic: Invalid database objects
Next Topic: Shared Pool Resize
Goto Forum:
  


Current Time: Sun Jan 26 13:21:32 CST 2025