运行 Transact-SQL 调试器Run the Transact-SQL Debugger

本主题适用于:是SQL Server是Azure SQL 数据库是Azure SQL 数据仓库是并行数据仓库THIS TOPIC APPLIES TO: yesSQL ServeryesAzure SQL DatabaseyesAzure SQL Data Warehouse yesParallel Data Warehouse

在打开 Transact-SQLTransact-SQL 查询编辑器窗口之后,即可启动 数据库引擎Database Engine 调试器。You can start the Transact-SQLTransact-SQL debugger after you open a 数据库引擎Database Engine Query Editor window. 然后,您可在调试模式下运行 Transact-SQLTransact-SQL 代码,直到您停止调试器。Then, you can run your Transact-SQLTransact-SQL code in debug mode until you stop the debugger. 您可以设置选项以自定义调试器的运行方式。You can set options to customize how the debugger runs.

启动和停止调试器Starting and Stopping the Debugger

启动 Transact-SQLTransact-SQL 调试器的要求如下:The requirements to start the Transact-SQLTransact-SQL debugger are as follows:

  • 如果您的 数据库引擎Database Engine 查询编辑器连接到其他计算机上的 数据库引擎Database Engine 实例,则必须配置调试器进行远程调试。If your 数据库引擎Database Engine Query Editor is connected to an instance of the 数据库引擎Database Engine on another computer, you must have configured the debugger for remote debugging. 有关详细信息,请参阅 运行 TSQL 调试器之前配置防火墙规则For more information, see Configure firewall rules before running the TSQL Debugger.

  • SQL Server Management StudioSQL Server Management Studio 必须在作为 sysadmin 固定服务器角色成员的 Windows 帐户下运行。 must be running under a Windows account that is a member of the sysadmin fixed server roll.

  • 数据库引擎Database Engine 查询编辑器窗口必须使用 Windows 身份验证来连接,或使用作为 sysadmin 固定服务器角色成员的 SQL ServerSQL Server 身份验证登录名来连接。The 数据库引擎Database Engine Query Editor window must be connected by using either a Windows Authentication or SQL ServerSQL Server Authentication login that is a member of the sysadmin fixed server role.

  • 数据库引擎Database Engine 查询编辑器窗口必须从 数据库引擎Database Engine Service Pack 2 (SP2) 或更高版本连接到 SQL Server 2005SQL Server 2005 实例。The 数据库引擎Database Engine Query Editor window must be connected to an instance of the 数据库引擎Database Engine from SQL Server 2005SQL Server 2005 Service Pack 2 (SP2) or later. 如果查询编辑器窗口连接到处于单用户模式下的实例,您将无法运行调试器。You cannot run the debugger when the Query Editor window is connected to an instance that is in single-user mode.

    我们建议在测试服务器上调试 Transact-SQLTransact-SQL 代码,而不要在生产服务器上调试,原因如下:We recommend that Transact-SQLTransact-SQL code be debugged on a test server, not a production server, for the following reasons:

  • 调试是一项需要高特权的操作。Debugging is a highly privileged operation. 因此只允许 sysadmin 固定服务器角色成员在 SQL ServerSQL Server中进行调试。Therefore, only members of the sysadmin fixed server role are allowed to debug in SQL ServerSQL Server.

  • 当您调查多个 Transact-SQLTransact-SQL 语句的运行时,调试会话通常会运行很长时间。Debugging sessions often run for long periods of time while you investigate the operations of several Transact-SQLTransact-SQL statements. 会话获取的锁(如更新锁)可能会持有很长时间,直到终止会话或者提交或回滚事务。Locks, such as update locks, that are acquired by the session might be held for extended periods, until the session is ended or the transaction committed or rolled back.

    启动 Transact-SQLTransact-SQL 调试器可将查询编辑器窗口置于调试模式。Starting the Transact-SQLTransact-SQL debugger puts the Query Editor window into debug mode. 在查询编辑器窗口进入调试模式时,调试器会在第一个代码行处暂停。When the Query Editor window enters debug mode, the debugger pauses at the first line of code. 然后,您可以单步执行代码,在特定 Transact-SQLTransact-SQL 语句上暂停执行,并使用调试器窗口来查看当前执行状态。You can then step through the code, pause the execution on specific Transact-SQLTransact-SQL statements, and use the debugger windows to view the current execution state. 可以通过在 “查询” 工具栏上单击 “调试” 按钮,或在 “调试” 菜单上单击 “启动调试” 来启动调试器。You can start the debugger by either clicking the Debug button on the Query toolbar or by clicking Start Debugging on the Debug menu.

    查询编辑器窗口会保持在调试模式下,直到查询编辑器窗口中的最后一个语句完成或您停止调试模式。The Query Editor window stays in debug mode until either the last statement in the Query Editor window finishes or you stop debug mode. 可以使用以下任何一种方法来停止调试模式和语句执行:You can stop debug mode and statement execution by using any one of the following methods:

  • “调试” 菜单中,单击 “停止调试”On the Debug menu, click Stop Debugging.

  • “调试” 工具栏上,单击 “停止调试” 按钮。On the Debug toolbar, click the Stop Debugging button.

  • “查询” 菜单上,单击 “取消执行查询”On the Query menu, click Cancel Executing Query.

  • “查询” 工具栏上,单击 “取消执行查询” 按钮。On the Query toolbar, click the Cancel Executing Query button.

    也可在 Transact-SQLTransact-SQL “调试” 菜单上单击 “全部分离” ,以停止调试模式,但允许剩余的 语句完成执行。You can also stop debug mode and allow for the remaining Transact-SQLTransact-SQL statements to finish executing by clicking Detach All on the Debug menu.

控制调试器Controlling the Debugger

可以使用以下命令、工具栏和快捷方式控制 Transact-SQLTransact-SQL 调试器的运行方式:You can control how the Transact-SQLTransact-SQL debugger operates by using the following menu commands, toolbars, and shortcuts:

  • “调试” 菜单和 “调试” 工具栏。The Debug menu and the Debug toolbar. 在焦点放到打开的查询编辑器窗口中之前, “调试” 菜单和 “调试” 工具栏处于不活动状态。Both the Debug menu and Debug toolbar are inactive until the focus is placed in an open Query Editor window. 它们将保持活动状态,直到关闭当前项目。They remain active until the current project is closed.

  • 调试器键盘快捷键。The debugger keyboard shortcuts.

  • 查询编辑器快捷菜单。The Query Editor shortcut menu. 当右键单击查询编辑器窗口中的某一行时,会显示快捷菜单。The shortcut menu is displayed when you right-click a line in a Query Editor window. 当查询编辑器窗口处于调试模式时,快捷菜单会显示适用于所选的行或字符串的调试器命令。When the Query Editor window is in debug mode, the shortcut menu displays debugger commands that apply to the selected line or string.

  • 调试器打开的窗口中的菜单项和上下文命令,例如 “监视”“断点”Menu items and context commands in the windows that are opened by the debugger, such as the Watch or Breakpoints windows.

    下表显示了调试器菜单命令、工具栏按钮和键盘快捷键。The following table shows the debugger menu commands, toolbar buttons, and keyboard shortcuts.

