If you would like to hire my services, you can now do so by visiting the following link:
Website Design Darlington
Article Statistics
Code Bank Statistics
Summary: An example of how we can create a function in .NET and then use them in SQL Server.
According to Microsoft, SQL Server 2005 significantly enhances the database programming model by hosting the Microsoft .NET Framework 2.0 Common Language Runtime (CLR). This enables developers to write procedures, triggers, and functions in any of the CLR languages, particularly Microsoft Visual C# .NET, Microsoft Visual Basic .NET, and Microsoft Visual C++.How do we implement this functionality?In order to use CLR we have to: 1) Write a .NET class with a public function 2) Compile the class to a DLL 3) Register the DLL with SQL Server 4) Create a SQL Server function to access the .NET functionCreating an example function and DLLTo provide a simple example that you can use, we need to first create a new project. In Visual Studio, start a new Class Library project and call it SQLServerCLRTest. Then, create a new Class called CLRFunctions and add the following code: Public Class CLRFunctions Public Shared Function HelloWorld(ByVal Name As String) As String Return ("Hello " & Name) End Function End Class You'll notice that we have a simple function (which is Public Shared so that SQL Server can access it) which accepts a name as parameter and returns a simple message saying "Hello" to whichever name is passed in.Now, we need to create a DLL out of this project so that we can register it with SQL Server. The easiest way to do this, is to simply click the Build->Build Solution menu item in VS which will build the application and create the DLL. As your project will be in debug mode the DLL will usually be found at a location such as: C:\Documents and Settings\mark.smith\My Documents\Visual Studio 2005\Projects\SQLServerCLRTest\SQLServerCLRTest\bin\Debug\SQLServerCLRTest.dll Once you've located this DLL we can copy it over to our SQL Server machine or simply make a note of this path if that happens to be the same machine as our development machine.Turn on CLR functionalityBy default, CLR is disabled in SQL Server so to turn it on we need to run this command against our database: exec sp_configure 'clr enabled',1 reconfigure go Registering the DLLIn order to use the function we wrote, we first have to register the DLL with SQL Server. To do this we have to create an assembly, assign it a name and point the assembly at the DLL. Using the path to the DLL we created, run the following command against the database: CREATE ASSEMBLY asmHelloWorld FROM 'C:\SQLServerCLRTest.dll' Accessing our function from SQL ServerIn order to access our .NET function, we need to create a SQL Server function which makes use of the "EXTERNAL NAME" option which informs SQL Server that we will be using a CLR function. The function we will be creating will look like this: CREATE FUNCTION dbo.clrHelloWorld ( @name as nvarchar(200) ) RETURNS nvarchar(200) AS EXTERNAL NAME asmHelloWorld.[SQLServerCLRTest.CLRFunctions].HelloWorldThere's two things to note about the above function. The first is that we use an nvarchar which will be the equivilant of the .NET string (it will produce an error if you try to use a varchar and the second is the format of the "EXTERNAL NAME" path. From the reading I've done, the format should be:MyAssemblyName.MyClassName.MyMethodNameHowever, when I tried this I received an error from SQL Server stating that it couldn't find the type, so in order to get it to work I had to use the format:MyAssemblyName.[MyAssemblyName.MyClassName].MyMethodNameCalling the functionNow that we've registered our function, we should be able to call it by using the following statement: SELECT dbo.clrHelloWorld('Mark') Hopefully, when you run this code you'll get a simple "Hello Mark" returned in your results.This is obviously just an example to show you how to implement CLR, but to create something useful you could look at creating functions that deal with Regular Expressions, interact with the File System or Registry, send emails or simply access any resources that are located outside of SQL Server.
Posted on 17/05/2007 02:34:28
1. Jason 19/05/2007 05:39:48
The new CLR stuff in SQL can be really useful. Unfortunately very few of the example articles mention some of the caveats:* If you create a UDF with CLR and reference that UDF from a T-SQL View or Stored Proc it's challenging to update and re-deploy the CLR project.* When a T-SQL Stored Proc queries a table or view, it uses the permissions of the user who created the stored proc. When a CLR proc queries a table or view, it uses the permissions of the users calling the CLR proc.I had to learn these the hard way.
2. Mark Smith 19/05/2007 06:33:00
Hi Jason,Thanks for your comments, I hope they come in useful for other people reading this.Can you expand on #1 though? What do you mean by "it's challenging to update and re-deploy the CLR project" and what problems did you face?Thanks,Mark
3. Rani 19/05/2007 23:09:21
Jason useful commentsMark Nice articleGuys I had a question in mind when MMSQL is loading the assembly in memory during execution, is it going into cashing of SQL Server, so if i have a heavy weight assembly will every call kill my server performance
4. Mark Smith 22/05/2007 00:34:32
Hi Rani,Thanks for your comment. To be honest, I'm not sure how SQL Server handles assemblies with regards to memory, but if you ask your question in the Tek Tips SQL Server Forum then I'm sure they will be able to answer you.Thanks,Mark
5. Dimitar Voynov 06/06/2007 11:00:47
Hei, I liked the article. Thanks!
6. Prawin 06/06/2007 22:08:15
Nice Article thnx for the Info
7. Asif Nadeem 07/06/2007 04:43:47
very good articlekeep it up man!
8. praveen 07/06/2007 05:34:55
really good!
9. Voltaire SR 13/06/2007 22:20:02
Very nice article.
10. Steven Bey 09/08/2007 02:47:43
I like the idea but what are the performance implications of using an external assembly?
11. Mark Smith 13/08/2007 07:14:34
Steven,Managed code has a decisive performance advantage over T-SQL with respect to most procedural computations, but for data access T-SQL generally fares better. Therefore, a good general rule is that computation and logic intensive code is a better choice for implementation in the CLR, whereas data access intensive code is best completed using T-SQL.Thanks,Mark
12. lauri bellamy 24/04/2008 23:57:08
Very good article. This is exactly what I needed.
13. Perry 21/05/2008 16:08:34
(Fifth attempt to comment; hopefully 5 is the lucky number this week.)Does anyone know how to load other system assemblies in order to use them from one's own custom assembly? The list of preloaded system assemblies in SQL Server 2005 is very short.
14. Abbass Mustafa 25/09/2008 00:54:41
Am a Software Engineer in Softflow Company/ Beirut.This article is very very useful.Thanks,
15. Nate 05/02/2009 17:14:51
FYI, I do not agree with the first user's claim that it's hard to make changes. I am using visual studio 2008 on the same machine with the sql server. I make changes like this...1. Recompile the library in visual studio2. Run this in query analyzer:ALTER ASSEMBLY asmHelloWorld from 'C:\Users\Nate\Documents\Visual Studio 2008\Projects\SQLServerCLRTest\SQLServerCLRTest\bin\Debug\SQLServerCLRTest.dll'GOI just tested it by changing the hello Mark response to 'Hello 2 Mark'.
Please keep your comments relevant to this page. Any inappropriate or purely promotional comments may be removed. Email addresses are never displayed but are required so you can confirm your comments.