Professional Documents
Culture Documents
The problem with using Profiler is that it is a client tool and unless it is run on the server itself the
connection may be lost and your trace stops. This could hit you hard if you are running profiler for
hours. This usually happens right before the problem your trying to troubleshoot occurs and you don’t
end up collecting that valuable information you need.
Client side trace:
1. Any time you open SQL Server Profiler and run a trace, you’re running a client-side trace.
2. If you open SQL Profiler on the server and run it there, it’s still client-side.
One alternative to using Profiler is to run a Server Side Trace. This process runs on the server and
collects trace statistics pretty much the same way that you do using Profiler, but the process involves
running a T-SQL script to start, run and stop the trace vs. using the Profiler GUI.
To run a server-side trace, one needs to create a script using below steps.
1. Open up Profiler and create a new trace as per your requirement.
2. Select Save to File and select a location to save trace file.
3. Select Enable File Rollover.
Select Enable Stop Time (Auto stop time for trace).Saurabh Sinha Page 1 9/15/2021
4. Choose your events and columns from the Events Selection tab.
5. Run the trace and then stop it right away.
6. From the File menu, choose Export > Script Trace Definition > and save the script to file.
7. Open your file in SSMS, making sure you’re connected to the instance you want to profile.
8. Search for sp_trace_create and replace fileName with exact location where you want to save
your trace ; Replace 0 with 2 to enable file rollover
Example:
exec @rc = sp_trace_create @TraceID output, 0, N’InsertFileNameHere’, @maxfilesize, @Datetime
Syntax: Reference
sp_trace_create [ @traceid = ] trace_id OUTPUT
, [ @options = ] option_value ---> Set it 2 for file rollover
, [ @tracefile = ] ‘trace_file’
[ , [ @maxfilesize = ] max_file_size ]
[ , [ @stoptime = ] ‘stop_time’ ]
[ , [ @filecount = ] ‘max_rollover_files’ ]
The server side trace can be modified to collect any event that the trace process can capture what data
to collect
To create the trace for these events and columns the command would look as follows: this
generated as sql script from above mention steps
/***********************************************/
/* Server Side Trace */
/***********************************************/
/****************************************************/
/* Created by: SQL Server 2008 R2 Profiler */
/* Date: 04/07/2013 00:24:13 AM */
/****************************************************/
-- Create a Queue
declare @rc int
declare @TraceID int
declare @maxfilesize bigint
declare @DateTime datetime
error:
select ErrorCode=@rc
finish:
go
There are basically four components to this to get this running:
EXEC sp_trace_setevent @TraceID, 9, 1, @on
EXEC sp_trace_setevent @TraceID, 9, 12, @on
EXEC sp_trace_setevent @TraceID, 9, 13, @on
To start, stop and delete a trace you use the following commands.
trace
To close and To delete you need to stop the trace first
sp_trace_setstatus traceid,0
delete a and then you can delete the trace. This
sp_trace_setstatus traceid, 2
trace will close out the trace file that is written.
Once the data has been collected you can view in no. of ways
1. Just drag and drop them in sql profiler
3. Load the data into a trace table and then run queries against the trace file. Using below
commands .
The following table describes the code values that users may get following completion of the stored
procedure.
Return
Description
code
0 No error.
1 Unknown error.
2 The trace is currently running. Changing the trace at this time will result in an error.
The specified event is not valid. The event may not exist or it is not an appropriate one for
3
the store procedure.
4 The specified column is not valid.
9 The specified trace handle is not valid.
Saurabh Sinha Page 4 9/15/2021
542313236.doc : Created by Saurabh Sinha