Track sql code on servers

  • What is the best way to capture all of the code executed on my sql servers which i want to save atleast for a month.

    Profiler may serve the purpose but i want to save them for reference ansd also running a profiler all the time may not be a good idea for performance perspective.

    I do have Quest Spotlight, will that help me?

  • a server side trace, rather than running profiler, is actually very light weight and has minimal impact.

    i personally use something like this, but it would rollover automatically based on size, and not based on say, 30 days worth like you are trying to do.:

    this is capturing event SQL:BatchCompleted, which is all the statments sent to teh server, and is also capturing some performance info so you can see what is running slow/taking a long duration.

    note at the end it is creating a view to make it easy to review the trace as well.

    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!

  • Sorry for the newbie trace question...

    Looks like your Stored Proc "sp_AddMyTrace" starts the trace, but how do you know the traceid to stop it ?

    What do you consider long duration ?

  • that's a good question.

    do a select * from sys.traces

    the first column, "id", is the traceid.

    with that value, you could stop and then delete the trace:

    assuming the traceid is 2, since 1 is the default 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

    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!

  • Don't you have to add RPC:Completed also to catch everything?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 5 posts - 1 through 4 (of 4 total)

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