zoukankan      html  css  js  c++  java
  • Trace-导出已有的服务器端跟踪

    跟踪(Trace)常被我们用来检查性能问题。通常我们会有针对CPU、Duration、Reads的创建跟踪定义,这一类的脚本一般不会包含太多的事件和列,筛选条件也相对简单。假如某一天你使用GUI定义了一个包含大量事件和列的服务器端跟踪,点击运行后你直接把GUI关掉,而你却没有导出原始定义脚本。此时你想知道定义的筛选条件,或者定义的事件和列,甚至是希望另外创建一个相同的跟踪(你肯定不希望再次打开GUI设置繁琐的事件和列及筛选条件)。其实只要跟踪定义在SQL Server内部创建之后,你可以通过查询多个跟踪目录视图得到运行中的服务器端跟踪定义。
    首先我们用Profiler创建一个跟踪,常规属性如下所示:

    选择事件和列:

    设置过滤条件:

    事件选择设置好后,点击运行,此时Profiler如下所示:

    查看sys.traces跟踪目录视图:

    TraceID=2的是行集提供者跟踪,TraceID=3的是文件提供者跟踪
    我们将行集提供者跟踪停止并移除:

    --停止跟踪
    exec sp_trace_setstatus 2, 0
    --移除跟踪
    exec sp_trace_setstatus 2, 2

    移除后Profiler如下所示:

    此时sys.traces跟踪目录视图只包含两条记录:

    注意此时写入文件系统上的E:PerfmonStandardTrace.trc对应的服务器端跟踪还是在运行的。此时我们可以通过 文件|属性 来看看跟踪的定义,还可以通过 文件|导出|编写跟踪定义的脚本,并选择目标导出跟踪定义:

    我们先导出跟踪定义StandardTrace.sql(后期对比),然后将Profiter关闭。
    用下面的脚本提取服务器端跟踪定义:

    -- 传入需要查看的TraceID
    DECLARE @TraceID int
    SET     @TraceID = 3
    
    SELECT 
    '/************************************************************
     * 功能说明:查询跟踪目录视图得到运行中的服务器端跟踪定义
     * 注意事项:如果要运行提取出的跟踪定义,请注意调整筛选条件部分参数赋值的位置
     ************************************************************/
    '
    UNION ALL
    SELECT 
    '-- Create a Queue
    DECLARE  @rc             int
            ,@TraceID        int
            ,@MaxFileSize    bigint=50
            ,@DateTime       datetime=dateadd(day,1,getdate())
    
    '
    UNION ALL
    SELECT  'SET @DateTime = '''+ convert(varchar,stop_time,121)+''''
    FROM    sys.traces
    WHERE   ID = @TraceID AND stop_time IS NOT NULL
    UNION ALL
    SELECT  'SET @MaxFileSize = '+ CAST(max_size as varchar(20))
    FROM    sys.traces
    WHERE   ID = @TraceID AND max_size IS NOT NULL
    UNION ALL
    select  'EXEC @rc = sp_trace_create @TraceID output ,'
            +
            CASE    WHEN [path] like 
    '\?C:Program FilesMicrosoft SQL ServerMSSQL__.MSSQLSERVERMSSQLDATAlackbox%'
                    THEN '@options = 8'
            ELSE
                    CASE    WHEN is_shutdown = 1
                            AND  is_rollover = 1    THEN '6'
                            WHEN is_shutdown = 1    THEN '4'
                            WHEN is_rollover = 1    THEN '2'
                            ELSE '0'
                     END + ',N'''
                         --+ REVERSE(SUBSTRING(SUBSTRING(REVERSE([path]),5,LEN([path]))
                         -- ,CHARINDEX('_',SUBSTRING(REVERSE([path]),5,LEN([path])),1)+1
                         -- ,LEN([path])))
                         + CASE WHEN [path] IS NULL THEN 'InsertFileNameHere' 
                           ELSE LEFT([path],LEN([path])-4)+CONVERT(VARCHAR,GETDATE(),112)+REPLACE(CONVERT(VARCHAR,GETDATE(),108),':','')
                           END
                         +''', @MaxFileSize, @DateTime'
                         --+','+ coalesce(''''+convert(varchar(20),stop_time,126)+'''','NULL')
                         + CASE WHEN is_rollover = 1 
                                THEN ','+cast(max_files as varchar(20))
                                ELSE ''
                           END   
            END
    from    sys.traces
    WHERE   ID = @TraceID
    UNION ALL
    SELECT 'if (@rc != 0) goto error
    
    -- Set the events
    DECLARE @on bit = 1
    '
    UNION ALL
    SELECT  CASE WHEN [path] like 
    '\?C:Program FilesMicrosoft SQL ServerMSSQL%.MSSQLSERVERMSSQLDATAlackbox%'
            THEN ''
            ELSE
            'EXEC @rc = sp_trace_setevent @TraceID' 
            +', '+CAST(EventID as varchar(20))
            +', '+CAST(columnid as varchar(20))
            +', @on'
            END
    FROM    ::fn_trace_geteventinfo(@TraceID) evi
    CROSS APPLY   sys.traces trc
    WHERE   trc.id = @TraceID
    UNION ALL
    SELECT '
    -- Set the Filters
    DECLARE  @Intfilter      int
            ,@BigIntfilter   bigint
    -- 请将参数的赋值放置到对应筛选表达式的前面(如果有的话)
    '
    UNION ALL
    SELECT  CASE   WHEN col.type_name = 'int'
                   THEN  'SET @IntFilter = ' 
                         +CAST(CAST(value as int)AS varchar(20))
                         +'--'+CAST(columnid AS varchar(20))+','+CAST(logical_operator AS varchar(20))+','+CAST(comparison_operator AS varchar(20))
                   WHEN   col.type_name = 'bigint'
                   THEN  'SET @BigIntFilter = ' 
                         +CAST(CAST(value as bigint)AS varchar(20))
                         +'--'+CAST(columnid AS varchar(20))+','+CAST(logical_operator AS varchar(20))+','+CAST(comparison_operator AS varchar(20))
                   ELSE  ''
            END 
    FROM    ::fn_trace_getfilterinfo(@TraceID) fil
    JOIN    sys.trace_columns col
    ON      fil.columnid = col.trace_column_id
    WHERE CHARINDEX('int',col.type_name)>0 and fil.value is not null
    UNION ALL   
    SELECT  'EXEC @rc = sp_trace_setfilter @TraceID'
            +', '+CAST(columnid as varchar(20))
            +', '+CAST(logical_operator as varchar(20))
            +', '+CAST(comparison_operator as varchar(20))
            +', '+CASE   WHEN value IS NULL THEN 'null'
                        WHEN col.type_name = 'int' THEN '@IntFilter'
                        WHEN col.type_name = 'bigint' THEN  '@BigIntFilter'
                        ELSE  'N'''+CAST(value as varchar(8000))+''''
                        END 
    FROM    ::fn_trace_getfilterinfo(@TraceID) fil
    JOIN    sys.trace_columns col
    ON      fil.columnid = col.trace_column_id   
    UNION ALL
    SELECT  
    '
    -- Set the trace status to start
    exec sp_trace_setstatus @TraceID, 1
    
    -- Display trace id for future references
    select TraceID=@TraceID
    goto finish
    '
    UNION ALL
    SELECT
    '
    error: 
    select ErrorCode=@rc
    
    finish:
    go
    '
    View Code

    在我的机器上服务器端跟踪ID为3,因此在脚本的开始设置@TraceID = 3,按Ctrl+T再运行脚本,将结果复制粘贴到新建查询窗口,并删除首行虚线及末行影响行数,得到脚本如下:

     1 /************************************************************
     2  * 功能说明:查询跟踪目录视图得到运行中的服务器端跟踪定义
     3  * 注意事项:如果要运行提取出的跟踪定义,请注意调整筛选条件部分参数赋值的位置
     4  ************************************************************/
     5 -- Create a Queue
     6 DECLARE  @rc             int
     7         ,@TraceID        int
     8         ,@MaxFileSize    bigint
     9         ,@DateTime       datetime
    10 
    11 SET @DateTime = '2015-12-05 21:25:30.533'
    12 SET @MaxFileSize = 17
    13 EXEC @rc = sp_trace_create @TraceID output ,2,N'E:PerfmonStandardTrace20151205151034', @MaxFileSize, @DateTime,0
    14 if (@rc != 0) goto error
    15 
    16 -- Set the events
    17 DECLARE @on bit = 1
    18 EXEC @rc = sp_trace_setevent @TraceID,10,1, @on
    19 EXEC @rc = sp_trace_setevent @TraceID,10,8, @on
    20 EXEC @rc = sp_trace_setevent @TraceID,10,9, @on
    21 EXEC @rc = sp_trace_setevent @TraceID,10,10, @on
    22 EXEC @rc = sp_trace_setevent @TraceID,10,11, @on
    23 EXEC @rc = sp_trace_setevent @TraceID,10,12, @on
    24 EXEC @rc = sp_trace_setevent @TraceID,10,13, @on
    25 EXEC @rc = sp_trace_setevent @TraceID,10,14, @on
    26 EXEC @rc = sp_trace_setevent @TraceID,10,15, @on
    27 EXEC @rc = sp_trace_setevent @TraceID,10,16, @on
    28 EXEC @rc = sp_trace_setevent @TraceID,10,17, @on
    29 EXEC @rc = sp_trace_setevent @TraceID,10,18, @on
    30 EXEC @rc = sp_trace_setevent @TraceID,10,31, @on
    31 EXEC @rc = sp_trace_setevent @TraceID,10,35, @on
    32 EXEC @rc = sp_trace_setevent @TraceID,12,1, @on
    33 EXEC @rc = sp_trace_setevent @TraceID,12,8, @on
    34 EXEC @rc = sp_trace_setevent @TraceID,12,9, @on
    35 EXEC @rc = sp_trace_setevent @TraceID,12,10, @on
    36 EXEC @rc = sp_trace_setevent @TraceID,12,11, @on
    37 EXEC @rc = sp_trace_setevent @TraceID,12,12, @on
    38 EXEC @rc = sp_trace_setevent @TraceID,12,13, @on
    39 EXEC @rc = sp_trace_setevent @TraceID,12,14, @on
    40 EXEC @rc = sp_trace_setevent @TraceID,12,15, @on
    41 EXEC @rc = sp_trace_setevent @TraceID,12,16, @on
    42 EXEC @rc = sp_trace_setevent @TraceID,12,17, @on
    43 EXEC @rc = sp_trace_setevent @TraceID,12,18, @on
    44 EXEC @rc = sp_trace_setevent @TraceID,12,31, @on
    45 EXEC @rc = sp_trace_setevent @TraceID,12,35, @on
    46 EXEC @rc = sp_trace_setevent @TraceID,13,1, @on
    47 EXEC @rc = sp_trace_setevent @TraceID,13,8, @on
    48 EXEC @rc = sp_trace_setevent @TraceID,13,9, @on
    49 EXEC @rc = sp_trace_setevent @TraceID,13,10, @on
    50 EXEC @rc = sp_trace_setevent @TraceID,13,11, @on
    51 EXEC @rc = sp_trace_setevent @TraceID,13,12, @on
    52 EXEC @rc = sp_trace_setevent @TraceID,13,14, @on
    53 EXEC @rc = sp_trace_setevent @TraceID,13,35, @on
    54 
    55 -- Set the Filters
    56 DECLARE  @Intfilter      int
    57         ,@BigIntfilter   bigint
    58 -- 请将参数的赋值放置到对应筛选表达式的前面(如果有的话)
    59 SET @BigIntFilter = 2000000--13,0,4
    60 SET @BigIntFilter = 5000000--13,0,5
    61 SET @IntFilter = 33--18,0,4
    62 EXEC @rc = sp_trace_setfilter @TraceID,1,0,7,N'%DBA%'
    63 EXEC @rc = sp_trace_setfilter @TraceID,1,0,1,null
    64 EXEC @rc = sp_trace_setfilter @TraceID,10,0,7,N'SQL Server Profiler - b42ea627-5083-476a-95d1-664e5e74731e'
    65 EXEC @rc = sp_trace_setfilter @TraceID,11,0,6,N'%Debug%'
    66 EXEC @rc = sp_trace_setfilter @TraceID,11,1,6,N'%Client%'
    67 EXEC @rc = sp_trace_setfilter @TraceID,11,0,1,null
    68 EXEC @rc = sp_trace_setfilter @TraceID,13,0,4,@BigIntFilter
    69 EXEC @rc = sp_trace_setfilter @TraceID,13,0,5,@BigIntFilter
    70 EXEC @rc = sp_trace_setfilter @TraceID,13,0,1,null
    71 EXEC @rc = sp_trace_setfilter @TraceID,18,0,4,@IntFilter
    72 EXEC @rc = sp_trace_setfilter @TraceID,18,0,1,null
    73 
    74 -- Set the trace status to start
    75 exec sp_trace_setstatus @TraceID, 1
    76 
    77 -- Display trace id for future references
    78 select TraceID=@TraceID
    79 goto finish
    80 
    81 error: 
    82 select ErrorCode=@rc
    83 
    84 finish:
    85 go
    View Code

    如果发现注释的中文是乱码,请检查当前数据库的排序规则,或者用[N'中文']。此时的脚本还不能直接运行,我们需要将图中红框中的内容作适当调整:

    调整为如下所示:

    调整后最终的跟踪定义脚本如下:

     1 /************************************************************
     2  * 功能说明:查询跟踪目录视图得到运行中的服务器端跟踪定义
     3  * 注意事项:如果要运行提取出的跟踪定义,请注意调整筛选条件部分参数赋值的位置
     4  ************************************************************/
     5 -- Create a Queue
     6 DECLARE  @rc             int
     7         ,@TraceID        int
     8         ,@MaxFileSize    bigint
     9         ,@DateTime       datetime
    10 
    11 SET @DateTime = '2015-12-05 21:25:30.533'
    12 SET @MaxFileSize = 17
    13 EXEC @rc = sp_trace_create @TraceID output ,2,N'E:PerfmonStandardTrace20151205151034', @MaxFileSize, @DateTime,0
    14 if (@rc != 0) goto error
    15 
    16 -- Set the events
    17 DECLARE @on bit = 1
    18 EXEC @rc = sp_trace_setevent @TraceID,10,1, @on
    19 EXEC @rc = sp_trace_setevent @TraceID,10,8, @on
    20 EXEC @rc = sp_trace_setevent @TraceID,10,9, @on
    21 EXEC @rc = sp_trace_setevent @TraceID,10,10, @on
    22 EXEC @rc = sp_trace_setevent @TraceID,10,11, @on
    23 EXEC @rc = sp_trace_setevent @TraceID,10,12, @on
    24 EXEC @rc = sp_trace_setevent @TraceID,10,13, @on
    25 EXEC @rc = sp_trace_setevent @TraceID,10,14, @on
    26 EXEC @rc = sp_trace_setevent @TraceID,10,15, @on
    27 EXEC @rc = sp_trace_setevent @TraceID,10,16, @on
    28 EXEC @rc = sp_trace_setevent @TraceID,10,17, @on
    29 EXEC @rc = sp_trace_setevent @TraceID,10,18, @on
    30 EXEC @rc = sp_trace_setevent @TraceID,10,31, @on
    31 EXEC @rc = sp_trace_setevent @TraceID,10,35, @on
    32 EXEC @rc = sp_trace_setevent @TraceID,12,1, @on
    33 EXEC @rc = sp_trace_setevent @TraceID,12,8, @on
    34 EXEC @rc = sp_trace_setevent @TraceID,12,9, @on
    35 EXEC @rc = sp_trace_setevent @TraceID,12,10, @on
    36 EXEC @rc = sp_trace_setevent @TraceID,12,11, @on
    37 EXEC @rc = sp_trace_setevent @TraceID,12,12, @on
    38 EXEC @rc = sp_trace_setevent @TraceID,12,13, @on
    39 EXEC @rc = sp_trace_setevent @TraceID,12,14, @on
    40 EXEC @rc = sp_trace_setevent @TraceID,12,15, @on
    41 EXEC @rc = sp_trace_setevent @TraceID,12,16, @on
    42 EXEC @rc = sp_trace_setevent @TraceID,12,17, @on
    43 EXEC @rc = sp_trace_setevent @TraceID,12,18, @on
    44 EXEC @rc = sp_trace_setevent @TraceID,12,31, @on
    45 EXEC @rc = sp_trace_setevent @TraceID,12,35, @on
    46 EXEC @rc = sp_trace_setevent @TraceID,13,1, @on
    47 EXEC @rc = sp_trace_setevent @TraceID,13,8, @on
    48 EXEC @rc = sp_trace_setevent @TraceID,13,9, @on
    49 EXEC @rc = sp_trace_setevent @TraceID,13,10, @on
    50 EXEC @rc = sp_trace_setevent @TraceID,13,11, @on
    51 EXEC @rc = sp_trace_setevent @TraceID,13,12, @on
    52 EXEC @rc = sp_trace_setevent @TraceID,13,14, @on
    53 EXEC @rc = sp_trace_setevent @TraceID,13,35, @on
    54 
    55 -- Set the Filters
    56 DECLARE  @Intfilter      int
    57         ,@BigIntfilter   bigint
    58 -- 请将参数的赋值放置到对应筛选表达式的前面(如果有的话)
    59 EXEC @rc = sp_trace_setfilter @TraceID,1,0,7,N'%DBA%'
    60 EXEC @rc = sp_trace_setfilter @TraceID,1,0,1,null
    61 EXEC @rc = sp_trace_setfilter @TraceID,10,0,7,N'SQL Server Profiler - b42ea627-5083-476a-95d1-664e5e74731e'
    62 EXEC @rc = sp_trace_setfilter @TraceID,11,0,6,N'%Debug%'
    63 EXEC @rc = sp_trace_setfilter @TraceID,11,1,6,N'%Client%'
    64 EXEC @rc = sp_trace_setfilter @TraceID,11,0,1,null
    65 SET @BigIntFilter = 2000000--13,0,4
    66 EXEC @rc = sp_trace_setfilter @TraceID,13,0,4,@BigIntFilter
    67 SET @BigIntFilter = 5000000--13,0,5
    68 EXEC @rc = sp_trace_setfilter @TraceID,13,0,5,@BigIntFilter
    69 EXEC @rc = sp_trace_setfilter @TraceID,13,0,1,null
    70 SET @IntFilter = 33--18,0,4
    71 EXEC @rc = sp_trace_setfilter @TraceID,18,0,4,@IntFilter
    72 EXEC @rc = sp_trace_setfilter @TraceID,18,0,1,null
    73 
    74 -- Set the trace status to start
    75 exec sp_trace_setstatus @TraceID, 1
    76 
    77 -- Display trace id for future references
    78 select TraceID=@TraceID
    79 goto finish
    80 
    81 error: 
    82 select ErrorCode=@rc
    83 
    84 finish:
    85 go
    View Code

    此时用BCompare比较上述脚本与手动导出的StandardTrace.sql脚本之间的差异:

     1 /****************************************************/
     2 /* Created by: SQL Server 2008 R2 Profiler          */
     3 /* Date: 2015/12/05  14:53:04         */
     4 /****************************************************/
     5 
     6 
     7 -- Create a Queue
     8 declare @rc int
     9 declare @TraceID int
    10 declare @maxfilesize bigint
    11 declare @DateTime datetime
    12 
    13 set @DateTime = '2015-12-05 21:25:30.000'
    14 set @maxfilesize = 5 
    15 exec @rc = sp_trace_create @TraceID output, 2, N'E:PerfmonStandardTrace.trc', @maxfilesize, @Datetime
    16 if (@rc != 0) goto error
    17 
    18 -- Client side File and Table cannot be scripted
    19 
    20 -- Set the events
    21 declare @on bit
    22 set @on = 1
    23 exec sp_trace_setevent @TraceID, 10, 15, @on
    24 exec sp_trace_setevent @TraceID, 10, 31, @on
    25 exec sp_trace_setevent @TraceID, 10, 8, @on
    26 exec sp_trace_setevent @TraceID, 10, 16, @on
    27 exec sp_trace_setevent @TraceID, 10, 1, @on
    28 exec sp_trace_setevent @TraceID, 10, 9, @on
    29 exec sp_trace_setevent @TraceID, 10, 17, @on
    30 exec sp_trace_setevent @TraceID, 10, 10, @on
    31 exec sp_trace_setevent @TraceID, 10, 18, @on
    32 exec sp_trace_setevent @TraceID, 10, 11, @on
    33 exec sp_trace_setevent @TraceID, 10, 35, @on
    34 exec sp_trace_setevent @TraceID, 10, 12, @on
    35 exec sp_trace_setevent @TraceID, 10, 13, @on
    36 exec sp_trace_setevent @TraceID, 10, 14, @on
    37 exec sp_trace_setevent @TraceID, 12, 15, @on
    38 exec sp_trace_setevent @TraceID, 12, 31, @on
    39 exec sp_trace_setevent @TraceID, 12, 8, @on
    40 exec sp_trace_setevent @TraceID, 12, 16, @on
    41 exec sp_trace_setevent @TraceID, 12, 1, @on
    42 exec sp_trace_setevent @TraceID, 12, 9, @on
    43 exec sp_trace_setevent @TraceID, 12, 17, @on
    44 exec sp_trace_setevent @TraceID, 12, 10, @on
    45 exec sp_trace_setevent @TraceID, 12, 14, @on
    46 exec sp_trace_setevent @TraceID, 12, 18, @on
    47 exec sp_trace_setevent @TraceID, 12, 11, @on
    48 exec sp_trace_setevent @TraceID, 12, 35, @on
    49 exec sp_trace_setevent @TraceID, 12, 12, @on
    50 exec sp_trace_setevent @TraceID, 12, 13, @on
    51 exec sp_trace_setevent @TraceID, 13, 8, @on
    52 exec sp_trace_setevent @TraceID, 13, 1, @on
    53 exec sp_trace_setevent @TraceID, 13, 9, @on
    54 exec sp_trace_setevent @TraceID, 13, 10, @on
    55 exec sp_trace_setevent @TraceID, 13, 14, @on
    56 exec sp_trace_setevent @TraceID, 13, 11, @on
    57 exec sp_trace_setevent @TraceID, 13, 35, @on
    58 exec sp_trace_setevent @TraceID, 13, 12, @on
    59 
    60 
    61 -- Set the Filters
    62 declare @intfilter int
    63 declare @bigintfilter bigint
    64 
    65 exec sp_trace_setfilter @TraceID, 1, 0, 7, N'%DBA%'
    66 exec sp_trace_setfilter @TraceID, 1, 0, 1, NULL
    67 exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Server Profiler - b42ea627-5083-476a-95d1-664e5e74731e'
    68 exec sp_trace_setfilter @TraceID, 11, 0, 6, N'%Debug%'
    69 exec sp_trace_setfilter @TraceID, 11, 1, 6, N'%Client%'
    70 exec sp_trace_setfilter @TraceID, 11, 0, 1, NULL
    71 set @bigintfilter = 2000000
    72 exec sp_trace_setfilter @TraceID, 13, 0, 4, @bigintfilter
    73 
    74 set @bigintfilter = 5000000
    75 exec sp_trace_setfilter @TraceID, 13, 0, 5, @bigintfilter
    76 
    77 set @bigintfilter = NULL
    78 exec sp_trace_setfilter @TraceID, 13, 0, 1, @bigintfilter
    79 
    80 set @intfilter = 33
    81 exec sp_trace_setfilter @TraceID, 18, 0, 4, @intfilter
    82 
    83 set @intfilter = NULL
    84 exec sp_trace_setfilter @TraceID, 18, 0, 1, @intfilter
    85 
    86 -- Set the trace status to start
    87 exec sp_trace_setstatus @TraceID, 1
    88 
    89 -- display trace id for future references
    90 select TraceID=@TraceID
    91 goto finish
    92 
    93 error: 
    94 select ErrorCode=@rc
    95 
    96 finish: 
    97 go
    View Code


    差异集中在事件和列顺序,以及筛选条件对null的处理。实际上两个脚本所定义跟踪完全相同。此时运行脚本,就能创建与StandardTrace.trc相同的跟踪了。
    执行后sys.traces跟踪目录视图就会包含三条记录:

    补充:有没有不用手动调整筛选部分代码就可以直接得出与原跟踪定义完全相同的脚本。此脚本根据<Professional SQL Server 2008 Internals and Troubleshooting>中10.7.3章节作了部分调整得出。筛选部分用游标写了段代码可以直接得出,但要一次得到整个跟踪定义却不知道如何拼凑,暂时放上筛选部分的获取代码:

     1 DECLARE @intfilter      INT 
     2        ,@BigIntfilter   bigint              
     3 DECLARE GetFilterinfo_Cursor CURSOR FOR 
     4 SELECT cast(fil.columnid AS VARCHAR(20)),cast(fil.logical_operator AS VARCHAR(20)),cast(fil.comparison_operator AS VARCHAR(20))
     5 ,cast(fil.value AS VARCHAR(8000)),cast(col.[type_name] AS VARCHAR(20))
     6 FROM    ::fn_trace_getfilterinfo(3) fil
     7 JOIN    sys.trace_columns col
     8 ON      fil.columnid = col.trace_column_id
     9 OPEN GetFilterinfo_Cursor
    10  DECLARE @columnid            VARCHAR(20),
    11          @logical_operator            VARCHAR(20),
    12          @comparison_operator                VARCHAR(20),
    13          @value            VARCHAR(8000),
    14          @type_name    VARCHAR(20)
    15 FETCH FROM GetFilterinfo_Cursor INTO @columnid,@logical_operator,@comparison_operator,@value,@type_name
    16     WHILE @@FETCH_STATUS=0
    17         BEGIN
    18             IF @value IS NULL
    19             BEGIN
    20                 SET @value='null'
    21                 PRINT 'EXEC @rc = sp_trace_setfilter @traceid,'+@columnid+','+@logical_operator+','+@comparison_operator+','+@value+''
    22             END
    23             ELSE IF @type_name='bigint' and @value is not null
    24             BEGIN
    25                 PRINT 'SET @BigIntfilter='+@value--cast(@value AS varchar(20))
    26                 PRINT 'EXEC @rc = sp_trace_setfilter @traceid,'+@columnid+','+@logical_operator+','+@comparison_operator+',@BigIntfilter'
    27             END
    28             ELSE IF @type_name='int' and @value is not null
    29             BEGIN
    30                 PRINT 'SET @Intfilter='+@value
    31                 PRINT 'EXEC @rc = sp_trace_setfilter @traceid,'+@columnid+','+@logical_operator+','+@comparison_operator+',@Intfilter'
    32             END
    33             ELSE 
    34             BEGIN
    35                 PRINT 'EXEC @rc = sp_trace_setfilter @traceid,'+@columnid+','+@logical_operator+','+@comparison_operator+',N'''+@value+''''
    36             END
    37             FETCH NEXT FROM GetFilterinfo_Cursor INTO @columnid,@logical_operator,@comparison_operator,@value,@type_name
    38         END
    39 CLOSE GetFilterinfo_Cursor;
    40 DEALLOCATE GetFilterinfo_Cursor;
    41 GO
    View Code

    执行此段代码的结果为:

    以上两种方式都可以得到已有服务器端跟踪。

  • 相关阅读:
    python-函数进阶
    SHELL wordpress.sh
    XFS: possible memory allocation deadlock in kmem_alloc (mode:0x2d0)
    Puppet install with nginx unicorn
    CentOS 6内核编译问题整理
    Openstack 本地yum源配置
    Openstack 本地yum源配置
    hpsa 0000:0a:00.0: out of memory
    openstack VNC安全问题
    CentOS下crash分析内核kdump文件方法
  • 原文地址:https://www.cnblogs.com/Uest/p/5021790.html
Copyright © 2011-2022 走看看