Scheduling SQL Server Traces - Part 2

  • Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/rsharma/schedulingsqlservertracespart2.asp

  • Nice Thing

    This is similar procedure for starting trace

    that I'm using in daily processing.

    CREATE PROCEDURE sp_startTrace

    AS

    DECLARE @TraceIdOut INT

    DECLARE @FolderTracenvarchar(128)

    DECLARE @FileTraceLikenvarchar(128)

    DECLARE @FileTracenvarchar(128)

    SELECT @FolderTrace = 'C:\FolderTrace\'

    SET @FileTraceLike = @FolderTrace + 'Trace%'

    IF

    (select count(*) from ::fn_trace_getinfo(default)

    where CAST(value AS VARCHAR) like cast(@FileTraceLike AS VARCHAR))

    > 0

    BEGIN

    PRINT 'Already started'

    RETURN

    END

    --------------trace file name TraceYYMMDDHHMMSS

    SET @FileTrace = @FolderTrace + 'Trace' +

    (select convert(CHAR(6),Getdate(), 12)) +

    SUBSTRING((select convert(CHAR(8),Getdate(), 108)),1,2)+

    SUBSTRING((select convert(CHAR(8),Getdate(), 108)),4,2)+

    SUBSTRING((select convert(CHAR(8),Getdate(), 108)),7,2)

    DECLARE @onValue as bit

    SET @onValue = 1

    DECLARE @maxfilesizeValue bigint

    SET @maxfilesizeValue = 1

    exec Master..sp_trace_create @TraceIdOut OUT,

    @Options =2,

    @TraceFile = @FileTrace,

    @maxfilesize = @maxfilesizeValue

    DECLARE Events_Cursor CURSOR FOR

    SELECT 10 As EventID

    UNION SELECT 11

    UNION SELECT 12

    UNION SELECT 13

    UNION SELECT 16

    UNION SELECT 17

    UNION SELECT 21

    UNION SELECT 22

    UNION SELECT 25

    UNION SELECT 33

    UNION SELECT 37

    UNION SELECT 40

    UNION SELECT 41

    UNION SELECT 42

    UNION SELECT 43

    UNION SELECT 53

    UNION SELECT 55

    UNION SELECT 59

    UNION SELECT 61

    UNION SELECT 67

    UNION SELECT 69

    UNION SELECT 74

    UNION SELECT 78

    UNION SELECT 79

    UNION SELECT 80

    UNION SELECT 92

    UNION SELECT 93

    UNION SELECT 100

    OPEN Events_Cursor

    DECLARE @EventID INT

    FETCH NEXT FROM Events_Cursor INTO @EventID

    WHILE @@FETCH_STATUS = 0

    BEGIN

    DECLARE Columns_Cursor CURSOR FOR

    SELECT 1 AS ColID

    UNION SELECT 3

    UNION SELECT 6

    UNION SELECT 8

    UNION SELECT 10

    UNION SELECT 11

    UNION SELECT 12

    UNION SELECT 13

    UNION SELECT 14

    UNION SELECT 15

    UNION SELECT 18

    UNION SELECT 27

    UNION SELECT 40

    OPEN Columns_Cursor

    DECLARE @ColumnID INT

    FETCH NEXT FROM Columns_Cursor INTO @ColumnID

    WHILE @@FETCH_STATUS = 0

    BEGIN

    exec Master..sp_trace_setevent @TraceId = @TraceIdOut,

    @EventID = @EventID ,

    @columnid = @ColumnID ,

    @on =@onValue

    FETCH NEXT FROM Columns_Cursor INTO @ColumnID

    END

    CLOSE Columns_Cursor

    DEALLOCATE Columns_Cursor

    FETCH NEXT FROM Events_Cursor INTO @EventID

    END

    CLOSE Events_Cursor

    DEALLOCATE Events_Cursor

    ----------- Filter

    exec Master..sp_trace_setfilter @traceid = @TraceIdOut,

    @columnid = 3,

    @logical_operator = 0,

    @comparison_operator = 0,

    @value = 15

    exec Master..sp_trace_setfilter @traceid = @TraceIdOut,

    @columnid = 10,

    @logical_operator = 0,

    @comparison_operator = 7,

    @value = N'SQL Profiler'

    exec Master..sp_trace_setstatus@traceid = @TraceIdOut ,

    @status = 1

    print ' Started Trace ' + @FileTrace

    GO

  • Interesting concept - But difficult to read and I have found several problems with the code (On SQL2k sp2 it would not run without some fixes.)

  • The comments are in-between the script itself and are self-explanatory hence I did not elaborate on them in text assuming that the user is familiar with sql and the functions used within the code.

    Also, the last that I tested it was on SS2K + SP3a....don't have SP2 only on a machine to test on so can't comment on what the issue might be on just SP2 (shouldn't be a big deal though since between SP2 and SP3a most of them have been bug fixes and new features like fn_get_sql etc. are not being used in the code). And when you cut and paste the code, make sure that the text is not messed up because of the font size for the comments in the code (it can wrap up and appear on the second line since the comments are marked as --).

    Rahul Sharma

    Senior Database Administrator

    Manhattan Associates

    Atlanta, GA

    Edited by - rsharma on 09/19/2003 11:06:29 AM

    Edited by - rsharma on 09/19/2003 11:11:16 AM

  • Great script! Thanks

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

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