Back in 1999, or possibly early 2000, I was told a rumor regarding the upcoming version of SQL Server. The rumor was that there was some experimenting going on at Microsoft, regarding the possibilities of using VBScript to write stored procedures in SQL Server. Supposedly there was a chance that this feature would be available in Shiloh (the codename for SQL Server 2000). A couple of months later the beta of SQL Server 2000 came, and then the final product, and as we all know there were no possibility to use VBScript to write stored procedures. Even though the person who told me this was definitely in a position to know about these things, I do not know exactly what truth there was to this rumor. And if it was correct I still have no idea how far this feature came.
Since then we have been waiting for a long time for a new version of SQL Server. And in November it will finally be available, the final version of SQL Server 2005 (also known as Yukon). The first official beta of Yukon was available two years ago, and since then there has been another beta and several Community Technical Preview (CTP) versions. For each version more and more people have previewed the product, and the hype and interest keeps on building. And in the middle of all this interest there is one feature --called CLR Integration or SQLCLR for short-- that is more discussed than any other. In SQL Server 2005 we will no longer be restricted to writing procedures in T-SQL; we can now use any .Net language to create database programmability objects! This is nothing short of a revolution in the SQL Server world, and many say that the complexity of this feature is the main reason why Yukon has taken so long to complete.
However, Microsoft did not add CLR Integration to SQL Server just because they had some extra time on their hands and thought it might be cool. The reality is that there are scenarios where a managed .Net procedure will perform better than one written in T-SQL. And it is definitely easier to write better code with a modern programming language such as C# than what we write with T-SQL (even though T-SQL is getting some nice new features in this area in SQL Server 2005). Many application developers who are also database developers will be interested in taking advantage of this. Visual Studio 2005 will have built-in integration with SQL Server 2005 for creating SQLCLR programmability objects. The developer can do all his work in his preferred development environment, so there is another reason for him to implement functionality with SQLCLR instead of T-SQL. But as you had probably guessed it is not as simple as just choosing between SQLCLR and T-SQL based on your preferences and skills. While I did say above that there are times when SQLCLR will perform better, there are definitely lots of scenarios where T-SQL is better. The integration of CLR brings nothing new to how data in a relational database is best managed. Declarative set-based DML always (or close to it) outperforms procedural processing here. One of the biggest concerns DBAs have regarding CLR Integration is that it will open up the doors to SQL Server for a hoard of programmers who know nothing about data management. I do not think that will happen though, why should everyone start programming database functionality just because they recognize the syntax better? But still, there is a quite possible risk that we will see a lot more procedural code, processing data row-by-row. We will also start seeing more or less exotic uses of SQL Server, such as procedures calling out to web services or working with the file system. The richness of the .Net Framework brings a lot of useful functionality, but it also means it will be much easier to do wrong --or at least questionable-- things in database code.
My personal opinion is that CLR Integration is an interesting new tool that can be helpful in some situations, but should definitely be used with care. I do not see it as the most important new feature in SQL Server 2005 (far from it), but it probably is the one that needs the most coverage to make sure that anyone who will be using it knows as much as possible about it. Developers will need to know what effects their .Net implementations will have in SQL Server, and DBAs will probably want to be able to review code that is being executed in their databases. This article is therefore the start of a series that I will publish on this matter. In the upcoming parts I will describe how CLR Integration works, what you can do with it (and of course how to do those things) and when you should or should not use it. To end this part, and start looking at CLR Integration, I will show an example of a useful user-defined scalar function implemented in C# and how to use it in SQL Server 2005.
using System.Data.SqlTypes; using System.Text.RegularExpressions; using Microsoft.SqlServer.Server; public partial class UserDefinedFunctions { [Microsoft.SqlServer.Server.SqlFunction] public static SqlBoolean RegExValidate( SqlString expressionToValidate, SqlString regularExpression) { Regex regex = new Regex(regularExpression.Value); return regex.IsMatch(expressionToValidate.Value); } }
DECLARE @regex NVARCHAR(100) SET @regex = N'^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$'
SELECT name, emailaddress, dbo.RegExValidate( emailaddress, @regex ) AS validemail FROM dbo.PEOPLE
CREATE TABLE dbo.PEOPLE ( name VARCHAR(25) NOT NULL , emailaddress VARCHAR(255) NOT NULL , CONSTRAINT PEOPLE_ck_validemailaddress CHECK ( dbo.RegExValidate( emailaddress, N'^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$' ) = 1 ) )
To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.
Subscribing to our newsletters gets you:
Steve Jones Editor, SQLServerCentral.com