--- title: "Create CLR Functions | Microsoft Docs" ms.custom: "" ms.date: "06/13/2017" ms.prod: "sql-server-2014" ms.reviewer: "" ms.technology: ms.topic: conceptual helpviewer_keywords: - "CLR functions [SQL Server]" - "user-defined functions [SQL Server], CLR" ms.assetid: a82df075-2243-4e19-bfe1-ae6d65dabd0f author: rothja ms.author: jroth manager: craigg --- # Create CLR Functions You can create a database object inside an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] that is programmed in an assembly created in the [!INCLUDE[msCoName](../../includes/msconame-md.md)] [!INCLUDE[dnprdnshort](../../includes/dnprdnshort-md.md)] common language runtime (CLR). Database objects that can leverage the rich programming model provided by the common language runtime include aggregate functions, functions, stored procedures, triggers, and types. Creating a CLR function in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] involves the following steps: - Define the function as a static method of a class in a language supported by the [!INCLUDE[dnprdnshort](../../includes/dnprdnshort-md.md)]. For more information about how to program functions in the common language runtime, see [CLR User-Defined Functions](../clr-integration-database-objects-user-defined-functions/clr-user-defined-functions.md). Then, compile the class to build an assembly in the [!INCLUDE[dnprdnshort](../../includes/dnprdnshort-md.md)] by using the appropriate language compiler. - Register the assembly in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] by using the CREATE ASSEMBLY statement. For more information about assemblies in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)], see [Assemblies (Database Engine)](../clr-integration/assemblies-database-engine.md). - Create the function that references the registered assembly by using the [CREATE FUNCTION](/sql/t-sql/statements/create-function-transact-sql) statement. > [!NOTE] > Deploying a SQL Server Project in [!INCLUDE[msCoName](../../includes/msconame-md.md)][!INCLUDE[vsprvs](../../includes/vsprvs-md.md)] 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-integration/deploying-clr-database-objects.md). > [!NOTE] > The ability of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] to execute CLR code is off by default. You can create, alter, and drop database objects that reference managed code modules, but these references will not execute in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] unless the [clr enabled Option](../../database-engine/configure-windows/clr-enabled-server-configuration-option.md) is enabled by using [sp_configure (Transact-SQL)](/sql/relational-databases/system-stored-procedures/sp-configure-transact-sql). ## Accessing External Resources CLR functions can be used to access external resources such as files, network resources, Web Services, other databases (including remote instances of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]). This can be achieved by using various classes in the [!INCLUDE[dnprdnshort](../../includes/dnprdnshort-md.md)], 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)](/sql/t-sql/statements/create-assembly-transact-sql). The SQL Client Managed Provider can be used to access remote instances of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. However, loopback connections to the originating server are not supported in CLR functions. **To create, modify, or drop assemblies in SQL Server** - [CREATE ASSEMBLY (Transact-SQL)](/sql/t-sql/statements/create-assembly-transact-sql) - [ALTER ASSEMBLY (Transact-SQL)](/sql/t-sql/statements/alter-assembly-transact-sql) - [DROP ASSEMBLY (Transact-SQL)](/sql/t-sql/statements/drop-assembly-transact-sql) **To create a CLR function** - [CREATE FUNCTION (Transact-SQL)](/sql/t-sql/statements/create-function-transact-sql) ## Accessing Native Code CLR functions can be used to 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](https://go.microsoft.com/fwlink/?LinkID=181929) for details). This can allow you to re-use legacy code as CLR UDFs, or write performance-critical UDFs in native code. This requires using an UNSAFE assembly. See [CLR Integration Code Access Security](../clr-integration/security/clr-integration-code-access-security.md) for cautions about use of UNSAFE assemblies. ## See Also [Create User-defined Functions (Database Engine)](create-user-defined-functions-database-engine.md) [Create User-defined Aggregates](create-user-defined-aggregates.md) [Execute User-defined Functions](execute-user-defined-functions.md) [View User-defined Functions](view-user-defined-functions.md) [Common Language Runtime (CLR) Integration Programming Concepts](../clr-integration/common-language-runtime-clr-integration-programming-concepts.md)