Introduction
We can import the .NET DLL or group of DLLs into SQL Server. Once the DLL is imported then, we can access the methods in the DLL inside the Stored Procedure, User-Defined function, or executed via TSQL. It is called CLR Assembly. In this article, we are going to explore how to create a custom CLR assembly and how to access it in the SQL Server.
CLR Assembly
- An assembly is DLL files that are written by one of the managed code languages hosted by the Microsoft.NET Framework common language runtime (CLR) and it can be used in the stored procedures, triggers, user-defined functions, etc.
- An assembly is a good option to expand the native functionality of the SQL server.
How to Create CLR Assembly
- Create the Class Library using C#
- Create Assembly from File
- Create User-Defined Function for Access the Assembly
- Invoke User-Defined Function
Create the Class Library using C#
Create Assembly from File
To create an assembly, we need to follow the below steps. Execute steps 1,2,3 queries are in the "master" database and step 4 and 5 in our database.
Step 1
Create Asymmetric Key from Assembly File.
Step 2
Create SQL Server Login linked to the Asymmetric Key.
Step 3
Grant UNSAFE assembly permission to the login created.
Step 4
Create a SQL Server database user for the SQL Server login created.
Step 5
Create CLR Assembly.
When creating an assembly in the MS SQLServer database, you can specify one of three different levels of security.
- SAFE
- EXTERNAL_ACCESS
- UNSAFE
SAFE is the default permission set and works for the majority of scenarios.
EXTERNAL_ACCESS addresses scenarios in which the code needs to access resources outside the server, such as files, network, registry, and environment variables.
UNSAFE code permission is for those situations in which an assembly is not verifiably safe or requires additional access to restricted resources, such as the Microsoft Win32 API.
Any one of the below conditions must be met for creating an EXTERNAL_ACCESS or UNSAFE assembly.
- The assembly is a strong name signed or Authenticode signed with a certificate. This strong name (or certificate) is created inside SQL Server as an asymmetric key (or certificate), and has a corresponding login with EXTERNAL ACCESS ASSEMBLY permission (for external access assemblies) or UNSAFE ASSEMBLY permission (for unsafe assemblies).
- The database owner (DBO) has EXTERNAL ACCESS ASSEMBLY (for EXTERNAL ACCESS assemblies) or UNSAFE ASSEMBLY (for UNSAFE assemblies) permission, and the database has the TRUSTWORTHY Database Property set to ON.
We have created the assembly with SAFE permission. So no need to disable the CLR Strict Security feature and enable the Database Trustworthy feature in the MS SQL Server.
Create User-Defined Function for Access the Assembly
Create the user-defined function for consuming the above assembly. I have created below "dbo.DateConvert" function.
Invoke User-Defined Function
Call the above user-defined function to consume the assembly for date conversion.
![Invoke User-Defined Function]()
If you want to drop an assembly, run the below query.
I hope you have liked this article and know about CLR assembly in MS SQL Server.