Home > Articles > Data > SQL Server

SQL Server Reference Guide

Hosted by

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

An Introduction to the CLR in SQL Server 2005

Last updated Mar 28, 2003.

Since the inception of Microsoft's Relational Database Management System (RDBMS) called SQL Server, there has been a separation of the code for the developers. The Transact-SQL (T-SQL) language is used for any data access or initial manipulation. In many of the tutorials here on Informit, I've focused on teaching you the basics of these statements. For any manipulation that requires more sophisticated operations or that contained complex error handling logic, developers use a high-level language such as C++, Visual Basic, Java or C#. The basic coding process involves creating a data access layer in the code to view, add, delete or change data and then a "wrapper" program presented the data to the user and allows the developer to deal with further data logic.

As time progressed, many developers started to place more and more logic inside the database using stored procedures, triggers and other programs that run on the database server, often called "server-side" code. This causes a decision about where to put the logic for a particular process. I've even devoted an entire article to that topic.

Even with stored procedures, a developer is limited to using T-SQL code, and by definition T-SQL is a procedural language. What that means is each line of code in a process is run one after another, with only a limited ability to "jump" from one line of code to another, or move backwards in code based on a condition. Procedural languages also often have weak error-trapping mechanisms.

Beginning in SQL Server 2005, Microsoft has added an entirely new layer to the code arsenal for SQL Server, called the Common Language Runtime (CLR). It's based on the .NET architecture, and opens up new options for code design and logic placement. In this tutorial, I'll give you the basics of this new methodology and show you a simple example that you can try on your system.

Let's take a minute and examine Microsoft's new programming methodology and how it is linked into SQL Server 2005. To do that, we need to back up a little and talk about how programs in general work. Forgive me if I get a little too basic here, but I need to be clear about why the CLR is a different kind of programming construct. I'll also sacrifice some accuracy so that we don't spend the entire article talking about computer architecture.

A computer is basically a series of on and off switches, just like those giant signs made up of light bulbs. By switching the light bulbs on or off in those large signs, you can create a message. In a computer, setting the millions of switches off and on routes electrons around to create bits of data which are used to create e-mails, light up the screen in a certain way or store something on a hard drive. This on and off capability is why a computer is a binary (on or off) device. Most of us are familiar with all this already, but I've found a few DBAs or developers that aren't as familiar with how things work after that.

Setting these binary switches is the job of the code called "Machine Language." This is a series of 1's and 0's that set the switches. Of course, most of us don't code in machine language, so the next level up is called an "Assembler." This type of code is slightly more readable, but still quite complex. To make code easier to work with, we use a high-level language, such as C++ or Visual Basic. These languages are fairly easy to read and understand, and are based on "objects" instead of line references. Because the programming is based on objects, you have a great deal of flexibility in branching, referencing and error handling.

There are actually two types of higher level languages. The first is called "interpreted." An interpreted language requires another program (the interpreter) that listens for the code to run, and then runs each line. If you remember the old BASICA programs, you'll remember that you first run the BASICA interpreter and then load and run the lines of BASIC code that it uses. In modern times, Java is a language that needs a "runtime" environment to work, which is a type of interpreter. Unfortunately these programs often run slowly because there are extra layers to deal with.

The other type of programming is called "compiled," because the code is "bundled up" together into an executable program which doesn't require anything other than an operating system to run.

Whether compiled or interpreted, these languages are distilled into Assembly language and then on to Machine language. This design has worked for decades, but it isn't without problems. For instance, if a developer writes a program in C++, that code isn't able to be interpreted or compiled by another language. Over time the underlying mechanisms change, so periodically older code won't run any more on a newer system. But the largest problem is that code can sometimes act in such a way that it causes a system to become unstable or allows unauthorized code to run.

In the Microsoft .NET architecture, another layer was added called the "Common Language Runtime," or CLR. It's a mix of an interpreted and compiled environment, so that it provides both speed and language independence. That means you can write a program in any of the .NET languages, such as Visual Basic.NET or C#, and it is interpreted/compiled into an intermediate language. That intermediate language is compiled to run quickly and safely on the Windows operating system.

Now we'll tie all this back to SQL Server 2005. Microsoft has included the CLR for the Database Engine, so you can code really complicated data logic and then call that logic from within T-SQL code. The code is protected from crashing the system by the CLR, where it is compiled and natively understood by the Operating System.

So now that we have the background of what is going on when we include and run a CLR process, the next step is to understand when you might choose to create a CLR procedure in your database. You can use a CLR reference anywhere you might use a stored procedure. Remember, with an object-based programming set, you have better referencing and error handling.

Not only can you use a CLR where you might use a T-SQL procedure, but you can also use them where you use T-SQL in a Function, Trigger or even to aggregate data. I'll show you how to do that in future tutorials.

A CLR has one more use: data types. SQL Server 2005 includes a huge array of data types, such as integers, character stings, date and time formats and more. But just like examples, you can never have enough types. There are always times when a data requirement calls for something other than what the database provides. Using the CLR, you can actually declare some code that returns a value as a data type.

To create a CLR procedure, you'll need to code in one of the .NET languages. Once you've written your code, you compile it into a Dynamic Link Library (DLL) file. Then you switch to the database and create an "Assembly," which is a reference to the DLL. When you create the Assembly you set the safety level, which essentially details what it is capable of, such as working with the Operating System or the network. After that you're able to call the procedure directly in your T-SQL code.

There are a lot of variables here, so let's start with a simple example of a stored procedure that returns ten percent of a number using C# code. To try this you'll need to do a few things first. The default for SQL Server is to turn off the ability to run a CLR process, so you'll need to run the SQL Server Surface Area Configuration tool (SAC) and select Surface Area Configuration for Features. Once inside, find the CLR Integration selection and check it. Click OK and close the tool.

Now you'll need to create the DLL code. One of the easiest ways to do that is with Visual Studio 2005. Open that tool (you can download the Express versions from Microsoft here) and create a new SQL Server Project. In the .NET world, this brings in all the constructs you need to create the assembly. Once you download that programming environment, you can create a SQL Server project.

Once you create the project and supply all the credentials to attach to the server, then click Add New Item from the Project menu, and make it a Stored Procedure type. Give it the name usp_clrReturnTenPercent.

Now type this code in the main window:

using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class UserDefinedFunctions
{
 [SqlFunction()]
 public static SqlDouble usp_clrReturnTenPercent(SqlDouble originalValue)
 {
 SqlDouble newValue = originalValue * .1;
 return newValue;
 }
}

Then from the Build menu click Build, and when that finishes click Deploy from the Build menu. You can also use the CREATE ASSEMBLY command in T-SQL, but if you're in the Visual Studio environment, the menu commands are easier. I'll explain a more manual process later.

Your project is now built and deployed into SQL Server. Now you're able to call that stored procedure with the following T-SQL code:

EXEC usp_clrReturnTenPercent(100)
GO

Of course there is a lot more to talk about in the Safety modes, working with data, and what you can and can't do in the CLR layer. I'll cover those in future tutorials.

Informit Articles and Sample Chapters

There's more about .NET right here on Informit.

Online Resources

This is a very basic tutorial. If you'd like to learn more about the CLR layer, check out this article.