Blog Post

SQL Server Blog
3 MIN READ

Debugging T-SQL using Visual Studio

erinstellato's avatar
erinstellato
Icon for Microsoft rankMicrosoft
Aug 30, 2024

Hey folks - I've seen some comments and replies stating that folks cannot use the debugger with SSDT in Visual Studio, so I decided to give it a try to see if there were any issues.  Good news, it works!  In this post we'll step through how to use the debugger with SSDT and Visual Studio, and I'll call out up front that it requires more permissions than you might realize.

 

To get started, install Visual Studio, and the SQL Server Data Tools – Sql Editor (SSDT) extension if you don't already have them.  If you do not have a license for Visual Studio, that’s ok, you can install Community edition!

 

Setup

 

With Visual Studio and SSDT installed, find a development or test server that you can use for initial testing.  I do not recommend working in a production environment.  All examples here use a local installation of SQL Server 2022 to prevent breaking anything for anyone else.

The first step is to connect to the instance and a test database and create a few objects.

 

Screenshot of creating a table and stored procedure in Visual Studio with SSDT

 

CREATE TABLE [dbo].[SampleProducts] (

                [ID] INT IDENTITY (1,1),

                [ProductName] NVARCHAR(100)

                );

GO

 

CREATE PROCEDURE [dbo].[AddSampleProduct] Name NVARCHAR(100)

AS

BEGIN

                INSERT INTO [dbo].[SampleProducts] ([ProductName])

                VALUES (@Name)

                DECLARE @NewID INT

                SELECT @NewID = ID

                FROM [dbo].[SampleProducts]

                WHERE [ProductName] = Name

                DECLARE @CharID NVARCHAR(10)

                SET @CharID = CAST(@NewID AS NVARCHAR(10))

                INSERT INTO [dbo].[SampleProducts] ([ProductName])

                VALUES (@CharID)

END;

GO

 

Debugging

 

Now we can start debugging.  In a new editor window, put a debug breakpoint next to the T-SQL statement of interest, and then initiate debugging via the Execute with Debugger icon (I added it to the toolbar in the screenshot below), or ALT + F5, or SQL -> Execute with Debugger.  For this example, I started debugging with @StartDebug variable declaration to show stepping into a stored procedure.

 

Screenshot of starting to debug T-SQL in Visual Studio with SSDT

 

With the debugger started we can step into the code, allowing us to see the current values for any parameter:

 

Screenshot of debugging T-SQL with variable values in the Locals window

 

Another step in drops us into the AddSampleProduct stored procedure, opening another editor on the right:

 

Screenshot of T-SQL debugging after stepping into a stored procedure

 

A few more steps and all related variables are populated.  As part of testing, you can edit values while debugging:

 

Screenshot of all variables in Locals window while debugging

 

This example illustrates how to use the T-SQL debugger in Visual Studio for SQL Server, and while simple, it encapsulates the typical scenario of how folks used the debugger in SSMS 17 and below: to step through code.

 

Important notes about debugging

 

There are several factors to keep in mind when debugging:

  • Debugging can cause blocking or other unexpected scenarios that affect performance. Do not debug in production. 
  • This will only work for SQL Server on-premises.
  • If the account you’re connected with, or the SQL Server service account, doesn’t have enough permissions, you’ll get errors.

The article Configure firewall rules before running the Transact-SQL debugger has a list of requirements at the end, as well as instructions for configuring firewall rules.  If you run into issues, that article is a good place to start with troubleshooting.  The main documentation for debugging in SSDT starts with the Transact-SQL debugger article

 

Conclusion

 

As discussed in the SSMS Roadmap post, there are no plans to bring this debugger back to SSMS.  For folks who still need this functionality, it exists in Visual Studio (Community edition is free if your company does not have a subscription).  Hope this helps!

Updated Feb 03, 2025
Version 2.0
  • Jungsooooo's avatar
    Jungsooooo
    Copper Contributor
    1. Set a breakpoint at the procedure call.
    2. Perform a step into at the call point.
    3. The procedure does not open but simply executes (as if step over is being performed instead of step into).
    4. Check the result. The return_value is output correctly.

    I’m trying to understand why step into is not working as expected in step 3.
    I know this isn’t a QA channel, but I thought I’d ask anyway. Could you help me figure out the reason?

  • ggp_92020's avatar
    ggp_92020
    Copper Contributor

    erinstellato No this was my first attempt based on the instruction you provided (thx for that!).  It stopped for about 2 secs on the breakpoint then automatically continued(?)

     

  • ArashLaylazi's avatar
    ArashLaylazi
    Copper Contributor

    erinstellato 
    It seems illogical for a SQL Server developer to install bulky software solely for debugging purposes alongside their primary work tool.

  • ggp_92020 Debugging absolutely works in VS 2022 v17.11.2.  I just re-tested to validate.  I assume you had it working in a previous build of VS 2022?

  • ggp_92020's avatar
    ggp_92020
    Copper Contributor

    Does not work in VS 2022 v17.11.2

    Sad that MSFT cannot provide the valuable tools like this!

  • ArashLaylazi's avatar
    ArashLaylazi
    Copper Contributor

    Debugging TSQL in SSMS is necessary, not in VS.
    Please restore this feature to SSMS.

  • Regarding the information about not being able to debug in Visual Studio 2022,
    I think the Debug SQL Stored Procedure not working in VS2022 17.6.4  issue.

    Until the problem was resolved on Nov 15, 2023, non-English VS 2022 did not allow step-in execution of statements in stored procedures.

     

    Currently, step-in execution can be performed without problems even in non-English versions.