Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Audit the SQL Server Schema?

$
0
0

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 modify for their own needs. Usually this works out OK for us but one particular team likes to really muck it up and it is impacting everyone else. So 2 questions:

In hindsight I wish we had set up something robust from the outset but we did not, just the default install. It would be nice to be able to see what has been done to the schema so far, even if its as simple as 'User XYZ changed Procedure ABC on 07/12/2012 at 9:00 AM'. Is there anything built into SQL Server and enabled by default that tracks this that we might leverage, and if so where/how? As far as a long term solution goes, what would you recommend for this? I've been reading up on DDL triggers a bit and that seems like a promising option. If you've used this approach can you share a bit with how it worked and what you could do with it?

thank you

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 [Audit].[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 [Audit].[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;

Viewing all articles
Browse latest Browse all 3160

Trending Articles