Search code examples
sqlsql-serverdmlcdc

How to maintain history of multiple tables in a single table without using CDC feature


Is it possible to consolidate the history of all the tables into a single table?

I tried to use the CDC feature provided by SQL server 2012 enterprise edition, but for that it creates a copy of every table, which increases the number of tables in the database.

Is it also possible track & insert the table name & column name in which DML has occurred into the history table? Will this cause any issues with performance?


Solution

  • Here is one solution using triggers.

    1 - Create a trigger for each table that you want history on.

    2 - Copy the modified data (INS, UPD, DEL) from base table to audit table during the action.

    3 - Store all the data in XML format so that multiple tables can store data in the same audit table.

    I did cover this in one of my blog articles. It is a great solution for auditing small amounts of data. There might be an overhead concern when dealing with thousands of record changes per second.

    Please test before deploying to a production environment!

    Here is the audit table that keeps track of the table name as well as the type of change.

    /*  
        Create data level auditing - table.
    */
    
    -- Remove table if it exists
    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = 
        OBJECT_ID(N'[ADT].[LOG_DML_CHANGES]') AND type in (N'U'))
    DROP TABLE [ADT].[LOG_DML_CHANGES]
    GO
    
    CREATE TABLE [ADT].[LOG_DML_CHANGES]
    (
        [ChangeId]BIGINT IDENTITY(1,1) NOT NULL,
        [ChangeDate] [datetime] NOT NULL,
        [ChangeType] [varchar](20) NOT NULL,
        [ChangeBy] [nvarchar](256) NOT NULL,
        [AppName] [nvarchar](128) NOT NULL,
        [HostName] [nvarchar](128) NOT NULL,
        [SchemaName] [sysname] NOT NULL,
        [ObjectName] [sysname] NOT NULL,
        [XmlRecSet] [xml] NULL,
     CONSTRAINT [pk_Ltc_ChangeId] PRIMARY KEY CLUSTERED ([ChangeId] ASC)
    ) 
    GO
    

    Here is the article.

    http://craftydba.com/?p=2060

    The image below shows a single [LOG_DML_CHANGES] table with multiple [TRG_TRACK_DML_CHGS_XXX] triggers.

    enter image description here