SQL server schema auditing?

前端 未结 4 1996
余生分开走
余生分开走 2021-01-01 05:33

We have a SQL Server 2008 Enterprise database with two different schemas, a locked one that we maintain and an open one that we allow outside development teams to add to and

4条回答
  •  隐瞒了意图╮
    2021-01-01 05:53

    I've got a system that uses a DDL trigger for exactly this type of thing. It works well enough for my needs. It was originally developed on Sql Server 2005, and now lives on a Sql Server 2008R2 system. It's similar to the one described by the link in Aaron Bertrand's comment.

    Create a table similar to this one.

    CREATE TABLE [dbo].[SchemaLog](
        [SchemaLogID] [int] IDENTITY(1,1) NOT NULL,
        [PostTimeUtc] [datetime] NOT NULL,
        [DatabaseUser] [nvarchar](128) NOT NULL,
        [Event] [nvarchar](128) NOT NULL,
        [Schema] [nvarchar](128) NULL,
        [Object] [nvarchar](128) NULL,
        [TSQL] [nvarchar](max) NOT NULL,
        [XmlEvent] [xml] NOT NULL,
     CONSTRAINT [PK_SchemaLog_1] PRIMARY KEY CLUSTERED 
    (
        [SchemaLogID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    

    Make sure everyone has insert permissions on the table then create a ddl trigger similar to this.

    CREATE TRIGGER [ddlDatabaseTriggerLog] ON DATABASE  FOR DDL_DATABASE_LEVEL_EVENTS AS  
    BEGIN     
        SET NOCOUNT ON;     
        DECLARE @data XML;     
        DECLARE @schema sysname;     
        DECLARE @object sysname;     
        DECLARE @eventType sysname;     
        SET @data = EVENTDATA();     
        SET @eventType = @data.value('(/EVENT_INSTANCE/EventType)[1]', 'sysname');     
        SET @schema = @data.value('(/EVENT_INSTANCE/SchemaName)[1]', 'sysname');     
        SET @object = @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'sysname')      
        IF @object IS NOT NULL         
            PRINT '  ' + @eventType + ' - ' + @schema + '.' + @object;     
        ELSE         
            PRINT '  ' + @eventType + ' - ' + @schema;     
    
        IF @eventType IS NULL         
            PRINT CONVERT(nvarchar(max), @data);     
    
        INSERT [dbo].[SchemaLog]          (         
            [PostTimeUtc]
        ,          [DatabaseUser]
        ,          [Event]
        ,          [Schema]
        ,          [Object]
        ,          [TSQL]
        ,          [XmlEvent]         )      
        VALUES          (         
            GETUTCDATE()
        ,          CONVERT(sysname, CURRENT_USER)
        ,          @eventType
        ,          CONVERT(sysname, @schema)
        ,          CONVERT(sysname, @object)
        ,          @data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(max)')
        ,          @data         ); 
    
    END;
    

    View to select changes in order

    create view SchemaLogOrdered
    as
    SELECT top 10000 *
    FROM            dbo.SchemaLog
    ORDER BY  SchemaLogID DESC
    

提交回复
热议问题