简体   繁体   English

表格审核轨迹

[英]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 . 我想对特定表进行审计跟踪,例如在表中插入,更新,删除的内容,所有这些日志都保存在一个表中,我正在使用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 . 在该表上创建一个after触发器,并将记录插入到日志表中。

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 . 这可以使用Triggers实现。 Trigger will make your DML operations slower, if large Insert, Delete and Update operations are happening on your table. 如果表上发生大量的插入,删除和更新操作,触发器将使DML操作变慢。 If it's small table you can create TRIGGER like below, to log the rows to another table based on the action occurred. 如果是小表,则可以按如下所示创建TRIGGER,以根据发生的操作将行记录到另一个表中。

You can make use of Inserted and Deleted magic tables which hold the rows which are being Inserted and Deleted inside a trigger. 您可以使用Inserted and Deleted魔术表,这些表保存触发器中正在插入和删除的行。

There is another alternate if you need more control over auditing using CDC (Change Data Capture) . 如果您需要使用CDC (更改数据捕获)对审核进行更多控制,则还有另一种选择。

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) . 尝试使用CDC(更改数据捕获) A very helpful tool which will help to manage the Audit Trail 一个非常有用的工具,将有助于管理审计跟踪

Read the article from MSDN 阅读来自MSDN的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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