Home > SQL Server > How to debug T-SQL statement in SQL Server Management Studio

How to debug T-SQL statement in SQL Server Management Studio


Debugging is an important feature for every programming language with no exception SQL Server has this to help developer trace the their code and remove potential logical issue in the code before moving to production environment or simply to find the bug in the code.

SQL Server management Studio (SSMS) provide easy way to debug T-SQL statements.

Expectation: This blog post is to show how to use debug feature in SSMS with simple T-SQL statement.

T-SQL

declare @i as int, @n as int;
set @i = 0;
set @n = 5;

while @i < @n
begin
set @i = @i + 1;
print @i;
end

Step 1: Open new query window and paste the above code

Step 2: Click Debug button on the toolbar

debug_button

Step 3: Once debug starts, another debug related toolbar and windows (local variables, call stack) will show up. We can view and modify local variables and parameters, view global variables, as well as control and manage breakpoints while debugging.

debug_window

Step 3: Click Step into (F11 – function key) to execute step by step.

This image is captured after 3 iterations. so, we can see the value of the variable @i = 3 and @n =5.

iteration

Try other debugging features in SSMS and keep  your code robust. Thanks.

Reference

Advertisements
Categories: SQL Server Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: