Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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. The Transact-SQL debugger supports viewing and modifying local variables and parameters, viewing global variables. It also provides the ability to control and manage breakpoints when debugging your Transact-SQL script.
This example shows how to create and debug a Transact-SQL stored procedure by stepping into it.
Note
Transact-SQL debugging isn't available for Azure SQL Database or Azure SQL Managed Instance.
Debug a stored procedure
- In the Database Engine Query Editor window, connect to an instance of the SQL Server Database Engine. Select a database in which you can create an example stored procedure. 
- Paste the following code in the Query Editor. - CREATE TABLE [dbo].[Products] ([Id] INT, [Name] NVARCHAR(128)) CREATE PROCEDURE [dbo].[AddProduct] @id INT, @name NVARCHAR(128) AS BEGIN INSERT INTO [dbo].[Products] ([Id], [Name]) VALUES (@id, @name) SELECT [Name] FROM [dbo].[Products] WHERE [Id] = @id DECLARE @nextid INT SET @nextid = @id + 1 INSERT INTO [dbo].[Products] ([Id], [Name]) VALUES (@id, @name) SELECT [Name] FROM [dbo].[Products] WHERE [Id] = @nextid END
- Press F5 to run the Transact-SQL code. 
- In SQL Server Object Explorer, right-click on the same Database Engine and select New Query.... Ensure you're connected to the same database in which you created the stored procedure. 
- Paste in the following code to the query window. - EXECUTE [dbo].[AddProduct] 50, N'T-SQL Debugger Test'; GO
- Select the left window margin to add a breakpoint to the - EXECstatement.
- Press the dropdown list 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 the SQL menu. Select SQL > Execute With Debugger. 
- Make sure that the Locals window is opened. If not, select the Debug menu, select Windows and Local. 
- Press F11 to step into the query. The parameters of the store procedure and their respective values show up in the Locals window. Alternatively, hover your mouse over the - @nameparameter in the- INSERTclause to see the T-SQL Debugger Test value being assigned to it.
- Select T-SQL Debugger Test in the textbox. Type Validate Change and press ENTER to change the - namevariable's value while debugging. You can also change its value in the Locals window. The value of the parameter is red, indicating a change.
- Press F10 to step over the remaining code. 
- When debugging is complete, query the - Producttable to view its contents.- SELECT * FROM [dbo].[Products]; GO
- In the results window, notice that new rows exist in the table.