How to debug a function

The tutorial describes the steps to debug a function with the help of T-SQL Debugger built into dbForge SQL Complete. For demo purposes, we are going to use the dbo.ufnGetStock function from the AdventureWorks2019 database.

To debug a Microsoft SQL Server function:

1. On the standard toolbar, click New Query and type the following EXEC statement:

EXECUTE dbo.ufnGetStock @ProductID = 0

2. To start debugging, click Step Into Step Into or press F11 until a new SQL document with the CREATE statement for the function opens.

Note

The yellow arrow identifies the stack frame where the execution pointer is currently located.

You can view the value of the variable by hovering over the variable in the query. The quick info tip will be displayed. However, be sure that the Enable code completion feature is selected in SQLComplete > Options > General.

View the value of the variable

3. In the CREATE statement document, insert a breakpoint by using one of the following options:

  • Right-click the line of the code where you want to set a breakpoint and select Insert Breakpoint.
  • Place a cursor on the line of the code you want to break on, navigate to the Debug menu, and select Toggle Breakpoint or press F9.
  • Click on the gray bar to the left of the code where you want to insert a breakpoint.

In the SQL query editor, the line with the breakpoint will be highlighted with the red color.

Insert breakpoints

4. On the Debug toolbar, click Debug the function Continue or press Alt+F5.

In the Breakpoints pane that opens, you can view breakpoints set in the code and manage them as follows:

  • Delete the selected breakpoint by clicking Delete the selected breakpoint icon Delete
  • Delete all breakpoints by clicking Delete all breakpoints icon Delete All Breakpoints
  • Disable all breakpoints by clicking Disable all breakpoints icon Disable All Breakpoints
  • Switch to the source code by clicking Go to Source Code icon Go to Source Code

5. Add the variable to the Watches pane. To do that, right-click the variable and then select Add Watch.

Add watch to the variable

Note

The Watches pane displays variables, their value, and type (values of variables being tracked). This option allows you to track the values of the variables while stepping through the code.

Add watch pane

If you want to delete a watch, in the Watches pane, right-click the watch and select Delete Watch.

6. On the Debug toolbar, click Step Into Step Into or press F11 repeatedly to step through the code.

Note

You may ignore stepping into the function by clicking Step Over icon Step Over. In this case, you will continue stepping through the function.

Step through the function until you go back to the function, and continue to the end.

Note

You may get back to the function code by clicking Step Out icon Step Out. In this case, you will continue stepping through the function.

You may click the function in the Call Stack window to get back to the parent code.

7. To stop debugging, use one of the following options:

  • On the Debug menu, select Stop Debugging or press Shift+F5.
  • On the Debugger toolbar, click Stop debugging on the Debug toolbar Stop Debugging.
  • On the Debugger toolbar, click Step Over icon Step Over or press F10.

Debug toolbar

Want to Find out More?

Overview

Overview

Take a quick tour to learn all about the key benefits delivered by dbForge SQL Complete for SQL Server.
All Features

All features

Get acquainted with the rich features and capabilities of the SQL Complete in less than 5 minutes.
Request a demo

Request a demo

If you consider employing the SQL Complete for your business, request a demo to see it in action.
Ready to start using dbForge SQL Complete for SQL Server?