Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
SQL Server 2016 (13.x) and later versions
You can create a database object inside SQL Server that is programmed in a CLR assembly. Database objects that can use the rich programming model provided by the CLR include triggers, stored procedures, functions, aggregate functions, and types.
Like the built-in aggregate functions provided in Transact-SQL, user-defined aggregate functions perform a calculation on a set of values and return a single value.
Creating a user-defined aggregate function in SQL Server involves the following steps:
Define the user-defined aggregate function as a class in a Microsoft .NET Framework-supported language. For more information about how to program user-defined aggregates in the CLR, see CLR user-defined aggregates. Compile this class to build a CLR assembly using the appropriate language compiler.
Register the assembly in SQL Server using the CREATE ASSEMBLY
statement. For more information about assemblies in SQL Server, see Assemblies (Database Engine).
Create the user-defined aggregate that references the registered assembly using the CREATE AGGREGATE
statement.
Executing CLR code is off by default in SQL Server. You can create, alter, and drop database objects that reference managed code modules, but these references don't execute in SQL Server, unless the clr enabled server configuration option is enabled by using sp_configure.
Deploying a SQL Server Project in Microsoft Visual Studio 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 Deploy CLR database objects.
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayTraining
Module
Create stored procedures and user-defined functions - Training
This content is a part of Create stored procedures and user-defined functions.
Documentation
Create User-defined Functions (Database Engine) - SQL Server
Create User-defined Functions (Database Engine)
User-defined functions - SQL Server
User-defined functions are routines that accept parameters, perform an action, and return the result as a single scalar value or a result set.
CREATE FUNCTION (Transact-SQL) - SQL Server
CREATE FUNCTION (Transact-SQL)