INF: How to Automate SQL Trace by Means of Scheduled TasksID: Q194860
|
You can use the SQL Trace utility in SQL Server 6.5 to monitor all incoming events sent to SQL Server. This article describes how to use the xp_sqltrace extended stored procedure with scheduled tasks to automate the tracing of incoming events to SQL Server. Xp_sqltrace is the procedure that the SQL Trace utility itself calls. Because tracing all incoming events can produce a large trace file, the procedure will allow you to change the output file of the trace automatically and delete the old trace files.
The steps below walk through setting up xp_sqltrace to capture all incoming
SQL Server events. If you want to alter this procedure to apply filtering
to the trace, or if you want detailed information on the xp_sqltrace
procedure, see the "xp_sqltrace (version 6.5)" topic in SQL Server Books
Online.
By default with SQL Trace, only completed events are captured. However,
there is an option for you to capture events on a prefilter basis. This
enables you to capture events as they are sent to the server, rather than
after they are completed. For more information, see below.
To automate the tracing, perform the following steps:
use master
go
create procedure sp_audit_trace
@directory varchar(30) = 'C:',
@eventfilter varchar(3) = '31'
as
declare @month char(2), @day char(2), @hour char(2),
@minute char(2), @str varchar(255)
select @month =
CASE WHEN (DATEPART(month, getdate())) < 10
THEN '0' + convert
(char(2), DATEPART(month, getdate()))
ELSE convert (char(2), DATEPART(month, getdate()))
END
select @day =
CASE WHEN (DATEPART(day, getdate())) < 10
THEN '0' + convert
(char(2), DATEPART(day, getdate()))
ELSE convert (char(2), DATEPART(day, getdate()))
END
select @hour =
CASE WHEN (DATEPART(hour, getdate())) < 10
THEN '0' + convert
(char(2), DATEPART(hour, getdate()))
ELSE convert (char(2), DATEPART(hour, getdate()))
END
select @minute =
CASE WHEN (DATEPART(minute, getdate())) < 10
THEN '0' + convert
(char(2), DATEPART(minute, getdate()))
ELSE convert (char(2), DATEPART(minute, getdate()))
END
select @str = "exec master..xp_sqltrace audit, @eventfilter="
+ @eventfilter + ", @fulltext=1, @fullfilepath='" + RTRIM(@directory)
+ "\sqltrace" + @month + @day + "_" + @hour + @minute + ".txt'"
exec (@str)
Name: Audit Trace
Type: TSQL
Database: Master
Command: Exec sp_audit_trace @directory = '<directory>'
Schedule: Recurring - set the schedule as desired.
Where <directory> is the directory you want the trace file to go to.
For example, if you want the file to go to D:\mydir, you would type
the following:Exec sp_audit_trace @directory = 'D:\mydir'If you don't specify a directory, it will default to the root of drive C.
MM = month when the trace was started
DD = day when the trace was started
hh = hour when the trace was started
mm = minute when the trace was started
Sqltrace1013_1034.txt
Sqltrace1014_1707.txt
Exec sp_audit_trace @eventfilter = '115',
@directory = '<directory>'
Name: Audit Trace Stop
Type: TSQL
Database: Master
Command: Exec xp_sqltrace audit, @eventfilter = 0
Schedule: On Demand
Additional query words: filename SEM
Keywords :
Version : WINNT:6.5
Platform : winnt
Issue type : kbhowto kbinfo
Last Reviewed: April 13, 1999