This documentation is archived and is not being maintained.

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

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 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 you are debugging locally, but the same basic procedure can be done when debugging remotely.


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 process by doing the following.

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

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

    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 gray 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