We usually face problem in Stored Procedures and other database object when we need to implement some complicated logic within it. We found inefficient performance when we try to implement complex logic & business rules in database objects. In many cases we found C# or VB classes more powerful to implement such things. Microsoft has launched a new concept to resolve such issues with SQL server 2005 called "CLR Stored Procedure".
What is CLR Stored Procedure?
Now, let us understand CLR stored procedure. CLR as most of .Net programmer knows is Common Language Runtime and Stored Procedures are routine stored procedures of database. Thus, CLR Stored Procedures are combination of both. As we all know, Common Language Runtime is core .Net component. The Common Language Runtime is runtime execution environment which supplies managed code with various services like cross language integration, code access security, lifetime management of object, resources management, threading, debugging & type safety etc. So now, CLR Stored Procedures are .Net objects which run in the memory of database.
The very first usage of CLR Stored Procedures can be said accessing system resources. Accessing system resources could also be done using Extended Stored Procedures which are again database object like Stored Procedures, Functions etc. Extended Stored Procedures can do most of the things which a standard executable program can do. Then, why are CLR Stored Procedures? The very first advantage of CLR Stored Procedures is it is a managed object unlike Extended Stored Procedures, which are unmanaged objects. The common thing between them is both runs under database memory. In this way CLR Stored Procedures gives all the benefits of managed objects. Following screen explains memory allocation while execution of CLR Stored Procedure.
When should I use CLR Stored Procedure?
Extended stored procedures run in the same process space as the database engine, memory leaks, bugs etc., can affects the performance of database engine. CLR stored procedures resolves these issues as they are managed object and runs per specifications of Common Language Runtime. CLR Stored Procedures can replace a standard stored procedure that contains complex logic and business rules. CLR Stored Procedures takes benefit of .Net classes and thus makes easy to implement complex logic, calculation, intense string operations, complex iterations, data encryptions etc., that are difficult to obtain in standard stored procedures. Standard stored procedures are still best for data oriented tasks. CLR Stored Procedures not only includes stored procedures but also includes Functions, Triggers etc. CLR Stored Procedures are compiled one so gives better performance.
Benefits of CLR Stored Procedures:
- Gives better results while executing complex logic, intense string operation or string manipulations, cryptography, accessing system resources and file management etc.
- CLR Stored Procedures are managed codes so ensures type safety, memory management etc.
- Better code management and provides object oriented programming capability thus enables encapsulation, polymorphism & inheritance.
- Convenient for programmer as CLR Stored Procedures can be written in C#, VB or any other language that .Net Framework supports.
- CLR Stored Procedures can also be used with Oracle 10g Release 2 or later versions.
Drawbacks of CLR Stored Procedures:
- Not convenient in all contexts for e.g. it should not be used to execute simple queries. In that case standard stored procedures give better results.
- Deployment may be difficult in some scenarios.
Standard Stored Procedures vs. CLR Stored Procedures:You are the best judge when to use regular Stored Procedures and when to use CLR Stored Procedures. CLR Stored Procedures can be used in following scenarios.
- When the program requires complex logic or business rules.
- When the flow is CPU intensive. CLR Stored Procedures gives better results as they are in complied form and managed one.
- The tasks which are not possible in TSQL, accessing system resources, cryptography, accessing web services etc.
- In option of Extended Stored Procedures. One should always consider CLR Stored Procedures before going for Extended Stored Procedures.
- An operation requires higher data safety.
Creating CLR Stored Procedure step by step:Let us create one simple CLR Stored Procedure which fetches all the rows from one table of the database. I have listed all SQL statement used for creating database, creating table, inserting dummy records in the table etc., under "SQL statements used in the demo" section. Application development specification:
- IDE: Visual Studio 2008
- Framework: 3.5 with SP 1
- Language: C# 3.0
- Database MS SQL Server 2005 Express edition
2) You can choose reference of existing database connection or click on Add New Reference.
3) If you selects from existing references skit step 3 else add new database reference as shown in following image and click on Test Connection to test the connection.
4) On clicking on OK button, Visual Studio will ask you to enable SQL/CLR debugging on the selected connection. You can select "Yes" to enable debugging or "No" to disable the same.
5) Once the database reference and debugging option is selected, the project will be displayed in Solution Explorer. Select the project and right click on Solution Explorer >> Click on Add >> Stored Procedure.6) Add new procedure from the installed templates as shown in following screen. Give proper name to it.
7) Once you select the template, it will create .cs file with the content shown in following image.
8 ) Add following code in the method already created. Pass "context connection=true" as connection string in the constructor while creating new SqlConnection. This CLR stored procedure is going to be the part of the database, so it will be the internal part of database so no need to connect database externally. So, no need to provide connection string that we usually provide in applications. Then Click on Build menu >> Click on Build Solution. Also click on Build menu >> Deploy solution. This will deploy the assembly to the database for which we have made connection initially.
9) Now, select the database >> Programmability. Right click on Stored Procedures >> Click on Refresh. The list of Stored Procedures should show one newly added stored procedure. Also right click on Assemblies >> click on Refresh. This should show newly added Assembly. Also, enable CLR Stored Procedure by following query.
sp_configure 'clr enabled', 1
Run following query to take effect or above query.
Now, execute the stored procedure. It should give similar results shown in following screen.