How to: Debug Stored Procedures
The Transact-SQL debugger allows you to interactively debug stored procedures by displaying the SQL call stack, local variables, and parameters for the SQL stored procedure. As with debugging in other programming languages, you can view and modify local variables and parameters, view global variables, as well as control and manage breakpoints while debugging your Transact-SQL script.
This example shows how to create and debug a Transact-SQL stored procedure by stepping into it.
Warning
The following procedure uses entities created in procedures in the Connected Database Development and Project-Oriented Offline Database Development sections.
To debug stored procedures
In Solution Explorer, right-click the TradeDev project and select Add, then Stored Procedure. Name this new stored procedure AddProduct and click Add.
Paste the following code to the store procedure.
CREATE PROCEDURE [dbo].[AddProduct] @id int, @name nvarchar(128) AS INSERT INTO [dbo].[Product] (Id, Name) VALUES (@id, @name)
Press F5 to build and deploy the project.
In SQL Server Object Explorer, under the Local node, right-click TradeDev database and select New Query.
Paste in the following code to the query window.
EXEC [dbo].[AddProduct] 50, N'Contoso'; GO
Click the left window margin to add a breakpoint to the EXEC statement.
Press the drop-down arrow on the green arrow button in the Transact-SQL editor toolbar and select Execute with Debugger to execute the query with debugging on.
Alternately, you can start debugging from SQL Server Object Explorer. Right-click the AddProduct stored procedure (located under Local -> TradeDev database -> Programmability -> Stored Procedures). Select Debug Procedure…. If the object requires parameters, the Debug Procedure dialog box appears, with a table containing a row for each parameter. Each row in the table contains a column for the name of the parameter, and one for the value of that parameter. Enter values for each parameter, and click OK.
Make sure that the Locals window is opened. If not, click the Debug menu, select Windows and Local.
Press F11 to step into the query. Notice that the parameters of the store procedure and their respective values show up in the Locals window. Alternatively, hover your mouse over the @name parameter in the INSERT clause, and you will see the Contoso value being assigned to it.
Click Contoso in the textbox. Type Fabrikam and press ENTER to change the name variable’s value while debugging. You can also change its value in the Locals window. Notice that the value of the parameter is now displayed in red, indicating that it has changed.
Press F10 to step over the remaining code.
In SQL Server Object Explorer, refresh the TradeDev database node in order to see the new contents in the data view of the Product table.
In SQL Server Object Explorer, under the Local node, find the Product table of the TradeDev database.
Right-click Product table and select View Data. Notice that the new row has been added to the table.