SQL server schema auditing?

前端 未结 4 1997
余生分开走
余生分开走 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:42

    For the second question you can consider triggers as an option. In the following example, information about the event that fired the trigger is captured using the SQL Server’s EVENTDATA() function. The SQL script creates DDL trigger that captures CREATE, ALTER, and DROP events on a database level (although, triggers can be created on the server level to capture events for all databases on the server; ON ALL SERVER option should be used, instead of ON DATABASE)

    CREATE TRIGGER Audit_DDL ON DATABASE
    FOR CREATE_TABLE , ALTER_TABLE , DROP_TABLE
    AS
    DECLARE
    @event xml;
    SET @event = EVENTDATA(
                      );
    INSERT INTO Audit_DDL_Events
    VALUES( REPLACE( CONVERT( varchar( 50
                                 ) , @event.query( 'data(/EVENT_INSTANCE/PostTime)'
                                                 )
                        ) , 'T' , ' '
               ) , 
        CONVERT( varchar( 150
                        ) , @event.query( 'data(/EVENT_INSTANCE/LoginName)'
                                        )
               ) , 
        CONVERT( varchar( 150
                        ) , @event.query( 'data(/EVENT_INSTANCE/UserName)'
                                        )
               ) , 
        CONVERT( varchar( 150
                        ) , @event.query( 'data(/EVENT_INSTANCE/DatabaseName)'
                                        )
               ) , 
        CONVERT( varchar( 150
                        ) , @event.query( 'data(/EVENT_INSTANCE/SchemaName)'
                                        )
               ) , 
        CONVERT( varchar( 150
                        ) , @event.query( 'data(/EVENT_INSTANCE/ObjectName)'
                                        )
               ) , 
        CONVERT( varchar( 150
                        ) , @event.query( 'data(/EVENT_INSTANCE/ObjectType)'
                                        )
               ) , 
        CONVERT( varchar( max
                        ) , @event.query( 'data(/EVENT_INSTANCE/TSQLCommand/CommandText)'
                                        )
               )
      );
    

    An appropriate storage table for the auditing data from EVENTDATA XML must be created also:

    CREATE TABLE Audit_DDL_Events( DDL_Event_Time datetime , 
                               DDL_Login_Name varchar( 150
                                                     ) , 
                               DDL_User_Name varchar( 150
                                                    ) , 
                               DDL_Database_Name varchar( 150
                                                        ) , 
                               DDL_Schema_Name varchar( 150
                                                      ) , 
                               DDL_Object_Name varchar( 150
                                                      ) , 
                               DDL_Object_Type varchar( 150
                                                      ) , 
                               DDL_Command varchar( max
                                                  )
                             );
    

提交回复
热议问题