Jen McCown’s (blog | twitter) third assignment in the Applied SQL series is all about setting up a Server Side Trace with SQL Server.
A server side trace can provide the dba the same information they can obtain by running SQL Profiler. Actually, we’ll use SQL Profiler to create the definition used for the server side trace. Which may cause one to ask why, if we have a tool like SQL Profiler why not just use that rather than scripting a server side trace. The main reason, a client side trace which is what we do when running SQL Profiler has significantly more of an impact on the monitored server, meaning that you will experience performance degradation. Linchi Shea has an excellent blog post Performance Impact: Profiler Tracing vs. Server Side SQL Tracing demonstrating the difference between running a client side versus a server side trace.
On to the assignment…
Level 1 Assignments: Generate the script for a server side trace.
[1-1] Generate the script using these options:
- Save to file: c:\temp\Trace
- Enable file rollover
- Enable trace stop time.
- In the events selection, add “DatabaseID” to all selected events
- Add a filter for DatabaseID = 5
To complete assignment 1-1, we need to open SQL Profiler and start a new trace. For the assignment, I left the trace template as the Standard trace.
To add the database id to the trace definition, we need to switch tabs to the Events Selection, check Show all Columns and the select the DatabaseID for all events.
For adding the filter for DatabaseID = 5, click on Column Filters, select DatabaseID and enter 5.
In order to generate the trace definition needed to build the server side trace (and complete the assignment) we need to start and then stop the trace. Once the trace is stopped, go to the File Menu, chose Export, then Script Trace Definition and finally, For SQL Server 2005 – 2008 R2 …

