DBCC TRACEON (Transact-SQL)DBCC TRACEON (Transact-SQL)

适用对象:yesSQL Server(从 2008 版开始)noAzure SQL 数据库noAzure SQL 数据仓库no并行数据仓库APPLIES TO: yesSQL Server (starting with 2008) noAzure SQL Database noAzure SQL Data Warehouse noParallel Data Warehouse

启用指定的跟踪标志。Enables the specified trace flags.

主题链接图标 TRANSACT-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

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

参数Arguments

trace#trace#
要打开的跟踪标记的编号。Is the number of the trace flag to turn on.

nn
表示可指定多个跟踪标志的占位符。Is a placeholder that indicates multiple trace flags can be specified.

-1-1
以全局方式打开指定的跟踪标记。Switches on the specified trace flags globally.

WITH NO_INFOMSGSWITH NO_INFOMSGS
取消显示所有信息性消息。Suppresses all informational messages.

RemarksRemarks

在生产服务器上,为了避免意外行为,建议您使用下列方法之一,仅在服务器范围内启用跟踪标记。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:

  • 使用 Sqlservr.exe 的 -T 命令行启动选项。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. 有关详细信息,请参阅 sqlservr ApplicationFor more information, see sqlservr Application.
  • 仅在用户或应用程序未对系统以并行方式运行语句时,才使用 DBCC TRACEON (trace# [, ....n],-1)。Use DBCC TRACEON (trace# [, ....n],-1) only while users or applications are not concurrently running statements on the system.

跟踪标记用于通过控制 SQL ServerSQL Server 的运行方式来自定义某些特征。Trace flags are used to customize certain characteristics by controlling how SQL ServerSQL Server operates. 启用的跟踪标记将在服务器中一直保持启用状态,直到执行 DBCC TRACEOFF 语句将其禁用为止。Trace flags, after they are enabled, remain enabled in the server until disabled by executing a DBCC TRACEOFF statement. SQL ServerSQL Server 中,有两种跟踪标志:会话和全局。In SQL ServerSQL 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. 若要确定跟踪标记的状态,请使用 DBCC TRACESTATUS。To determine the status of trace flags, use DBCC TRACESTATUS. 若要禁用跟踪标记,请使用 DBCC TRACEOFF。To disable trace flags, use DBCC TRACEOFF.

开启影响查询计划的跟踪标志后,执行 DBCC FREEPROCCACHE;,以便使用新的影响计划的行为重新编译缓存计划。After turning on a trace flag that affects query plans, execute DBCC FREEPROCCACHE; so that cached plans are recompiled using the new plan-affecting behavior.

结果集Result Sets

DBCC TRACEON 返回以下结果集(消息):DBCC TRACEON returns the following result set (message):

DBCC execution completed. If DBCC printed error messages, contact your system administrator.  

PermissionsPermissions

要求具有 sysadmin 固定服务器角色的成员身份。Requires membership in the sysadmin fixed server role.

示例Examples

以下示例通过打开跟踪标记 3205,禁用磁带驱动程序的硬件压缩功能。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  

以下示例以全局方式打开跟踪标记 3205The following example switches on trace flag 3205 globally.

DBCC TRACEON (3205, -1);  
GO  

以下示例以全局方式打开跟踪标记 3205260The following example switches on trace flags 3205, and 260 globally.

DBCC TRACEON (3205, 260, -1);  
GO  

另请参阅See Also

DBCC (Transact-SQL)DBCC (Transact-SQL)
DBCC TRACEOFF (Transact-SQL)DBCC TRACEOFF (Transact-SQL)
DBCC TRACESTATUS (Transact-SQL)DBCC TRACESTATUS (Transact-SQL)
跟踪标志 (Transact-SQL)Trace Flags (Transact-SQL)
启用可通过特定查询级别上的不同跟踪标志控制的影响计划的 SQL Server 查询优化器行为Enable plan-affecting SQL Server query optimizer behavior that can be controlled by different trace flags on a specific-query level