How To Debug TSQL Queries

Since SQL Server 2008, we can debug our TSQL queries as in Visual Studio. In this way, we can see which variable gets what value at run time, especially in cases where we have problems with our long scripts.

After debugging the script, you can see the value of the variables by scrolling line by line with F11 key. Or we want to put a breakpoint on a line, press F5 and go directly to that line to see the value on that line.

I think it’s a feature that makes it really easy to solve errors when developing code with TSQL.

Let’s examine how it works by making a simple example.

Paste the following block of code into a query screen in SQL Server Management Studio.

Then let’s press the Debug button you see above and move line by line by pressing F11.

When we move the mouse cursor over the @Address variable while you are on the line “Select @Adresss”, we will see the current value of the @Address variable.

We need to use breakpoints if we want to go directly to the line we want rather than line by line in long scripts. Let’s put a breakpoint on the line “Select @Adress” (We left-click the beginning of the line we want to breakpoint.)

After debugging, you need to press Alt and F5 simultaneously to go directly to the next breakpoint.

dbtut
Author: dbtut

We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.

Leave a Reply

Your email address will not be published. Required fields are marked *