How to: Debug a SQL CLR Stored Procedure

This topic applies to:

Edition

Visual Basic

C#

C++

Web Developer

Express

Topic does not apply Topic does not apply Topic does not apply Topic does not apply

Standard

Topic does not apply Topic does not apply Topic does not apply Topic does not apply

Pro and Team

Topic applies Topic applies Topic applies Topic applies

Table legend:

Topic applies

Applies

Topic does not apply

Does not apply

Topic applies but command hidden by default

Command or commands hidden by default.

You can debug an existing SQL CLR stored procedure by 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. These 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.

Note

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 that follows, 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 that follows. 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 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.

      Note

      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 also.

    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.

Example

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
{
    [SqlProcedure]
    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() + "')";
        InsertCurrencyCommand.ExecuteNonQuery();
    }
}

This is the test script that is 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

Tasks

How to: Create and Run a CLR SQL Server Stored Procedure

Other Resources

SQL CLR Database Debugging