Registering a User-defined type in SQL Server

This is not new, it’s been around since SQL 2005, but I had never had to do it before.  If you installed the SQL server samples as part of your SQL client installation, you can find a CLR folder with all sorts of goodies, it will usually be installed at C:\Program Files\Microsoft SQL Server\90\Samples\Engine\Programmability\CLR.  Go to the UserDefinedDataType folder and open up the solution.  This will allow you to build it.  You might need to generate the strong name key, which you can easily do by opening up your VS2005 command prompt and typing: sn -k "C:\Program Files\Microsoft SQL Server\90\Samples\SampleKey.snk"
Ok, so now that that's our of the way, you need to load the CLR assembly onto SQL server:

CREATE ASSEMBLY ComplexNumberDll
FROM ‘C:\Program Files\Microsoft SQL Server\90\Samples\Engine\Programmability\CLR\UserDefinedDataType\CS\ComplexNumber\bin\debug\ComplexNumber.dll’

That adds an entry on the Assemblies list, you can get a list of all your registered assemblies like this:

SELECT * FROM sys.assemblies

And finally, to register your user-defined type:

CREATE TYPE ComplexNumber EXTERNAL NAME ComplexNumberDll.[Microsoft.Samples.SqlServer.ComplexNumber]

The square brackets are very important!!  [Assembly Name].[Fully-qualified type name]

Scroll to Top