Best way for DML and DDL Audit

  • Hi,

    what is the best way to put DML and DDL audit on sql server 2005 Enterprise, basically want to Audit who is performing DML and DDL on my database ?

    Thank you!!

  • server side traces using sp_trace_create are low impact and a good way to track recent changes.

    when you create a trace, there is an expectation that you only keep X number of MB of for a history of changes.

    The default trace is a great tool for DDL changes, Create/Alter/Delete of objects.

    creating a different trace for DML changes is easy to do.

    if you need SOX level auditing, I'll hope someone with more eperience than me has an answer.

    here's my proc I use for creating a DML trace,and a view to look at the trace results as well.

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    CREATE procedure [dbo].[sp_AddMyTrace]

    AS

    BEGIN

    SET NOCOUNT ON

    SET XACT_ABORT ON

    BEGIN TRAN

    declare @sql varchar(1000)

    declare @path nvarchar(256)

    declare @traceidout int

    declare @maxfilesize bigint

    declare @maxRolloverFiles int

    declare @on bit

    set @on = 1

    set @maxRolloverFiles = 2

    set @maxfilesize = 50

    --we want the current trace folder

    --ie c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG

    SELECT @path = REVERSE(SUBSTRING(REVERSE(path),CHARINDEX('\',REVERSE(path)),256)) FROM sys.traces WHERE is_default = 1

    SET @path = @path + N'MyDMLtrace' --system appends .trc automatically for the filename

    --create the trace

    exec sp_trace_create @traceidout output, @maxRolloverFiles, @path, @maxfilesize, NULL

    --for the Event Every SQL statement completed, capture all 64 columns of accessible data

    exec sp_trace_setevent @traceidout, 12, 1, @on --SQL:BatchCompleted,TextData

    exec sp_trace_setevent @traceidout, 12, 2, @on --SQL:BatchCompleted,BinaryData

    exec sp_trace_setevent @traceidout, 12, 3, @on --SQL:BatchCompleted,DatabaseID

    exec sp_trace_setevent @traceidout, 12, 4, @on --SQL:BatchCompleted,TransactionID

    exec sp_trace_setevent @traceidout, 12, 5, @on --SQL:BatchCompleted,LineNumber

    exec sp_trace_setevent @traceidout, 12, 6, @on --SQL:BatchCompleted,NTUserName

    exec sp_trace_setevent @traceidout, 12, 7, @on --SQL:BatchCompleted,NTDomainName

    exec sp_trace_setevent @traceidout, 12, 8, @on --SQL:BatchCompleted,HostName

    exec sp_trace_setevent @traceidout, 12, 9, @on --SQL:BatchCompleted, ClientProcessID

    exec sp_trace_setevent @traceidout, 12,10, @on --SQL:BatchCompleted,ApplicationName

    exec sp_trace_setevent @traceidout, 12,11, @on --SQL:BatchCompleted,LoginName

    exec sp_trace_setevent @traceidout, 12,12, @on --SQL:BatchCompleted,SPID

    exec sp_trace_setevent @traceidout, 12,13, @on --SQL:BatchCompleted,Duration

    exec sp_trace_setevent @traceidout, 12,14, @on --SQL:BatchCompleted,StartTime

    exec sp_trace_setevent @traceidout, 12,15, @on --SQL:BatchCompleted,EndTime

    exec sp_trace_setevent @traceidout, 12,16, @on --SQL:BatchCompleted,Reads

    exec sp_trace_setevent @traceidout, 12,17, @on --SQL:BatchCompleted,Writes

    exec sp_trace_setevent @traceidout, 12,18, @on --SQL:BatchCompleted,CPU

    exec sp_trace_setevent @traceidout, 12,19, @on --SQL:BatchCompleted,Permissions

    exec sp_trace_setevent @traceidout, 12,20, @on --SQL:BatchCompleted,Severity

    exec sp_trace_setevent @traceidout, 12,21, @on --SQL:BatchCompleted,EventSubClass

    exec sp_trace_setevent @traceidout, 12,22, @on --SQL:BatchCompleted,ObjectID

    exec sp_trace_setevent @traceidout, 12,23, @on --SQL:BatchCompleted,Success

    exec sp_trace_setevent @traceidout, 12,24, @on --SQL:BatchCompleted,IndexID

    exec sp_trace_setevent @traceidout, 12,25, @on --SQL:BatchCompleted,IntegerData

    exec sp_trace_setevent @traceidout, 12,26, @on --SQL:BatchCompleted,ServerName

    exec sp_trace_setevent @traceidout, 12,27, @on --SQL:BatchCompleted,EventClass

    exec sp_trace_setevent @traceidout, 12,28, @on --SQL:BatchCompleted,ObjectType

    exec sp_trace_setevent @traceidout, 12,29, @on --SQL:BatchCompleted,NestLevel

    exec sp_trace_setevent @traceidout, 12,30, @on --SQL:BatchCompleted,State

    exec sp_trace_setevent @traceidout, 12,31, @on --SQL:BatchCompleted,Error

    exec sp_trace_setevent @traceidout, 12,32, @on --SQL:BatchCompleted,Mode

    exec sp_trace_setevent @traceidout, 12,33, @on --SQL:BatchCompleted,Handle

    exec sp_trace_setevent @traceidout, 12,34, @on --SQL:BatchCompleted,ObjectName

    exec sp_trace_setevent @traceidout, 12,35, @on --SQL:BatchCompleted,DatabaseName

    exec sp_trace_setevent @traceidout, 12,36, @on --SQL:BatchCompleted,FileName

    exec sp_trace_setevent @traceidout, 12,37, @on --SQL:BatchCompleted,OwnerName

    exec sp_trace_setevent @traceidout, 12,38, @on --SQL:BatchCompleted,RoleName

    exec sp_trace_setevent @traceidout, 12,39, @on --SQL:BatchCompleted,TargetUserName

    exec sp_trace_setevent @traceidout, 12,40, @on --SQL:BatchCompleted,DBUserName

    exec sp_trace_setevent @traceidout, 12,41, @on --SQL:BatchCompleted,LoginSid

    exec sp_trace_setevent @traceidout, 12,42, @on --SQL:BatchCompleted,TargetLoginName

    exec sp_trace_setevent @traceidout, 12,43, @on --SQL:BatchCompleted,TargetLoginSid

    exec sp_trace_setevent @traceidout, 12,44, @on --SQL:BatchCompleted,ColumnPermissions

    exec sp_trace_setevent @traceidout, 12,45, @on --SQL:BatchCompleted,LinkedServerName

    exec sp_trace_setevent @traceidout, 12,46, @on --SQL:BatchCompleted,ProviderName

    exec sp_trace_setevent @traceidout, 12,47, @on --SQL:BatchCompleted,MethodName

    exec sp_trace_setevent @traceidout, 12,48, @on --SQL:BatchCompleted,RowCounts

    exec sp_trace_setevent @traceidout, 12,49, @on --SQL:BatchCompleted,RequestID

    exec sp_trace_setevent @traceidout, 12,50, @on --SQL:BatchCompleted,XactSequence

    exec sp_trace_setevent @traceidout, 12,51, @on --SQL:BatchCompleted,EventSequence

    exec sp_trace_setevent @traceidout, 12,52, @on --SQL:BatchCompleted,BigintData1

    exec sp_trace_setevent @traceidout, 12,53, @on --SQL:BatchCompleted,BigintData2

    exec sp_trace_setevent @traceidout, 12,54, @on --SQL:BatchCompleted,GUID

    exec sp_trace_setevent @traceidout, 12,55, @on --SQL:BatchCompleted,IntegerData2

    exec sp_trace_setevent @traceidout, 12,56, @on --SQL:BatchCompleted,ObjectID2

    exec sp_trace_setevent @traceidout, 12,57, @on --SQL:BatchCompleted,Type

    exec sp_trace_setevent @traceidout, 12,58, @on --SQL:BatchCompleted,OwnerID

    exec sp_trace_setevent @traceidout, 12,59, @on --SQL:BatchCompleted,ParentName

    exec sp_trace_setevent @traceidout, 12,60, @on --SQL:BatchCompleted,IsSystem

    exec sp_trace_setevent @traceidout, 12,61, @on --SQL:BatchCompleted,Offset

    exec sp_trace_setevent @traceidout, 12,62, @on --SQL:BatchCompleted,SourceDatabaseID

    exec sp_trace_setevent @traceidout, 12,63, @on --SQL:BatchCompleted,SqlHandle

    exec sp_trace_setevent @traceidout, 12,64, @on --SQL:BatchCompleted,SessionLoginName

    --turn on the trace

    exec sp_trace_setstatus @traceidout, 1 ---start trace

    --exec sp_trace_setstatus 2, 0 ---stop trace, you must know the traceid to stop it

    --exec sp_trace_setstatus 2, 2 ---close trace you must know the traceid to delete it

    IF EXISTS(select * from master.dbo.sysobjects where xtype='V' and name='sp_DMLTrace')

    BEGIN

    SET @sql = 'ALTER VIEW sp_DMLTrace AS SELECT * FROM ::fn_trace_gettable(''' + @path +'.trc'', default)'

    exec(@sql)

    END

    ELSE

    BEGIN

    SET @sql = 'CREATE VIEW sp_DMLTrace AS SELECT * FROM ::fn_trace_gettable(''' + @path + '.trc'', default)'

    exec(@sql)

    END

    COMMIT TRAN

    END

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I would create a server-side trace that filters on the database that I am concerned with.

    Lowell won, with a script example as well.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply