Click here to Skip to main content
Click here to Skip to main content

SQL CLR Functions

, 1 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
SQL CLR functions


In this tip, we are going to create an assembly using .NET managed code to encrypt and decrypt a string. After that, we will use the same assembly to create a CLR function in SQL server as a scalar function.

We can create a function within SQL Server that depends on a SQL assembly which itself is compiled using any of the .NET framework Common Language Runtime (CLR) managed code.

Beginning with SQL Server 2005, we can write user-defined functions which are of scalar (which returns single value) and table-valued function types. However, in this blog post, we are dealing with Scalar type CLR functions.

T-SQL has lot of inbuilt functions and features. However to custom our own complex logic, we use any CLR managed code like C# or VB.NET, etc., and incorporate it in SQL environment.

Here are the steps to create Scalar CLR functions:

Create a project of type "Class Library" using Visual Studio.

Add your static methods. In our case "Encrypt" and "Decrypt” methods.

Specify "SqlFunction()" attribute to all the functions that can be accessed from SQL Server function/stored procedure.

Compile & build the application in "Release" mode to get the assembly (.dll)

Now go to SQL Server MS; select your database and create New Query and execute the following statements below to enable CLR:

sp_configure 'clr enabled', 1;

If you encounter any compatibility level errors, then check to see your database compatibility level using:


If it is set to 100 or above, execute the following statement to set it to 90.

sp_dbcmptlevel 'SQLCLR', 90 

Before adding the 'DLL' to the SQL assemblies, you need to set the database to trustworthy. This can be used to reduce threats that can exist as a result of attaching a database that contains (malicious) assemblies with an EXTERNAL_ACCESS or UNSAFE permission setting.:


Now expand your database node to go to Assemblies located within "Programmability" and create new assembly.

Choose the assembly file path and set the permissions for assembly owner.
(Note: In case you get any errors, check the steps #5, #6 and #7.)

Later to access the external CLR functions from within assembly, we need to create a function in SQL Server. Execute the following query to create external names Encrypt and Decrypt functions:

CREATE FUNCTION [dbo].Encrypt(@Input nvarchar(max)) RETURNS nvarchar(max)
CREATE FUNCTION [dbo].Decrypt(@Input nvarchar(max)) RETURNS nvarchar(max)

When everything is ready; use the following query to Encrypt the given string:

Select dbo.Encrypt('Hello World') 

and use function to Decrypt the given encrypted string:

Select dbo.Decrypt('i9E2KOEoT7D+Doc2CBdjDA==') 

This can be used to encrypt passwords, credit card details and other sensitive information within SQL without depending on the code. Visit MSDN to know further about CLR.

Points of Interest

Make sure you reconfigure your database to enable CLR.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sreekanth Mothukuru
Software Developer (Senior)
India India
No Biography provided
Follow on   Twitter

Comments and Discussions

GeneralMy vote of 5 PinprofessionalCatchExAs14-Jul-14 12:45 
General[My vote of 2] Feedback PinmemberMalte Klena2-Jul-14 5:02 
QuestionWould it be wise to have the encrypt/decrypt function on the DB? PinmemberAshman7862-Jul-14 0:53 
AnswerRe: Would it be wise to have the encrypt/decrypt function on the DB? PinmemberM. Sreekanth3-Jul-14 20:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 1 Jul 2014
Article Copyright 2014 by Sreekanth Mothukuru
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid