As we all know SQL Server 2005 has CLR integrated with it. This means one can write a code in manage application and execute it SQL Server. I read about this since Microsoft has launched SQL Server 2005, however I have never used it practically till last week.
First question came in my mind when I read about CLR integration was why we require CLR in SQL. After searching on internet and reading some good articles I found the reason. As we all know SQL server has performance issue with cursor and looping. When we use recursive functions or a cursor in stored procedures we have to compromise with performance. While integrating CLR we can write the looping and recursive functionalities in C# (or any other language). CLR stored procedures can take advantage of the built-in functionality provided by the classes in the .NET Framework, making it relatively easy to add functionality such as complex mathematical expressions or data encryption. Plus, since CLR stored procedure are compiled rather than interpreted like T-SQL, they can provide a significant performance advantage for code that’s executed multiple times. Here is the link which shows real time performance difference between T-SQL statements and CLR Procedure. The second reason can be (which is my observation) most of the developers are really good at programming however when it comes to deal with SQL sps and user defined functions they are not as smart as in programming.
In this article I will show you how to create a CLR stored procedure and how to use in code.
Open visual studio, click on new project and select SQL Server project from C# or VB projects and name it as LearnCLRIntegration,
Fig – (1) Creating new project.
Right click on on project in solution explorer and select Add New Item and select stored procedure.
Fig – (2) Create New Stored Procedure.
This will generate a new class as shown in code snippiest,
Fig – (3) Code Snippiest for new CLR stored procedure.
[Microsoft.SqlServer.Server.SqlProcedure] attributes tells the compiler that the fuction StoredProcedure1() will be the CLR stored procedure. You can have SqlFunction, Sqltrigger, SqlMethod, SqlUserDefinedType.. and more as options. You may have understand what this class is however let me describe it more. In this class we can write different methods as we write in normal class. The attribute on method helps compiler to identify that whether this method is a stored procedure or a function or a user defined type. So if you are creating a trigger you need to set the attribute to [Microsoft.SqlServer.Server.SqlTrigger].
I have created a demo table “Employee” in my database and created a sp to insert a record in table. I know that using CLR stored procedure for insert , delete and update when you not have any complex logic before performing above operation is not advisable, however to keep the example simple I have used simple Insert example. Below code shows Insert SP,
Fig – (4) Simple Insert SP
We have used “context connection = true”. It means this CLR stored procedure will be executed in same connection by which the data access layer code has called this sp. Now, Compile the code, if it compiles successfully right click on project in solution explorer and click on Deploy. This will create a assembly in SQL server and creates a clr stored procedure automatically. You can do this task manually also. (If you have used class library as project type instead of SQL Server Project you have to follow steps mentioned below. If you have used SQL Server Project do ignore following 4 steps.) To do this follow steps,
(1) Create a signed assembly from you code. To do this, right click on project and click “properties”. Select the “Sign the assembly” checkbox and select “<New>” from dropdown list. Give a appropriate name to key and that’s it. Now when you compile the project it will generate signed assembly.
Fig – (5) Signing Assembly (Generating strong name for assembly)
(2) Enable CLR integration for selected database. To do this write following commands in query analyzer,
Fig – (6) Enabling CLR Integration
(3) Create assembly in SQL Server.
Fig – (7) Create assembly in SQL Server
There are three modes for Permission Set: SAFE, UNSAFE, EXTERNAL_ACCESS; SAFE – DLL can access only local resources (on same machine), EXTERNAL_ACCESS – DLL can access any resource on network and same machine. In both case it checks ACL for resource. UNSAFE – DLL can access anything without restriction. Choose appropriate permission set for your dll.
(4) Create a stored procedure from assembly,
Fig – (8) Creating a stored procedure.
Here you can see your DLL and stored procedure in SQL server as shown below,
Fig – (9) CLR stored procedure and assembly in SQL Server.
You can use this SP as normal stored procedure in code or in query analyzer. Below is the code that uses this SP for insert,
Fig – (10) Code to use CLR stored procedure in Data Access Layer
CLR stored procedures can not be used as replacement of T-SQL statements. Execution of normal T-SQL statement is much faster than CLR Stored Procedure. We can use CLR Stored Procedure to avoid cursors and complex computation.
Happy Programming !!!