CLR stored procedures in sql server

To create a CLR stored procedure in SQL Server….

I used this nice blog posting by Scott Sutherland as a starting point:

If you are using SQL Server 2017 it will be necessary to sign your CLR assembly.

If you are doing this in a development environment, you may wish to create a public/private key pair:

Once you’ve got your public key, you can reference it in your assembly at compile time.

Note that not all assemblies are supported by sql server. A list of supported assemblies is available here:

Here is a stackoverflow article that talks about registering the assemblies necessary to use JSON serialization.

Here’s the sample dot net CLR assembly from Scott’s blog posting.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Diagnostics;
using System.Text;
using System.Reflection;

[assembly: AssemblyKeyFileAttribute(@"F:\research\publicprivatekey\public.snk")]
[assembly: AssemblyDelaySignAttribute(true)]

//don't use a namespace

public partial class StoredProcedures
    public static void cmd_exec(SqlString execCommand)
        Process proc = new Process();
        proc.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
        proc.StartInfo.Arguments = string.Format(@" /C {0}", execCommand.Value);
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;

        // Create the record and specify the metadata for the columns.
        SqlDataRecord record = new SqlDataRecord(new SqlMetaData("output", SqlDbType.NVarChar, 4000));

        // Mark the beginning of the result set.

        // Set values for each column in the row
        record.SetString(0, proc.StandardOutput.ReadToEnd().ToString());

        // Send the row back to the client.

        // Mark the end of the result set.


Once you’ve compiled the assembly, you need to put it in a place where the sql server service principal has permissions.

I created a directory, here:
C:\Program Files (x86)\Microsoft SQL Server\CLRSP

You’ll then need to run some sql commands to turn on CLR procedures (if necessary) and to register the new CLR stored procedure. Note that the text in brackets (“sqlclrtest.sqlclrtest”) includes the namespace.

use msdb

-- Enable show advanced options on the server
sp_configure 'show advanced options',1
-- Enable clr on the server
sp_configure 'clr enabled',1
--drop assembly sqlclrtest
-- Import the assembly
CREATE ASSEMBLY [sqlclrtest.sqlclrtest]
FROM 'C:\Program Files (x86)\Microsoft SQL Server\CLRSP\sqlclrtest.dll'

-- Link the assembly to a stored procedure
CREATE PROCEDURE [dbo].[sqlclrtest] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [sqlclrtest.sqlclrtest].[StoredProcedures].[cmd_exec];

In this example, the new CLR stored procedure can then be used to run a sql command.

[sqlclrtest] 'dir'

 Volume in drive C has no label.   Volume Serial Number is AA1D-9186     Directory of C:\WINDOWS\system32    08/27/2018  09:32 AM              .  08/27/2018  09:32 AM              ..  08/03/2018  04:27 PM              ....

Written by

42   Posts

View All Posts

Leave a Reply