|
Server side tracing allows the collection of Profiler
Trace files (.TRC) by the SQL Server itself, rather
than separately running up Profiler on the server or from a PC.
There are two ways to perform server side tracing; either manually or
automatically.
Please note that this information relates only to SQL Server 2000, not
SQL Server 2005.
Click here for Server-Side Tracing for SQL Server 2005.
There are two methods:
This method works best when it is ad hoc
because the collection will stop and not restart if the database server
is restarted.
Below is the entire script for this process which must be modified to
suit your needs and executed in
sections as described below.
Server-Side_Trace_Setup_SQL2000.zip (Size 3 KB)
-- STEP 1: Create a trace by using sp_trace_create.
declare @TRACE_ID int
declare @tfilename nvarchar(100)
declare @maxfilesize bigint
declare @timestop datetime
set @tfilename = N'E:\SQLTRACE\'
+ convert(nvarchar(100), SERVERPROPERTY('MachineName')) --<-- Change drive and path.
PRINT @tfilename
set @maxfilesize = 100 --<-- Change max file size (units are in MB)
set @timestop = DATEADD(hh,2,getdate()) --<-- If used, change stop time hours (currently 2 hours).
EXEC sp_trace_create @traceid = @TRACE_ID OUTPUT, @options = 2, @tracefile = @tfilename, @maxfilesize = @maxfilesize,
@stoptime = NULL
--EXEC sp_trace_create @traceid = @TRACE_ID OUTPUT, @options = 2, @tracefile = @tfilename,
@maxfilesize = @maxfilesize, @stoptime = @timestop
PRINT str(@TRACE_ID)
RAISERROR ('Server Side Trace created via sp_trace_create. TraceID = %d',16, 1, @TRACE_ID) WITH LOG
In the next section below, change the value of @TRACE_ID to
what was
displayed in Step 1 above.
Change the @eventid and @columnid to suit your data
collection
requirements.
This link
http://msdn.microsoft.com/en-us/library/aa260314%28SQL.80%29.aspx contains
more information about specific
events.
When you are happy with this section of the script, execute
it.
-- STEP 2: Add events with sp_trace_setevent.
BEGIN
declare @TRACE_ID int
declare @onbit bit
SET @onbit = 1
SET @TRACE_ID = 1 --<-- Change value to the same as displayed above, check server log if unsure.
For brevity sake all occurrences of sp_trace_setevent were removed. See script for full list.
END
The next section below, starts the tracing.
Change the value of @TRACEID to what was displayed in Step 1 above.
You should now see a trace file created in the drive and path you
previously specified in the script above.
-- STEP 3: Start the trace with sp_trace_setstatus.
-- Set @traceid to correct value.
EXEC sp_trace_setstatus @traceid = 1, @status = 1
To stop the tracing, run the following.
Change the value of @TRACEID to what was displayed in Step 1 above.
You can start it again by running the script in Step 3.
-- STEP 4: Stop the trace with sp_trace_setstatus.
-- Set @traceid to correct value.
EXEC sp_trace_setstatus @traceid = 1, @status = 0
If you are completely finished with the tracing, then run
the following.
Make sure you run Step 4 and then run the following to remove the trace
events.
Change the value of @TRACEID to what was displayed in Step 1 above.
Restarting the SQL Server will also achieve the same result.
-- STEP 5: Close the trace with sp_trace_setstatus.
-- Only run this when you're no longer interested in using the trace.
-- Set @traceid to correct value.
EXEC sp_trace_setstatus @traceid = 1, @status = 2
This method allows for the automatic collection of trace files
even after the SQL Server has been restarted.
Below is the entire script for this process which must be modified to
suit your needs before being executed.
It is important to note that if the SQL Server is restarted, the trace
file names will begin numbering from "1" again and not continue with
the last used number before the server was restarted.
EXEC master..sp_procoptionRun the following to check that the stored procedure is set to execute on server startup.
@ProcName = 'usp_Server_Side_Trace_SQL2000',
@OptionName = 'startup',
@OptionValue = 'on'
select name from sysobjects
where objectproperty(id,'ExecIsStartup')=1
Unless you want to restart the server, you will need to kick off the tracing by running the following.
EXEC master..usp_Server_Side_Trace_SQL2000
Stop the trace by running the following, making sure that you set @traceid to correct value which can be found in the server log.
-- Stop the trace with sp_trace_setstatus.
EXEC sp_trace_setstatus @traceid = 1, @status = 0
-- Close the trace with sp_trace_setstatus.
EXEC sp_trace_setstatus @traceid = 1, @status = 2
To disable the execution of the stored procedure on server startup then run..
EXEC master..sp_procoptionRun the following to check that the stored procedure is not set to execute on server startup.
@ProcName = 'usp_Server_Side_Trace_SQL2000',
@OptionName = 'startup',
@OptionValue = 'off'
select name from sysobjects
where objectproperty(id,'ExecIsStartup')=1
If you know the location and file name of a trace file, you can use Query Analyzer to read the file using a SELECT query. You can read multiple files by changing the second parameter of fn_trace_gettable.
SELECT *
FROM fn_trace_gettable('E:\SQLTRACE\SERVERNAME_n.trc', 1)
WHERE TextData is not null
In essence you can search the trace file for particular
attributes and
sort/order the data returned.
NOTE: Data
in columns like Duration are in
microseconds rather than milliseconds as seen from Profiler.
Information on SQL Profiler Data Columns: http://msdn.microsoft.com/en-us/library/aa173882(SQL.80).aspx