Archive

Posts Tagged ‘Debug’

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

September 1, 2015 Leave a comment

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:
%d bloggers like this: