| title | Debug Database Objects | |
|---|---|---|
| description | Find out how to debug stored procedures, functions, and triggers. See how to turn on debugging, set breakpoints, and run a SQL Server unit test in debug mode. | |
| author | dzsquared | |
| ms.author | drskwier | |
| ms.reviewer | randolphwest | |
| ms.date | 09/09/2025 | |
| ms.service | sql | |
| ms.subservice | ssdt | |
| ms.topic | how-to | |
| ms.custom |
|
A SQL Server unit test consists:
-
Unit test code written in C# or Visual Basic. This code, which is generated by the SQL Server Unit Test Designer, is responsible for submitting the Transact-SQL script that forms the body of the test.
-
One or more test conditions, which are written in C# or Visual Basic. To debug test conditions, follow the procedure for debugging a unit test as described in How to: Debug while a Test Is Running (Visual Studio 2010) or How to: Debug while a Test Is Running (Visual Studio 2012).
-
One or more Transact-SQL scripts that run on objects in the database that you're testing. You can't debug these Transact-SQL scripts.
The procedures in this article describe how to debug particular database objects, such as stored procedures, functions, and triggers in the database you're testing. To debug a database object, follow these procedures in this order:
- Enable SQL Server debugging on your test project.
- Enable application debugging on the SQL Server instance that hosts the database you're testing.
- Set breakpoints in the Transact-SQL script of the database objects you're debugging.
- Debug your unit test. In this procedure, you run the test in debug mode.
-
Open Solution Explorer.
-
In Solution Explorer, right-click the test project, and select Properties.
A properties page that has the same name as the test project opens.
-
On the properties page, select Debug.
-
Under Enable Debuggers, select Enable SQL Server debugging.
-
Save your changes.
-
On the File menu, point to Open, and select File.
-
Browse to the folder that contains your test project, and double-click the
app.configfile.The
app.configfile opens in the editor. -
Modify the
ExecutionContextnode to add a command timeout, as in the following example:<ExecutionContext CommandTimeout ="300" Provider="System.Data.SqlClient" ConnectionString="Data Source=TargetServerName\TargetInstanceName;Initial Catalog=TargetDatabaseName;Integrated Security=True;Pooling=False" />
-
Save your changes.
-
Rebuild your unit test project.
Important
If you don't rebuild your project, the changes that you made to app.config aren't applied when you run your unit tests, and debugging fails.
-
On the View menu, open SQL Server Object Explorer.
-
Under Data Connections, expand the node of the database that you want to test.
-
If a small red 'x' appears next to the icon of the database, the connection to the database is closed. In this case, right-click the database, and select Refresh. You might have to supply credentials to open the connection to the database.
-
Expand the Views, Stored Procedures, or Functions node to find the object that you want to debug.
-
Double-click the object that you want to debug.
-
Select the gray sidebar to set a breakpoint.
-
Visual Studio 2010, open the (Test > Windows) Test View window. In Visual Studio 2012, open the Test Explorer window.
-
Right-click the test whose Transact-SQL script exercises the database object in which you set breakpoints and select Debug Selection.
The test runs in debug mode until a breakpoint in the database object is encountered.
-
(Optional) To open another debug window, open the Debug menu, point to Windows, and select Breakpoints, Output, or Immediate.