title | description | author | ms.author | ms.date | ms.service | ms.topic | helpviewer_keywords | ||
---|---|---|---|---|---|---|---|---|---|
Create CLR Functions |
Create CLR Functions |
rwestMSFT |
randolphwest |
06/28/2022 |
sql |
conceptual |
|
[!INCLUDE sqlserver2016]
You can create a database object inside an instance of [!INCLUDEssNoVersion] that is programmed in an assembly created in the [!INCLUDEmsCoName] [!INCLUDEdnprdnshort] common language runtime (CLR). Database objects that can use the rich programming model provided by the common language runtime include aggregate functions, functions, stored procedures, triggers, and types.
Creating a CLR function in [!INCLUDEssNoVersion] involves the following steps:
-
Define the function as a static method of a class in a language supported by the [!INCLUDEdnprdnshort]. For more information about how to program functions in the common language runtime, see CLR User-Defined Functions. Then, compile the class to build an assembly in the [!INCLUDEdnprdnshort] by using the appropriate language compiler.
-
Register the assembly in [!INCLUDEssNoVersion] by using the CREATE ASSEMBLY statement. For more information about assemblies in [!INCLUDEssNoVersion], see Assemblies (Database Engine).
-
Create the function that references the registered assembly by using the CREATE FUNCTION statement.
Executing CLR code is off by default in [!INCLUDEssNoVersion]. You can create, alter, and drop database objects that reference managed code modules, but these references won't execute in [!INCLUDEssNoVersion] unless you enable clr enabled option using sp_configure (Transact-SQL).
Deploying a SQL Server Project in [!INCLUDEmsCoName] [!INCLUDEvsprvs] registers an assembly in the database that was specified for the project. Deploying the project also creates CLR functions in the database for all methods annotated with the SqlFunction attribute. For more information, see Deploying CLR Database Objects.
CLR functions can be used to access external resources such as files, network resources, Web Services, other databases (including remote instances of [!INCLUDEssNoVersion]). This can be achieved by using various classes in the [!INCLUDEdnprdnshort], such as System.IO
, System.WebServices
, System.Sql
, and so on. The assembly that contains such functions should at least be configured with the EXTERNAL_ACCESS permission set for this purpose. For more information, see CREATE ASSEMBLY (Transact-SQL). The SQL Client Managed Provider can be used to access remote instances of [!INCLUDEssNoVersion]. However, loopback connections to the originating server aren't supported in CLR functions.
CLR functions can access native (unmanaged) code, such as code written in C or C++, via the use of PInvoke
from managed code (see Calling Native Functions from Managed Code for details). This can allow you to reuse legacy code as CLR UDFs, or write performance-critical UDFs in native code, and requires using an UNSAFE assembly. See CLR Integration Code Access Security for cautions about use of UNSAFE assemblies.