Author : HASSAN MD TAREQ

Enable CLR in SQL Server

EXEC sp_configure 'clr enabled', 1;  
RECONFIGURE;  
GO

CLR assembly

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public class MySprocs
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void InsertAuthor()
    {
        using (SqlConnection conn = new SqlConnection("context connection=true"))
        {
            SqlCommand comm = new SqlCommand();
            comm.CommandText = "INSERT INTO AUTHORS (FirstName, LastName) VALUES ('Sally', 'Smith')";

            comm.Connection = conn;

            conn.Open();
            comm.ExecuteNonQuery();
            conn.Close();
        }
    }

    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString isActive(SqlInt32 ActiveBit)
    {

        if (ActiveBit == 1)
            return "Active";
        else
            return "Not Active";
    }
}

Import CLR assembly

  • FooDB > Programmability > Assemblies > Right click > New Assembly

Creating stored procedure using CLR object

CREATE PROC InsertAuthor
AS
EXTERNAL NAME CLRTest.MySprocs.InsertAuthor  -- EXTERNAL NAME <aseemblyName>.<className>.<methodName>

Usage

EXEC InsertAuthor

Creating function using CLR object

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public class myFunctions
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString isActive(SqlInt32 ActiveBit)
    {
        if (ActiveBit == 1)
            return "Active";
        else
            return "Not Active";
    }
}
CREATE FUNCTION isActive(@activeBit AS int)
RETURNS NVARCHAR(50)
AS
EXTERNAL NAME CLRfunctions.myFunctions.isActive

Choosing between T-SQL vs. CLR

Now that we’ve seen how to create a stored procedures using T-SQL and how to create stored procedures using C#, you’re going to be faced with a decision. Every stored procedure you create, you have to choose either T-SQL or C#. I’d like to give you some tips on which one to use. There’s very little that can be done and one that can’t be done in the other. If you try hard enough, you can accomplish anything with either one. But each one does have its stress and its weaknesses. T-SQL’s strength is certainly querying data. We can just go in there and directly write a SELECT, UPDATE, INSERT or DELETE statement. You don’t have to set up a connection, don’t have to close a connection. We just write our T-SQL statements and they run. T-SQL however, is poor with complex logic. We have very few decision structures. Basically, you’ve an if, and not a whole lot of else, and we have a very few looping structures. You have a while, and not a whole lot else. T-SQL does not natively support, arrays, collections, for-each or classes, and all of those are things that the typical programmer wants. The .NET languages, their weakness is querying data and it’s not super difficult to query data, but you’re going to have to write approximately 3 to 5 lines of code to set up a connection, and then every time you want to use that connection, you should manually open the connection, and manually close the connection. So it’s placing some administrative overhead on the developer. The .NET however, is very robust with looping and control of flow. We’ve lots of great looping structures, lots of great decision structures in all of the .NET languages. And the .NET languages can be compiled for multiprocessor environments and will run much faster and much more efficiently in a multiprocessor environment. So when making this choice, Microsoft has clearly stated repeatedly that you should use T-SQL for any stored procedure where the primary focus is data access. However, if you have complex logic or a CPU intensive function, they recommend the .NET framework. I certainly agree with both of those recommendations. I’ll go ahead and add a third one, but you also have to think about human resources, the people that are writing the program, if they are much stronger in T-SQL, or much stronger in C#, obviously, that’s going to influence your decision. But if your team of developers has approximately the same level of experience with both, then certainly these are valid ways to make the choice. T-SQL for data access and .NET for complex logic.