Sunday, 3 May 2015

Trace Flags

Trace flags are used within SQL Server to provide information / logging about specific system events, or to change specific configuration of the SQL Server instance. 

Trace flags can be set on / off for the current connection only, using the following T-SQL code:
-- Turn on trace flag 1234
DBCC TRACEON (1234);
GO

-- Turn off trace flag 1234
DBCC TRACEOFF (1234);
GO
Traces can be set on / off globally (i.e. for ALL connections to the SQL Engine) with the addition of the -1 argument at the end:
 -- Turn on trace flag 1234, globally
DBCC TRACEON (1234, -1);
GO
Multiple traces can be switched on by listing them within the parenthesis:
-- Turn on trace flags 1234, 2345 and 3456
DBCC TRACEON (1234, 2345, 3456);
GO
You can see which trace flags are currently active as follows (if none are active, a message of "DBCC execution completed. If DBCC printed error messages, contact your system administrator" will appear): 
-- Check status of current trace flags
DBCC TRACESTATUS
Only members of the sysadmin fixed group can switch traces on / off.

Useful Trace Flags:
1222: Examine deadlocks
1204: Write deadlock information to SQL Server log

Trace flag resources:
MSDN: https://msdn.microsoft.com/en-us/library/ms188396.aspx
SQL Mag: http://sqlmag.com/sql-server/investigating-trace-flags
Stack Exchange: http://dba.stackexchange.com/questions/27704/sql-server-concise-list-of-trace-flags

No comments:

Post a Comment

Updating massive amount of rows whilst avoiding blocking

The following SQL is a good means to split an update on a massive table into smaller chunks, whilst reducing blocking. The method is to upda...