Trace flags are used to gather in-depth information about certain logs during some transactions. We use DBCC commands to enable trace flags, disable trace flags or check the status of trace flags. Here i am going to discuss about SQL Server Trace flag and how we can enable, disable or check their status whether they are enabled or disabled on your SQL Server Instance.
Let’s start with the basics. MSDN says “Trace flags are used to temporarily set specific server characteristics or to switch off a particular behavior. Trace flags are frequently used to diagnose performance issues or to debug stored procedures or complex computer systems.”
There are two types of trace flags in SQL Server.
We can enable trace flag for the context of a specific query that we can run under a session. Session trace flags can be enabled for a connection and are visible only to that connection. This type of trace flag never affects another session running on your database server and the effect of the trace flag is lost when that connection logs out.
Global trace flags are set at the server level and must be enabled globally. If you will try to enable global trace flag for a particular session then there will not be any impact of this trace flag. Let’s go ahead and understand how we can check the status of trace flags running on your SQL Server instance.
Check Trace Flag Status
We use DBCC TRACESTATUS command to check all running or active trace flags on database system. We can also check the status of a specific trace flag using this DBCC command. The output of this DBCC command has four columns. First column is the trace flag number, second column shows the status of trace flag, third and fourth columns are showing whether the trace flag is enabled at the global level or session level.
The status column of this DBCC command has two values. The value shows whether the trace flag is ON (1) or OFF (0). You can see the below screenshot to understand the trace flags and their status.
We can run above DBCC command in different ways to get distinct outputs about trace flags status. Here, I am giving you few examples to use DBCC TRACESTATUS command.
Run below command to display the status of all trace flags that are currently enabled globally.
DBCC TRACESTATUS(-1); GO
We can even get status of multiple specific trace flags by executing below command. Run below command to get the status of trace flags 8690 and 1118.
DBCC TRACESTATUS (8690, 1118); GO
If you want to list all the trace flags that are enabled for the current session then you can get it by running below command.
DBCC TRACESTATUS(); GO
If you want to check whether trace flag status for specific trace is enabled globally or not then you can run below command.
DBCC TRACESTATUS(8690, -1)
Enable Trace Flag
We enable trace flags to gather more information about certain logs during some transactions. We use DBCC TRACEON command to enable SQL Server trace flags. Here, I will show you different uses of DBCC TRACEON command to enable trace flags.
Remember, all trace flags enabled using DBCC TRACEON command will be disabled once you restart the SQL Server service. If you want to enable any trace flag permanently then we recommend that you should enable them at startup, by using the -T command line option. This ensures the trace flag remains active after a server restart. You can launch SQL Server Configuration Manager and open the Properties window of SQL Server service. Here you can click on Startup Parameters tab. Here you can add your trace flag number along with -T and then click on Ok button to close the window.
If we need to enable a trace flag globally then we need to run below command with -1 option as shown in below screenshot.
DBCC TRACEON (8690, -1)
Now we can check the trace flag status whether it’s been enabled globally on our database system or not.
If you want this trace to be enabled for current connection only then you should run below command. You can remove -1 from above command.
DBCC TRACEON (8690); GO
Even you can enable multiple trace flags by running single command. You can run below command to enable trace flag 8690 and 1117 globally. If you want to enable it for current connection only then you can remove -1 from below command.
DBCC TRACEON (8690, 1117, -1); GO
Run below DBCC command to check the status of the trace flag that we have enabled by running above command.
You can see trace flag 8690 is showing along with its value set as 1 for session as well as global in below screenshot.
If you want to check trace flag status for specific trace number then you can run below command. Below command will let you know whether trace flag 8690 is enabled globally or not.
DBCC TRACESTATUS(8690, -1)
Disable Trace Flag
Similarly, we can disable trace flag 8690 that we have enabled in above section by executing another DBCC command known as DBCC TRACEOFF. Run below command to disable trace flag. If we will use -1 value along with DBCC TRACEOFF command that means it will disable this trace flag globally.
DBCC TRACEOFF(8690, -1)
If you want to disable it for current connection only then we will remove -1 from above command and run below command.
DBCC TRACEOFF(8690); GO
Now we can again check the status of trace flag 8690 whether this trace is still active or it has been deactivated by above command.
We can see trace 8690 is disabled now and this trace flag cannot be active in below image.
If you want to check trace flag status for a individual trace then you can run below command.
DBCC TRACESTATUS(8690, -1)
- Find Total no of Running Traces files on your Database Server
- Understanding Database Shrink Operation
- How to Avoid Page Split in SQL Server?