[1-2] Open the script and add these queries to the bottom, commented out:
- Add a trace stop command.
- Add a trace delete command.
- Add a query to get information about all existing server-side traces. (This will take just a little research.)
/*
** [1-2] Open the script and add these queries to the bottom, commented out:
**
** - Add a trace stop command.
** - Add a trace delete command.
** - Add a query to get information about all existing server-side traces.
** (This will take just a little research.)
**
** For the @traceid parameter, we need to set it to the Trace Id returned when
** starting the trace, or looking it up using fn_trace_get_info or from
** the sys.traces cataloged view
*/
-- sp_trace_setstatus @traceid = ?, @status = 0 -- Trace stop
-- sp_trace_setstatus @traceid = ?, @status = 2 -- Trace delete
/*
** Get Information about all existing server side traces.
**
** The is_rowset is used to distinguish the Server Side Trace from
** the Client Side traces. If is_rowset = 0 it means the trace data
** is written directly to disk
*/
--SELECT *
--FROM sys.traces
--WHERE is_rowset = 0 -- Server Side Traces
-- AND is_default = 0; -- Omit the default trace
Level 2 Assignment: Edit the script.
[2-1] Open the generated script and make these edits:
- Stop time should be 30 minutes after the start of the script.
- Max file size should be 20 Mb.
- Trace output file should be c:\temp\MyTestTraceOutput.
- Add the option for trace file rollover.
declare @maxfilesize bigint
declare @DateTime datetime
set @DateTime = DATEADD(MINUTE,30, GETDATE())
set @maxfilesize = 20
-- Please replace the text InsertFileNameHere, with an appropriate
-- filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension
-- will be appended to the filename automatically. If you are writing from
-- remote server to local drive, please use UNC path and make sure server has
-- write access to your network share
exec @rc = sp_trace_create
@TraceID OUTPUT
, 2 -- TRACE_FILE_ROLLOVER
, N'c:\temp\MyTestTraceOutput'
, @maxfilesize
, @Datetime
, 10 -- @filecount
[2-2] Find and comment out all the code that adds tracing for the “SQL:BatchCompleted” event.
First, we need to determine what trace event “SQL:BatchCompleted” is:
SELECT *
FROM sys.trace_events
WHERE name = 'SQL:BatchCompleted';
Now, find and comment out the sp_trace_setevents
exec sp_trace_setevent @TraceID, 10, 6, @on
exec sp_trace_setevent @TraceID, 10, 14, @on
--exec sp_trace_setevent @TraceID, 12, 15, @on
--exec sp_trace_setevent @TraceID, 12, 16, @on
--exec sp_trace_setevent @TraceID, 12, 1, @on
--exec sp_trace_setevent @TraceID, 12, 9, @on
--exec sp_trace_setevent @TraceID, 12, 17, @on
--exec sp_trace_setevent @TraceID, 12, 6, @on
--exec sp_trace_setevent @TraceID, 12, 10, @on
--exec sp_trace_setevent @TraceID, 12, 14, @on
--exec sp_trace_setevent @TraceID, 12, 18, @on
--exec sp_trace_setevent @TraceID, 12, 3, @on
--exec sp_trace_setevent @TraceID, 12, 11, @on
--exec sp_trace_setevent @TraceID, 12, 12, @on
--exec sp_trace_setevent @TraceID, 12, 13, @on
exec sp_trace_setevent @TraceID, 13, 1, @on
exec sp_trace_setevent @TraceID, 13, 9, @on
Complete script for the server side trace…
/****************************************************/
/* Created by: SQL Server 2008 R2 Profiler */
/* Date: 09/10/2011 10:57:07 AM */
/****************************************************/
-- Create a Queue
declare @rc int
declare @TraceID int
declare @maxfilesize bigint
declare @DateTime datetime
set @DateTime = DATEADD(MINUTE,30, GETDATE())
set @maxfilesize = 20
-- Please replace the text InsertFileNameHere, with an appropriate
-- filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension
-- will be appended to the filename automatically. If you are writing from
-- remote server to local drive, please use UNC path and make sure server has
-- write access to your network share
exec @rc = sp_trace_create
@TraceID OUTPUT
, 2 -- TRACE_FILE_ROLLOVER
, N'c:\temp\MyTestTraceOutput'
, @maxfilesize
, @Datetime
, 10 -- @filecount
if (@rc != 0) goto error
-- Client side File and Table cannot be scripted
-- Set the events
declare @on bit
set @on = 1
exec sp_trace_setevent @TraceID, 14, 1, @on
exec sp_trace_setevent @TraceID, 14, 9, @on
exec sp_trace_setevent @TraceID, 14, 6, @on
exec sp_trace_setevent @TraceID, 14, 10, @on
exec sp_trace_setevent @TraceID, 14, 14, @on
exec sp_trace_setevent @TraceID, 14, 3, @on
exec sp_trace_setevent @TraceID, 14, 11, @on
exec sp_trace_setevent @TraceID, 14, 12, @on
exec sp_trace_setevent @TraceID, 15, 15, @on
exec sp_trace_setevent @TraceID, 15, 16, @on
exec sp_trace_setevent @TraceID, 15, 9, @on
exec sp_trace_setevent @TraceID, 15, 17, @on
exec sp_trace_setevent @TraceID, 15, 6, @on
exec sp_trace_setevent @TraceID, 15, 10, @on
exec sp_trace_setevent @TraceID, 15, 14, @on
exec sp_trace_setevent @TraceID, 15, 18, @on
exec sp_trace_setevent @TraceID, 15, 3, @on
exec sp_trace_setevent @TraceID, 15, 11, @on
exec sp_trace_setevent @TraceID, 15, 12, @on
exec sp_trace_setevent @TraceID, 15, 13, @on
exec sp_trace_setevent @TraceID, 17, 1, @on
exec sp_trace_setevent @TraceID, 17, 9, @on
exec sp_trace_setevent @TraceID, 17, 6, @on
exec sp_trace_setevent @TraceID, 17, 10, @on
exec sp_trace_setevent @TraceID, 17, 14, @on
exec sp_trace_setevent @TraceID, 17, 3, @on
exec sp_trace_setevent @TraceID, 17, 11, @on
exec sp_trace_setevent @TraceID, 17, 12, @on
exec sp_trace_setevent @TraceID, 10, 15, @on
exec sp_trace_setevent @TraceID, 10, 16, @on
exec sp_trace_setevent @TraceID, 10, 9, @on
exec sp_trace_setevent @TraceID, 10, 17, @on
exec sp_trace_setevent @TraceID, 10, 2, @on
exec sp_trace_setevent @TraceID, 10, 10, @on
exec sp_trace_setevent @TraceID, 10, 18, @on
exec sp_trace_setevent @TraceID, 10, 3, @on
exec sp_trace_setevent @TraceID, 10, 11, @on
exec sp_trace_setevent @TraceID, 10, 12, @on
exec sp_trace_setevent @TraceID, 10, 13, @on
exec sp_trace_setevent @TraceID, 10, 6, @on
exec sp_trace_setevent @TraceID, 10, 14, @on
--exec sp_trace_setevent @TraceID, 12, 15, @on
--exec sp_trace_setevent @TraceID, 12, 16, @on
--exec sp_trace_setevent @TraceID, 12, 1, @on
--exec sp_trace_setevent @TraceID, 12, 9, @on
--exec sp_trace_setevent @TraceID, 12, 17, @on
--exec sp_trace_setevent @TraceID, 12, 6, @on
--exec sp_trace_setevent @TraceID, 12, 10, @on
--exec sp_trace_setevent @TraceID, 12, 14, @on
--exec sp_trace_setevent @TraceID, 12, 18, @on
--exec sp_trace_setevent @TraceID, 12, 3, @on
--exec sp_trace_setevent @TraceID, 12, 11, @on
--exec sp_trace_setevent @TraceID, 12, 12, @on
--exec sp_trace_setevent @TraceID, 12, 13, @on
exec sp_trace_setevent @TraceID, 13, 1, @on
exec sp_trace_setevent @TraceID, 13, 9, @on
exec sp_trace_setevent @TraceID, 13, 6, @on
exec sp_trace_setevent @TraceID, 13, 10, @on
exec sp_trace_setevent @TraceID, 13, 14, @on
exec sp_trace_setevent @TraceID, 13, 3, @on
exec sp_trace_setevent @TraceID, 13, 11, @on
exec sp_trace_setevent @TraceID, 13, 12, @on
-- Set the Filters
declare @intfilter int
declare @bigintfilter bigint
set @intfilter = 5
exec sp_trace_setfilter @TraceID, 3, 0, 0, @intfilter
exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Server Profiler - e57686a1-a5b3-4a0a-8f9f-1f12a6424f72'
-- Set the trace status to start
exec sp_trace_setstatus @TraceID, 1
-- display trace id for future references
select TraceID=@TraceID
goto finish
error:
select ErrorCode=@rc
finish:
/*
** [1-2] Open the script and add these queries to the bottom, commented out:
**
** - Add a trace stop command.
** - Add a trace delete command.
** - Add a query to get information about all existing server-side traces.
** (This will take just a little research.)
**
** For the @traceid parameter, we need to set it to the Trace Id returned when
** starting the trace, or looking it up using fn_trace_get_info or from
** the sys.traces cataloged view
*/
-- sp_trace_setstatus @traceid = ?, @status = 0 -- Trace stop
-- sp_trace_setstatus @traceid = ?, @status = 2 -- Trace delete
/*
** Get Information about all existing server side traces.
**
** The is_rowset is used to distinguish the Server Side Trace from
** the Client Side traces. If is_rowset = 0 it means the trace data
** is written directly to disk
*/
--SELECT *
--FROM sys.traces
--WHERE is_rowset = 0 -- Server Side Traces
-- AND is_default = 0; -- Omit the default trace
go