Share via


Walkthrough: Debugging a SQL Server CLR Integration User-Defined Scalar Function

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

This example shows how to debug a SQL Server Common Language Run-time (SQL CLR) User Defined Function (UDF). It creates a new SQL CLR User-Defined Function in the AdventureWorks sample database.

If, when you try to debug a SQL CLR object, the message "Canceled by user" appears, you must manually configure both the computer on which you are running Visual Studio as well as the computer that is running SQL Server. For more information, see How to: Configure Your Computers to Enable Transact-SQL and SQL Server CLR Integration Debugging.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To debug a SQL CLR user-defined scalar function

  1. In a new SQL CLR project, establish a connection to the AdventureWorks sample database. For more information, see How to: Connecting to a Database.

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

  3. Add a script that tests the function by including it in a SELECT statement. 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 CallOrderCount.sql. Right-click the file name, and choose Set as Default Debug Script.

  4. Place a breakpoint in OrderCount.cs on the line that instantiates a SqlCommand, 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 the breakpoint, you are debugging your function.

  5. Try out different debugging features.

    1. Step past the statement instantiating the SqlCommand using Step Into from the Debug menu.

    2. In the Locals window, open the variable sqlComm, which is a SqlCommand, and examine its members.

    3. Click Step Into on the Debug menu to step one line in the function. Note that the member sqlComm.CommandText has been instantiated.

    4. In the Text Editor, drag sqlComm to any location in the Watch window. The variable is now added to the list of watched variables.

    5. Choose Step Into again, and note that a new window labeled Dynamic T-SQL opens, displaying the SQL statement that is about to be executed.

    6. Choose Step Into to execute the statement and return to the function.

    7. Press Continue again, and note the second Dynamic T-SQL window that opens, showing the code that returns the value of the function.

    8. Press Continue again to finish debugging the function.

Example

This is the code required to create the function.

using System;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
 
 
public partial class UserDefinedFunctions
{
    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
    public static int OrderCount()
    {
        using (SqlConnection conn = new SqlConnection("context connection=true"))
            {
                  conn.Open();
                  SqlCommand cmd = new SqlCommand();
                  cmd.Connection = conn;
                  cmd.CommandText = "SELECT 42";
                  
                  //cmd = new SqlCommand("SELECT 42",conn);
 
                  int result = (int)cmd.ExecuteScalar();
                  return result;
            }
    }
};

This is the test script that calls the function.

SELECT dbo.OrderCount()

See Also

Tasks

How to: Create and Run a SQL Server User-Defined Function by using Common Language Run-time Integration