Applies a filter to a trace. sp_trace_setfilter may be executed only on existing traces that are stopped (status is 0). SQL Server returns an error if this stored procedure is executed on a trace that does not exist or whose status is not 0.
The following table describes the code values that users may get following completion of the stored procedure.
The trace is currently running. Changing the trace at this time results in an error.
The specified Column is not valid.
The specified Column is not allowed for filtering. This value is returned only from sp_trace_setfilter.
The specified Comparison Operator is not valid.
The specified Logical Operator is not valid.
The specified Trace Handle is not valid.
Out of memory. Returned when there is not enough memory to perform the specified action.
The function is not valid for this trace.
sp_trace_setfilter is a SQL Server stored procedure that performs many of the actions previously executed by extended stored procedures available in earlier versions of SQL Server. Use sp_trace_setfilter instead of the xp_trace_set*filter extended stored procedures to create, apply, remove, or manipulate filters on traces. For more information, see Filtering a Trace.
All filters for a particular column must be enabled together in one execution of sp_trace_setfilter. For example, if a user intends to apply two filters on the application name column and one filter on the username column, the user must specify the filters on application name in sequence. SQL Server returns an error if the user attempts to specify a filter on application name in one stored procedure call, followed by a filter on username, then another filter on application name.
Parameters of all SQL Trace stored procedures (sp_trace_xx) are strictly typed. If these parameters are not called with the correct input parameter data types, as specified in the argument description, the stored procedure returns an error.
The following example sets three filters on Trace 1. The filters N'SQLT%' and N'MS%' operate on one column (AppName, value 10) using the "LIKE" comparison operator. The filter N'joe' operates on a different column (UserName, value 11) using the "EQUAL" comparison operator.
sp_trace_setfilter 1, 10, 0, 6, N'SQLT%' sp_trace_setfilter 1, 10, 0, 6, N'MS%' sp_trace_setfilter 1, 11, 0, 0, N'joe'