跟踪(Profiler)常被我们用来检查性能问题。通常我们会有针对CPU、Duration、Reads的创建跟踪定义,这一类的脚本一般不会包含太多的事件和列,筛选条件也相对简单。假如某一天你使用GUI定义了一个包含大量事件和列的服务器端跟踪,点击运行后你直接把GUI关掉,而你却没有导出原始定义脚本。此时你想知道定义的筛选条件,或者定义的事件和列,甚至是希望另外创建一个相同的跟踪(你肯定不希望再次打开GUI设置繁琐的事件和列及筛选条件)。其实只要跟踪定义在SQL Server内部创建之后,你可以通过查询多个跟踪目录视图得到运行中的服务器端跟踪定义。
首先我们用GUI创建一个跟踪,常规属性如下所示:
选择事件和列:
设置过滤条件:
事件选择设置好后,点击运行,此时Profiler如下所示:
查看sys.traces跟踪目录视图:
TraceID=2的是行集提供者跟踪,TraceID=3的是文件提供者跟踪
我们将行集提供者跟踪停止并移除:
--停止跟踪 exec sp_trace_setstatus 2, 0 --移除跟踪 exec sp_trace_setstatus 2, 2
移除后Profiler如下所示:
此时sys.traces跟踪目录视图只包含两条记录:
注意此时写入文件系统上的E:\Perfmon\StandardTrace.trc对应的服务器端跟踪还是在运行的。此时我们可以通过 文件|属性 来看看跟踪的定义,还可以通过 文件|导出|编写跟踪定义的脚本,并选择目标导出跟踪定义:
我们先导出跟踪定义StandardTrace.sql(后期对比),然后将Profiter关闭。
用下面的脚本提取服务器端跟踪定义:
1 -- 传入需要查看的TraceID 2 DECLARE @TraceID int 3 SET @TraceID = 3 4 5 SELECT 6 ‘/************************************************************ 7 * 功能说明:查询跟踪目录视图得到运行中的服务器端跟踪定义 8 * 注意事项:如果要运行提取出的跟踪定义,请注意调整筛选条件部分参数赋值的位置 9 ************************************************************/ 10 ‘ 11 UNION ALL 12 SELECT 13 ‘-- Create a Queue 14 DECLARE @rc int 15 ,@TraceID int 16 ,@MaxFileSize bigint 17 ,@DateTime datetime 18 19 ‘ 20 UNION ALL 21 SELECT ‘SET @DateTime = ‘‘‘+ convert(varchar,stop_time,121)+‘‘‘‘ 22 FROM sys.traces 23 WHERE ID = @TraceID 24 UNION ALL 25 SELECT ‘SET @MaxFileSize = ‘+ CAST(max_size as varchar(20)) 26 FROM sys.traces 27 WHERE ID = @TraceID 28 UNION ALL 29 select ‘EXEC @rc = sp_trace_create @TraceID output ,‘ 30 + 31 CASE WHEN [path] like 32 ‘\\?\C:\Program Files\Microsoft SQL Server\MSSQL__.MSSQLSERVER\MSSQL\DATA\blackbox%‘ 33 THEN ‘@options = 8‘ 34 ELSE 35 CASE WHEN is_shutdown = 1 36 AND is_rollover = 1 THEN ‘6‘ 37 WHEN is_shutdown = 1 THEN ‘4‘ 38 WHEN is_rollover = 1 THEN ‘2‘ 39 ELSE ‘0‘ 40 END + ‘,N‘‘‘ 41 --+ REVERSE(SUBSTRING(SUBSTRING(REVERSE([path]),5,LEN([path])) 42 -- ,CHARINDEX(‘_‘,SUBSTRING(REVERSE([path]),5,LEN([path])),1)+1 43 -- ,LEN([path]))) 44 + LEFT([path],LEN([path])-4)+CONVERT(VARCHAR,GETDATE(),112)+REPLACE(CONVERT(VARCHAR,GETDATE(),108),‘:‘,‘‘) 45 +‘‘‘, @MaxFileSize, @DateTime‘ 46 --+‘,‘+ coalesce(‘‘‘‘+convert(varchar(20),stop_time,126)+‘‘‘‘,‘NULL‘) 47 + CASE WHEN is_rollover = 1 48 THEN ‘,‘+cast(max_files as varchar(20)) 49 ELSE ‘‘ 50 END 51 END 52 from sys.traces 53 WHERE ID = @TraceID 54 UNION ALL 55 SELECT ‘if (@rc != 0) goto error 56 57 -- Set the events 58 DECLARE @on bit = 1 59 ‘ 60 UNION ALL 61 SELECT CASE WHEN [path] like 62 ‘\\?\C:\Program Files\Microsoft SQL Server\MSSQL%.MSSQLSERVER\MSSQL\DATA\blackbox%‘ 63 THEN ‘‘ 64 ELSE 65 ‘EXEC @rc = sp_trace_setevent @TraceID‘ 66 +‘,‘+CAST(EventID as varchar(20)) 67 +‘,‘+CAST(columnid as varchar(20)) 68 +‘, @on‘ 69 END 70 FROM ::fn_trace_geteventinfo(@TraceID) evi 71 CROSS APPLY sys.traces trc 72 WHERE trc.id = @TraceID 73 UNION ALL 74 SELECT ‘ 75 -- Set the Filters 76 DECLARE @Intfilter int 77 ,@BigIntfilter bigint 78 -- 请将参数的赋值放置到对应筛选表达式的前面(如果有的话) 79 ‘ 80 UNION ALL 81 SELECT CASE WHEN col.type_name = ‘int‘ 82 THEN ‘SET @IntFilter = ‘ 83 +CAST(CAST(value as int)AS varchar(20)) 84 +‘--‘+CAST(columnid AS varchar(20))+‘,‘+CAST(logical_operator AS varchar(20))+‘,‘+CAST(comparison_operator AS varchar(20)) 85 WHEN col.type_name = ‘bigint‘ 86 THEN ‘SET @BigIntFilter = ‘ 87 +CAST(CAST(value as bigint)AS varchar(20)) 88 +‘--‘+CAST(columnid AS varchar(20))+‘,‘+CAST(logical_operator AS varchar(20))+‘,‘+CAST(comparison_operator AS varchar(20)) 89 ELSE ‘‘ 90 END 91 FROM ::fn_trace_getfilterinfo(@TraceID) fil 92 JOIN sys.trace_columns col 93 ON fil.columnid = col.trace_column_id 94 WHERE CHARINDEX(‘int‘,col.type_name)>0 and fil.value is not null 95 UNION ALL 96 SELECT ‘EXEC @rc = sp_trace_setfilter @TraceID‘ 97 +‘,‘+CAST(columnid as varchar(20)) 98 +‘,‘+CAST(logical_operator as varchar(20)) 99 +‘,‘+CAST(comparison_operator as varchar(20)) 100 +‘,‘+CASE WHEN value IS NULL THEN ‘null‘ 101 WHEN col.type_name = ‘int‘ THEN ‘@IntFilter‘ 102 WHEN col.type_name = ‘bigint‘ THEN ‘@BigIntFilter‘ 103 ELSE ‘N‘‘‘+CAST(value as varchar(8000))+‘‘‘‘ 104 END 105 FROM ::fn_trace_getfilterinfo(@TraceID) fil 106 JOIN sys.trace_columns col 107 ON fil.columnid = col.trace_column_id 108 UNION ALL 109 SELECT 110 ‘ 111 -- Set the trace status to start 112 exec sp_trace_setstatus @TraceID, 1 113 114 -- Display trace id for future references 115 select [email protected] 116 goto finish 117 ‘ 118 UNION ALL 119 SELECT 120 ‘ 121 error: 122 select [email protected] 123 124 finish: 125 go 126 ‘
在我的机器上服务器端跟踪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:\Perfmon\StandardTrace20151205151034‘, @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
此时的脚本还不能直接运行,我们需要将图中红框中的内容作适当调整:
调整为如下所示:
调整后最终的跟踪定义脚本如下:
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:\Perfmon\StandardTrace20151205151034‘, @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
此时用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:\Perfmon\StandardTrace.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
差异集中在事件和列顺序,以及筛选条件对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
执行此段代码的结果为:
以上两种方式都可以得到已有服务器端跟踪。