DBCC TRACEON/TRACEOFF/TRACESTATUS

时间:2023-12-13 20:40:02

1. enable trace

DBCC TRACEON ( trace# [ ,...n ][ , -1 ] ) [ WITH NO_INFOMSGS ]

trace#  Is the number of the trace flag to turn on.
n Is a placeholder that indicates multiple trace flags can be specified.
-1  Switches on the specified trace flags globally.
WITH NO_INFOMSGS Suppresses all informational messages.

On a production server, to avoid unpredictable behavior, we recommend that you only enable trace flags server-wide by using one of the following methods:

  • Use the -T command-line startup option of Sqlservr.exe. This is a recommended best practice because it makes sure that all statements will run with the trace flag enabled.

    These include commands in startup scripts. For more information, see sqlservr Application.

  • Use DBCC TRACEON ( trace# [, ....n], -1 ) only while users or applications are not concurrently running statements on the system.

Trace flags are used to customize certain characteristics by controlling how SQL Server operates. Trace flags, after they are enabled, remain enabled in the server until disabled by executing a DBCC TRACEOFF statement. In SQL Server, there are two types of trace flags: session and global. Session trace flags are active for a connection and are visible only for that connection. Global trace flags are set at the server level and are visible to every connection on the server. To determine the status of trace flags, use DBCC TRACESTATUS. To disable trace flags, use DBCC TRACEOFF.

the following example disables hardware compression for tape drivers, by switching on trace flag 3205. This flag is switched on only for the current connection.

DBCC TRACEON (3205);
GO

The following example switches on trace flag 3205 globally.

DBCC TRACEON (3205, -1);
GO

The following example switches on trace flags 3205, and 260 globally.

DBCC TRACEON (3205, 260, -1);
GO
2. disable the trace flag.

DBCC TRACEOFF ( trace# [ ,...n ] [ , -1 ] ) [ WITH NO_INFOMSGS ]

trace# Is the number of the trace flag to disable.
-1 Disables the specified trace flags globally.
WITH NO_INFOMSGS Suppresses all informational messages that have severity levels from 0 through 10.

The following example disables trace flag 3205.

DBCC TRACEOFF (3205);
GO

The following example first disables trace flag 3205 globally

DBCC TRACEOFF (3205, -1);
GO

The following example disables trace flags 3205 and 260 globally.

DBCC TRACEOFF (3205, 260, -1);
GO 3.check the trace flag
DBCC TRACESTATUS ( [ [ trace# [ ,...n ] ] [ , ] [ -1 ] ] ) [ WITH NO_INFOMSGS ]
trace#    Is the number of the trace flag for which the status is displayed. If trace#, and -1 are not specified, all trace flags that are enabled for the session are displayed.
n            Is a placeholder that indicates multiple trace flags can be specified.
-1           Displays the status of trace flags that are enabled globally. If -1 is specified without trace#, all the global trace flags that are enabled are displayed.
WITH NO_INFOMSGS  Suppresses all informational messages that have severity levels from 0 through 10.

The following table describes the information in the result set.

Column name

Description

TraceFlag

Name of trace flag

Status

Indicates whether the trace flag is set ON of OFF, either globally or for the session.

1 = ON

0 = OFF

Global

Indicates whether the trace flag is set globally

1 = True

0 = False

Session

Indicates whether the trace flag is set for the session

1 = True

0 = False

DBCC TRACESTATUS returns a column for the trace flag number and a column for the status. This indicates whether the trace flag is ON (1) or OFF (0).

The column heading for the trace flag number is either Global Trace Flag or Session Trace Flag, depending on whether you are checking the status for a global or a session trace flag.

The following example displays the status of all trace flags that are currently enabled globally.

DBCC TRACESTATUS(-1);
GO

The following example displays the status of trace flags 2528 and 3205.

DBCC TRACESTATUS (2528, 3205);
GO

The following example displays whether trace flag 3205 is enabled globally.

DBCC TRACESTATUS (3205, -1);
GO

The following example lists all the trace flags that are enabled for the current session.

DBCC TRACESTATUS();
GO 备注:
如果开的是global的trace flag,那么也只能通过disable global的trace flag,也就是要带 -1 作为参数. DBCC TRACEON(3605,1222,1204,-1)
DBCC TRACESTATU(-1)

DBCC TRACEOFF(3605,1222,1204,-1)