Sql Server Start, Stop, Delete server side trace using t-sql
- Posted by Sqltimes
- On October 26, 2014
- 0 Comments
In troubleshooting some issues with databases, sometimes you need to capture server side traces. Sometimes Profiler could help capture specific events as needed. Another option is to use the server side stored procedures to capture trace into a file. There are several points to keep in mind when you run server side traces, but for this post, the content will be limited to stopping and starting server side traces.
These are the steps I follow to create server side traces:
- Step 1: Create trace definition using sp_Trace_Create
- Step 2: Add individual events to capture using sp_Trace_SetEvent
- Step 3: Add filters to limit the amount of transactions you capture using sp_Trace_SetFilter
- Step 4: Start the trace using sp_trace_setstatus
Now, following are the steps to start, stop and delete the definition from Sql Server.
Get a list of all traces that are running. TraceID is used as a parameter for setting status.
1
2
3
4
5
6
|
-- -- Get a list of all traces running -- SELECT * FROM sys.traces GO |
Now using the trace ID set the status to either start, stop or delete.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
-- -- Start trace -- EXEC sp_trace_setstatus 2, 1 GO -- -- Stop trace -- EXEC sp_trace_setstatus 2, 0 GO -- -- Delete trace definition -- EXEC sp_trace_setstatus 2, 2 GO |
Note: Please note that Microsoft is planning to discontinue this feature as they encourage usage of Extended Events
Hope this helps,
_Sqltimes
0 Comments