简体   繁体   中英

AUDIT TRAIL OF TABLES

I want to do audit trail on specific table like what inserted,updated,deleted in table and all this logs are save in one table I am using sql server 2012 . Can any one please help me with how to achieve this?

Please note - Use of cursor is restricted

create an after trigger on that table and insert the records into the log table .

create trigger <trigger_name> after insert/update/delete/ on 
table <orig table>
  begin
     insert into the log tables ('all the fields that you require');
  end 

This can be achieve using Triggers . Trigger will make your DML operations slower, if large Insert, Delete and Update operations are happening on your table. If it's small table you can create TRIGGER like below, to log the rows to another table based on the action occurred.

You can make use of Inserted and Deleted magic tables which hold the rows which are being Inserted and Deleted inside a trigger.

There is another alternate if you need more control over auditing using CDC (Change Data Capture) .

CREATE TABLE TrailTable
(
    Id INT,
    Name VARCHAR(100)
);

CREATE TABLE TrailTableLog
(
    Id INT,
    Name VARCHAR(100),
    Action CHAR(3)
);


Insert Into TrailTable VALUES (1,'Vi');
Insert Into TrailTable VALUES (2,'Vr');
Insert Into TrailTable VALUES (3,'An');
Insert Into TrailTable VALUES (4,'Ma');

CREATE TRIGGER dbo.TRG_IDU_TrailTable
ON dbo.TrailTable
AFTER INSERT, UPDATE, DELETE
AS 
BEGIN
    SET NOCOUNT ON;

    DECLARE @Action as char(1);
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM INSERTED)
                         AND EXISTS(SELECT * FROM DELETED)
                        THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM INSERTED)
                        THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM DELETED)
                        THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)

    IF(@Action = 'I')
    BEGIN
        INSERT INTO TRG_IDU_TrailTable (Id, Name, Action)
        SELECT Id, Name, 'I' FROM INSERTED;
    END

    IF(@Action = 'D')
    BEGIN
        INSERT INTO TRG_IDU_TrailTable (Id, Name, Action)
        SELECT Id, Name, 'D' FROM DELETED;
    END

    IF(@Action = 'U')
    BEGIN

        INSERT INTO TRG_IDU_TrailTable (Id, Name, Action)
        SELECT Id, Name, 'U-D' FROM INSERTED; -- Records Deleted to Update

        INSERT INTO TRG_IDU_TrailTable (Id, Name, Action)
        SELECT Id, Name, 'U-I' FROM INSERTED; --Records Inserted to Update
    END

END

Try using CDC (Change Data Capture) . A very helpful tool which will help to manage the Audit Trail

Read the article from MSDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM