Best way to implement an audit trail in SQL Server?

后端 未结 7 2268
臣服心动
臣服心动 2020-11-27 03:23

I don\'t know if these requirements are standard or not but I\'m wondering is there a solution out there which can do the following:

  • For a specified set of tab
相关标签:
7条回答
  • 2020-11-27 03:50

    There are many ways to do that; it depends which version of SQL Server you are using.

    Here are few

    • Audit trail with shadow table and trigger Here is the link

    • Also you can consider to use SQL Server 2008 Audit feature Here is the link

    0 讨论(0)
  • 2020-11-27 03:51

    You can try out a 3rd party point-and-click trigger based solution such as ApexSQL Audit - an auditing tool for SQL Server databases, which captures data changes that have occurred on a database including the information on who made the change, which objects were affected, when it was made, as well as the information on the SQL login, application and host used to make the change. It stores all captured information in a central repository and exports them in print friendly formats

    Disclaimer: I work as a Product Support Engineer at ApexSQL

    0 讨论(0)
  • 2020-11-27 03:59

    Take a look at this article - Auditing in SQL Server 2008 which beautifully takes advantage of the auditing features already present in SQL Server 2008.

    I must also mention that @Microtechie answers points to some great article. Read them and decide which one is more easy to adapt.

    0 讨论(0)
  • 2020-11-27 04:02

    I create trigger which does it for XML this way we can log all tables to same table, making it more flexible

    CREATE TABLE [dbo].[AuditAll] (
        AuditId    int           NOT NULL IDENTITY(1,1),
        [DateTime] datetime      NOT NULL,
        TableName  nvarchar(255) NOT NULL,
        AuditEntry xml           NULL,
    
        CONSTRAINT [PK_AuditAll] PRIMARY KEY CLUSTERED ( AuditId ASC )
    )
    

    I needed only 'old' values, so I store deleted table only, inserted table can be seen in the table anyhow.

    CREATE TRIGGER AuditSimple 
        ON Simple
        AFTER INSERT,DELETE,UPDATE
    AS 
    BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    
    IF (SELECT COUNT(*) FROM deleted) > 0 
    begin
        Declare @AuditMessage XML
        --set valut to all xml from deleted table
        set @AuditMessage = (select * from deleted for xml auto) 
    
        insert into AuditAll( DateTime, TableName, AuditEntry ) 
            values ( GetDate(), 'Simple', @AuditMessage )
    end
    
    END
    GO
    

    I guess this could easily be called in sp_foreach to create it for each table in datatabase but we did not needed it at the moment, just remember to change your table names

    cheers

    0 讨论(0)
  • 2020-11-27 04:04

    With Database Snapshots, you can keep a readonly copy of your data in that instant. Also with your backups of the logs you can restore your information at a specific period of time if necessary.

    You can also read information from the log to retrieve the information changed.

    The other solution that is not of your preference is to trace the changes using triggers, but it may require to work on each table. You can also enable the Change Data Capture feature to detect changes, this feature also needs to be enable for each table, but it requires less code than the triggers.

    Finally, there are third party tools like Apex SQL Trigger that do this job automatically with few clicks and configurations.

    0 讨论(0)
  • 2020-11-27 04:13

    I see 3 ways to achieve this:

    1. Triggers are the best solution at all.
    2. You can implement replication/log shipping for the table or the database which will always be a few mili seconds/seconds old copy of that table/database.
    3. Schedule differential backups as per the time duration old copy you need.

    With option '2', in case of any problem immediately you can turn off replication/log shipping and get a few seconds previous copy of exact data. With option '3', for example you have differential backup frequency of every 5 minutes, then in case of any problem you can recover 5 minutes old copy of correct data.

    0 讨论(0)
提交回复
热议问题