Professional Documents
Culture Documents
CLR Stored Procedure in SQL Server 2005
CLR Stored Procedure in SQL Server 2005
In
SQL Server 2005
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.
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. We will see real time performance difference
between T-SQL statements and CLR Procedure in sample project. The second reason
can be 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 we will see how to create a CLR stored procedure and how to use in
code.
1. Open visual studio, click on new project and select SQL Server project from C#
or VB projects and name it as LearnCLRIntegration
1: using System;
2: using System.Data;
3: using System.Data.SqlClient;
4: using System.Data.SqlTypes;
5: using Microsoft.SqlServer.Server;
6:
7:
8: public partial class StoredProcedures
9: {
10: [Microsoft.SqlServer.Server.SqlProcedure]
11: public static void StoredProcedure1()
12: {
13: // Put your code here
14: }
15: };
1: using System;
2: using System.Data;
3: using System.Data.SqlClient;
4: using System.Data.SqlTypes;
5: using Microsoft.SqlServer.Server;
6:
7:
8: public partial class StoredProcedures
9: {
10: [Microsoft.SqlServer.Server.SqlProcedure]
11: public static int sp_InsertEmployee(string FirstName, string LastName, string City)
12: {
13: using (SqlConnection cnn = new SqlConnection("context connection = true"))
14: {
15: cnn.Open();
16: SqlCommand cmd = new SqlCommand("Insert into Employee values ('" + FirstName +
"','" + LastName + "','" + City + "')", cnn);
17: int intRowsAffetced = (int)cmd.ExecuteNonQuery();
18: return intRowsAffetced;
19: }
20: }
21: };
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. To
do this follow steps,
1: CREATE ASSEMBLY
2: LearnCLRIntegration -- Assembly name, you can write which you want
3: FROM
4: 'C:\LearnCLRIntegration.dll' -- write appropriate path and DLL name
5: WITH PERMISSION_SET = SAFE
6: Go
Here you can see your DLL and stored procedure in SQL server as shown
below,
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using Microsoft.Practices.EnterpriseLibrary.Data;
5: using Microsoft.Practices.EnterpriseLibrary.Common;
6: using System.Data.Common;
Conclusion:
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.