To create a trace
Note SQL Server Profiler automatically and invisibly creates a trace queue.
Important There are multiple procedures beginning with the string xp_trace_set. When you see xp_trace_set*, this refers to all trace procedures beginning with this string.
This example creates a trace called All Data for Query Analyzer that traces the specified events issued from SQL Server Query Analyzer. The example saves the queue definition in the registry as a shared queue definition.
USE master
DECLARE @queue_handle int
-- A value of 0 for the 5th parameter of xp_trace_addnewqueue provides
-- all columns in the output.
EXEC xp_trace_addnewqueue 1000, 5, 95, 90, 0, @queue_handle OUTPUT
EXEC xp_trace_seteventclassrequired @queue_handle, 29, 1
EXEC xp_trace_setappfilter @queue_handle, 'SQL Server Query Analyzer',
NULL
-- Insert any applicable xp_trace_set* procedures
-- Set the queue destination to a file using Compact ANSI
EXEC xp_trace_setqueuedestination @queue_handle, 2, 1, NULL,
'c:\mssql7\data\trace.txt'
-- Saving the definition is optional and can be done later, or in
-- repeated places, and is shared.
EXEC xp_trace_savequeuedefinition @queue_handle,
'All Data for Query Analyzer', 1
-- Start the trace queue consumer
EXEC xp_trace_startconsumer @queue_handle
Creating Traces | xp_trace_seteventclassrequired |
xp_trace_addnewqueue | xp_trace_setqueuedestination |
xp_trace_savequeuedefinition | xp_trace_startconsumer |