Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

How to: Debug a SQL CLR Stored Procedure

This topic applies to:

Visual Studio Edition

Visual Basic




Visual Web Developer



















You can debug an existing SQL CLR stored procedure using direct database debugging, the same way you would debug a T-SQL procedure. However that will not work if you need to create or modify a SQL CLR procedure, because you need to compile and deploy it, steps that do not exist for the T-SQL procedure. In this case, you need to create a SQL Server project in Visual Studio.

The following task creates a new SQL CLR stored procedure in the Adventureworks database, one of the databases installed with SQL Server 2005, and then shows how to debug it. You create a stored procedure that adds a new currency to the Sales.Currency table.

This example focuses on debugging within a SQL Server project. Once you have created the stored procedure, you can debug it using direct database debugging. For more information, see How to: Step into an Object Using Server Explorer.


The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To debug a SQL CLR stored procedure

  1. In a new SQL Server Project, establish a connection to the AdventureWorks sample database. For more information, see How to: Connect to a Database.

  2. Create a new stored procedure using the code from the first example section below, and name it InsertCurrency.cs. For more information, see How to: Develop with the SQL Server Project Type.

  3. Add a script that tests the stored procedure by calling it. In Solution Explorer, right-click the TestScripts directory, click Add Test Script, and insert the code from the second Example section below. Save the file with the name InsertCurrency.sql. Right-click the file name, and click Set as Default Debug Script.

  4. Set breakpoints in InsertCurrency.cs, and then on the Debug menu, click Start to compile, deploy, and unit-test the project. When the instruction pointer, designated by a yellow arrow, appears on a breakpoint, you are debugging your stored procedure.

  5. Try out different debugging features.

    1. Open the Locals window, and on the Debug menu, click Step Into to step one line in the stored procedure. Notice that the value of the variable @mynvarchar has changed in the Locals window and its value is now displayed in red, indicating it has changed. For more information, see Using the Locals Window.


      The server may not reflect changes to values of variables in the debugger windows. For more information, see SQL Debugging Limitations.

    2. Open the Watch window. In the Text Editor, drag the InsertCurrencyCommand variable to any location in the Watch window.

      The variable is now added to the list of watched variables. For more information, see How to: Use Debugger Variable Windows.

      Note   You can edit the values of variables in the Watch window as well.

    3. In the Text Editor, right-click the InsertCurrencyCommand.ExecuteNonQuery line, and on the shortcut menu, click Insert Breakpoint.

    4. On the Debug menu, click Continue and the debugger will run the code up to the new breakpoint.

  6. Click Continue again to finish debugging the stored procedure.

    A message appears in the Output window stating that the stored procedure was successfully deployed, and displaying the result of executing the commands in the InsertCurrency.sql file.


Replace the stored procedure template with this code.

using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlServer;
using System.Data.SqlTypes;

public partial class StoredProcedures
    public static void InsertCurrency(SqlString currencyCode, 
                                               SqlString name)
        using(SqlConnection conn = new SqlConnection("context connection=true")) {
        SqlCommand cmd = new SqlCommand([your SQL statement], conn);
        InsertCurrencyCommand.CommandText = "insert Sales.Currency"
                     + " (CurrencyCode, Name, ModifiedDate) values('" 
                     + currencyCode.ToString() + "', '" 
                     + name.ToString() + "', '" 
                     + DateTime.Now.ToString() + "')";

This is the test script used to execute the stored procedure.

- Delete any row that might exist with a key value
- that matches the one we are going to insert
DELETE Sales.Currency
WHERE CurrencyCode = 'eee'
EXEC InsertCurrency 'eee', 'MyCurr4'
SELECT * FROM Sales.Currency WHERE CurrencyCode = 'eee'

See Also

Community Additions

© 2015 Microsoft