Database
  Home arrow Database arrow Page 4 - Building an Audit Trail for Your Data
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE

Building an Audit Trail for Your Data
By: Michael Kleane
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 41
    2004-02-25

    Table of Contents:
  • Building an Audit Trail for Your Data
  • Creating Audit Tables
  • Passing Table Names
  • Creating the Audit Triggers
  • Making the Trigger Code Readable
  • Auditing Tips/Techniques

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building an Audit Trail for Your Data - Creating the Audit Triggers


    (Page 4 of 6 )

    Once audit tables exist, a second script is used to create triggers that fire whenever inserts, updates, or deletes occur on the main table.

    The code for audit_triggers.sql is below.  As you will notice, it uses many of the same constructs used in the audit_tables.sql. Two cursors are defined for each table name and definition, then processed.


    --audit_triggers.sql creates (or replacestriggers on audit tables.
    SET SERVEROUTPUT ON SIZE 1000000
    SET FEEDBACK OFF
    SPOOL build_audit_triggers
    .sql
       
    DECLARE
      v_prefix      VARCHAR2
    (5)   := NULL;
      v_condition   VARCHAR2
    (30)  := NULL;
      --
    Select all user tables with a corresponding audit table.
      CURSOR cur_tbl2audit IS
        SELECT table_name 
          FROM user_tables a
          WHERE table_name NOT LIKE 
    '%$AUD'
          
    AND EXISTS 
           
    (SELECT 'x'
              FROM user_tables b
              WHERE b
    .table_name  
              
    SUBSTR(a.table_name,1,26) || '$AUD');
      --
    Select table def of audit tablesans audit columns.      
      CURSOR cur_col2audit
    (p_audittbl USER_TABLES.TABLE_NAME%TYPEIS 
        SELECT column_name 
          FROM user_tab_columns 
          WHERE table_name  
    p_audittbl 
          
    AND column_name NOT IN 
          
    ('AUD_ACTION','AUD_TIMESTAMP','AUD_USER'
     ORDER BY column_id
    ;  
    BEGIN
      
    FOR cur_tbl2audit_rec IN cur_tbl2audit LOOP 
        DBMS_OUTPUT
    .PUT_LINE('CREATE OR REPLACE TRIGGER '||
         SUBSTR
    (cur_tbl2audit_rec.table_name,1,23)||'$AUDTRG '||CHR(10)||
         
    ' AFTER INSERT OR DELETE OR UPDATE '||
         
    'ON '||cur_tbl2audit_rec.table_name||
         
    ' FOR EACH ROW ');
        
    v_prefix       := ':new';
        v_condition    
    := 'IF INSERTING OR UPDATING THEN';
        
    DBMS_OUTPUT.PUT_LINE('DECLARE '||CHR(10)||
         
    'v_operation VARCHAR2(10) := NULL;');
        DBMS_OUTPUT
    .PUT_LINE('BEGIN ');
        
    IF v_prefix ':new' THEN 
           DBMS_OUTPUT
    .PUT_LINE
            
    '    IF INSERTING THEN '||CHR(10)||
            
    '       v_operation := ''INS''; '||CHR(10)||
            
    '    ELSIF UPDATING THEN '||CHR(10)||
            
    '       v_operation := ''UPD''; '||CHR(10)||
            
    '    ELSE '||CHR(10)||
            
    '       v_operation := ''DEL''; '||CHR(10)||
            
    '    END IF; '||CHR(13));
        END 
    IF;
        
    LOOP 
          DBMS_OUTPUT
    .PUT_LINE(v_condition||CHR(10));
          DBMS_OUTPUT
    .PUT_LINE('   INSERT INTO '||
           SUBSTR
    (cur_tbl2audit_rec.table_name,1,26) || '$AUD (');
          
          
    --Loop through 1st to get column names:
          
    FOR cur_col2audit_rec IN cur_col2audit
           
    (cur_tbl2audit_rec.table_nameLOOP 
      DBMS_OUTPUT
    .PUT_LINE(cur_col2audit_rec.column_name|| ',');
          END LOOP
    ;
          
          DBMS_OUTPUT
    .PUT_LINE('aud_action,aud_timestamp,aud_user) '||
        
    'VALUES (');
     
          
    --Loop a 2nd time for the values
          
    FOR cur_col2audit_rec IN cur_col2audit(
           cur_tbl2audit_rec
    .table_nameLOOP          
           DBMS_OUTPUT
    .PUT_LINE(v_prefix||'.'||
            cur_col2audit_rec
    .column_name|| ',');
          END LOOP
    ;
          
          DBMS_OUTPUT
    .PUT_LINE('v_operation,SYSDATE,USER);'||CHR(10));
       
           
    EXIT WHEN v_prefix ':old';
             v_prefix 
    := ':old';
             v_condition 
    := 'ELSE ';
         END LOOP
    ;
         
         DBMS_OUTPUT
    .PUT_LINE('   END IF;'||CHR(10)||
          
    'END;'||CHR(10)||'/'||CHR(10));
       END LOOP

      
    EXCEPTION 
       
    --Any additional error checking would go here.
       WHEN OTHERS THEN 
         DBMS_OUTPUT
    .PUT_LINE('Failure building audit triggers : '||
          SUBSTR
    (SQLERRM,1,200));
         RAISE
    ;
    END
    ;
    /
    SPOOL OFF
    --Build the audit triggers:
    @build_audit_triggers.sql


    More Database Articles
    More By Michael Kleane


       · Hi ,The content is well written and innovative.A charger to readers...
       · With a Web App the user of the database maybe the same (asp.net user) no matter who...
     

    DATABASE ARTICLES

    - Building Applications with Anonymous Types
    - A Closer Look at Anonymous Types
    - Programming with Anonymous Types
    - Converting Your Excel Worksheet into a Worki...
    - Excel Reference
    - Database Programming in C# with MySQL : Usin...
    - Formatting Techniques for Data Access from E...
    - Data Access from Excel VBA
    - Generating a Multiple Table Crystal Report u...
    - ADO and the Command Object
    - On Wiring Up an ADO Data Control
    - Reading and Writing to Files on the Intranet
    - Using ADO Record to Create and Navigate Intr...
    - Using Data Access Pages to Access Data on a ...
    - Using ADO with the SQL Native Client





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek