DECLARE @sql nvarchar(MAX) = (
SELECT 'EXEC sp_trace_setstatus ' + CAST(id AS varchar(10)) + ', 0;
EXEC sp_trace_setstatus ' + CAST(id AS varchar(10)) + ', 2;
'
FROM sys.traces
WHERE path LIKE '%\yourtracefilenamepattern%.trc'
ORDER BY id
FOR XML PATH(''), TYPE).value('.', 'nvarchar(MAX)');
PRINT @sql;
--EXEC(@sql);
扩展事件跟踪:
DECLARE @sql nvarchar(MAX) = (
SELECT 'ALTER EVENT SESSION ' + QUOTENAME(name) + ' ON SERVER STATE = STOP;
DROP EVENT SESSION ' + QUOTENAME(name) + ' ON SERVER;
'
FROM sys.dm_xe_sessions
WHERE name LIKE '%yourtracenamepattern%'
ORDER BY name
FOR XML PATH(''), TYPE).value('.', 'nvarchar(MAX)');
PRINT @sql;
--EXEC (@sql);
下面是几个 T-SQL 示例,它们将停止和删除服务器端 SQL 跟踪或您可以针对您的特定情况自定义的扩展事件跟踪。
SQL 跟踪:
扩展事件跟踪: