How to: Debug Transact-SQL Using the SQL Server Management Studio

This topic applies to:

Edition

Visual Basic

C#

C++

Web Developer

Express

Topic does not apply Topic does not apply Topic does not apply Topic does not apply

Standard

Topic does not apply Topic does not apply Topic does not apply Topic does not apply

Pro and Team

Topic applies Topic applies Topic applies Topic applies

Table legend:

Topic applies

Applies

Topic does not apply

Does not apply

Topic applies but command hidden by default

Command or commands hidden by default.

You can do all your T-SQL development except debugging inside the SQL Server Management Studio. For complex T-SQL development this is an option to consider.

This procedure assumes that you have already developed and successfully deployed a T-SQL object to the database, and you are now ready to debug it. This procedure also assumes that you are debugging locally, but the same basic procedure can be done when you debug remotely.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To run a test script from SQL Server Management Studio

  1. Open Visual Studio and establish a connection to the database. For more information see How to: Connect to a Database.

  2. Attach the debugger to the SQL Server Management Studio process by doing the following.

    1. From the Tools menu, select Attach to Process.

    2. In the dialog box, find ssms.exe in the list of available processes, and click to highlight.

      Important noteImportant Note:

      You must attach to SQL Server Management Studio before you connect SQL Server Management Studio to the database server. If you do not, then Visual Studio will not stop at the breakpoint.

    3. Press the Select button to open the Select Code Type box.

    4. Select the Automatically determine the type of code to debug option, and click OK.

    5. Click the Attach button.

  3. In Server Explorer, open the database object to be debugged and set breakpoints.

  4. In the SQL Server Management Studio, run a script that will test the database object. You cannot set breakpoints in this script, but the script will cause the database object to execute, and its breakpoints to be hit.

  5. The code for the object will appear in Visual Studio with a yellow arrow in the left shaded margin indicating the statement about to be executed. You may now use most of the familiar debugger features.

  6. To finish debugging, press F5 or Start. The code will execute and exit the debugger.

See Also

Other Resources

Common Procedure Reference

Change History

Date

History

Reason

June 2010

Updated topic to specify the process to which you connect and when you should connect to help users avoid issues. Updates made because of customer feedback.

Customer feedback.