调试菜单命令Debug menu command 编辑器快捷方式命令Editor shortcut command 工具栏按钮Toolbar button 键盘快捷键Keyboard shortcut 操作Action
窗口/断点Windows/Breakpoints 不可用Not available “断点”Breakpoints Ctrl+Alt+BCTRL+ALT+B 显示 “断点” 窗口,您可在其中查看和管理断点。Display the Breakpoints window in which you can view and manage breakpoints.
窗口/监视/监视1Windows/Watch/Watch1 不可用Not available 断点/监视/监视1Breakpoints/Watch/Watch1 CTRL+ALT+W, 1CTRL+ALT+W, 1 显示 “监视1” 窗口。Display the Watch1 window.
窗口/监视/监视2Windows/Watch/Watch2 不可用Not available 断点/监视/监视2Breakpoints/Watch/Watch2 CTRL+ALT+W, 2CTRL+ALT+W, 2 显示 “监视2” 窗口。Display the Watch2 window.
窗口/监视/监视3Windows/Watch/Watch3 不可用Not available 断点/监视/监视3Breakpoints/Watch/Watch3 CTRL+ALT+W, 3CTRL+ALT+W, 3 显示 “监视3” 窗口。Display the Watch3 window.
窗口/监视/监视4Windows/Watch/Watch4 不可用Not available 断点/监视/监视4Breakpoints/Watch/Watch4 Ctrl+Alt+W,4CTRL+ALT+W, 4 显示 “监视4” 窗口。Display the Watch4 window.
窗口/局部变量Windows/Locals 不可用Not available 断点/局部变量Breakpoints/Locals Ctrl+Alt+V,LCTRL+ALT+V, L 显示 “局部变量” 窗口。Display the Locals window.
窗口/调用堆栈Windows/Call Stack 不可用Not available 断点/调用堆栈Breakpoints/Call Stack Ctrl+Alt+CCTRL+ALT+C 显示 “调用堆栈” 窗口。Display the Call Stack window.
窗口/线程Windows/Threads 不可用Not available 断点/线程Breakpoints/Threads Ctrl+Alt+HCTRL+ALT+H 显示 “线程” 窗口。Display the Threads window.
ContinueContinue 不可用Not available ContinueContinue Alt+F5ALT+F5 运行到下一个断点。Run to the next breakpoint. 在将焦点放在处于调试模式的查询编辑器窗口上之前,“继续” 处于不活动状态。Continue is not active until you are focused on a Query Editor window that is in debug mode.
“调试”Start Debugging 不可用Not available “调试”Start Debugging Alt+F5ALT+F5 将查询编辑器窗口置于调试模式,并运行到第一个断点。Put a Query Editor window into debug mode and run to the first breakpoint. 如果将焦点放在处于调试模式的查询编辑器窗口上,则 “启动调试” 将由 “继续” 替代。If you are focused on a Query Editor window that is in debug mode, Start Debugging is replaced by Continue.
全部中断Break All 不可用Not available 全部中断Break All Ctrl+Alt+BreakCTRL+ALT+BREAK Transact-SQLTransact-SQL 调试器不使用此功能。This feature not used by the Transact-SQLTransact-SQL debugger.
停止调试Stop Debugging 不可用Not available “停止调试”Stop Debugging Shift+F5SHIFT+F5 使查询编辑器窗口脱离调试模式,并将其恢复到常规模式。Take a Query Editor window out of debug mode and return it to regular mode.
菜单上单击Detach All 不可用Not available 不可用Not available 不可用Not available 停止调试模式,但在查询编辑器窗口中执行剩余的语句。Stops debug mode, but executes the remaining statements in the Query Editor window.
逐语句Step Into 不可用Not available 逐语句Step Into F11F11 运行下一个语句,如果下一个语句运行存储过程、触发器或函数,还将打开处于调试模式的新查询编辑器窗口。Run the next statement, and also open a new Query Editor window in debug mode if the next statement runs a stored procedure, trigger, or function.
逐过程Step Over 不可用Not available 逐过程Step Over F10F10 “逐语句” 相同,只不过它不会调试函数、存储过程或触发器。Same as Step Into, except that no functions, stored procedures, or triggers are debugged.
跳出Step Out 不可用Not available 跳出Step Out Shift+F11SHIFT+F11 执行触发器、函数或存储过程中的剩余代码,而不在任何断点处暂停。Execute the remaining code in a trigger, function, or stored procedure without pausing for any breakpoints. 当控件返回到调用该模块的代码时,常规调试模式恢复。Regular debug mode resumes when control is returned to the code that called the module.
不可用Not available 运行至光标处Run To Cursor 不可用Not available Ctrl+F10CTRL+F10 从上次停止位置开始执行所有代码,一直到当前光标位置,在断点处不停止。Execute all code from the last stop location to the current cursor location without stopping at any breakpoints.
快速监视QuickWatch 快速监视QuickWatch 不可用Not available Ctrl+Alt+QCTRL+ALT+Q 显示 “快速监视” 窗口。Display the QuickWatch window.
切换断点Toggle Breakpoint 断点/插入断点Breakpoint/Insert Breakpoint 不可用Not available F9F9 在当前或选定的 Transact-SQLTransact-SQL 语句上设置一个断点。Position a breakpoint on the current or selected Transact-SQLTransact-SQL statement.
不可用Not available 断点/删除断点Breakpoint/Delete Breakpoint 不可用Not available 不可用Not available 从选定行上删除断点。Delete the breakpoint from the selected line.
不可用Not available 断点/禁用断点Breakpoint/Disable Breakpoint 不可用Not available 不可用Not available 禁用选定行上的断点。Disable the breakpoint on the selected line. 断点在代码行中保留,但不会停止执行,直到重新启用断点。The breakpoint remains on the line of code, but will not stop execution until it is reenabled.
不可用Not available 断点/启用断点Breakpoint/Enable Breakpoint 不可用Not available 不可用Not available 启用选定行上的断点。Enable the breakpoint on the selected line.
删除全部断点Delete All Breakpoints 不可用Not available 不可用Not available Ctrl+Shift+F9CTRL+SHIFT+F9 删除所有断点。Delete all breakpoints.
禁用所有断点Disable All Breakpoints 不可用Not available 不可用Not available 不可用Not available 禁用所有断点。Disable all breakpoints.
不可用Not available 添加监视Add Watch 不可用Not available 不可用Not available 将选定的表达式添加到 “监视” 窗口中。Add the selected expression to the Watch window.

另请参阅See Also

Transact-SQL 调试器 Transact-SQL Debugger
逐句通过 Transact-SQL 代码 Step Through Transact-SQL Code
Transact-SQL 调试器信息 Transact-SQL Debugger Information
数据库引擎查询编辑器 (SQL Server Management Studio) Database Engine Query Editor (SQL Server Management Studio)
实时查询统计信息Live Query Statistics