
Invoking User-defined Functions That Return a Scalar Value
You can invoke a user-defined function that returns a scalar value anywhere that a scalar expression of the same data type is allowed in Transact-SQL statements. Scalar-valued functions must be invoked by using at least the two-part name of the function. For more information about multipart names, see Transact-SQL Syntax Conventions (Transact-SQL).
Queries
User-defined functions that return scalar values are allowed in these locations:
-
As an expression in the select_list of a SELECT statement:
USE AdventureWorks;
GO
SELECT ProductID, ListPrice, dbo.ufnGetProductDealerPrice(ProductID, StartDate) AS DealerPrice,
StartDate, EndDate
FROM Production.ProductListPriceHistory
WHERE ListPrice > .0000
ORDER BY ProductID, StartDate;
GO
-
As an expression or string_expression in a WHERE or HAVING clause predicate:
USE AdventureWorks;
GO
SELECT ProductID, ListPrice, StartDate, EndDate
FROM Production.ProductListPriceHistory
WHERE dbo.ufnGetProductDealerPrice(ProductID, StartDate) > .0000
ORDER BY ProductID, StartDate;
GO
-
As a group_by_expression in a GROUP BY clause.
-
As an order_by_expression in an ORDER BY clause.
-
As an expression in the SET clause in an UPDATE statement:
USE AdventureWorks;
GO
UPDATE Production.ProductListPriceHistory
SET ListPrice = dbo.ufnGetProductDealerPrice(ProductID, StartDate)
WHERE ProductID > 900;
GO
-
As an expression in the VALUES clause of an INSERT statement:
User-defined functions referenced in these locations are logically executed one time per row.
CHECK Constraints
User-defined functions that return scalar values can be invoked in CHECK constraints if the argument values passed to the function reference columns only in the table or constants. Every time the query processor checks the constraint, the query processor calls the function with the argument values associated with the current row being checked. The owner of a table must also be the owner of the user-defined function invoked by a CHECK constraint on the table.
DEFAULT Definitions
User-defined functions can be invoked as the constant_expression of DEFAULT definitions if the argument values passed to the function only contain constants. The owner of the table must also be the owner of the user-defined function invoked by a DEFAULT definition on the table.
Computed Columns
Functions can be invoked by computed columns if the argument values passed to the function reference only columns in the table or constants. The owner of the table must also be the owner of the user-defined function invoked by a computed column in the table.
Assignment Operators
Assignment operators (left_operand = right_operand) can invoke user-defined functions that return a scalar value in the expression specified as the right operand.
Control-of-Flow Statements
User-defined functions that return scalar values can be invoked by control-of-flow statements in their Boolean expressions.
CASE Expressions
User-defined functions that return a scalar value can be invoked in any of the CASE expressions.
PRINT Statements
User-defined functions that return a character string can be invoked as the string_expr expression of PRINT statements.
Functions and Stored Procedures
-
Function arguments can also be a reference to a user-defined function that returns a scalar value.
-
RETURN integer_expression statements in stored procedures can invoke user-defined functions that return an integer as the integer_expression.
-
RETURN return_type_spec statements in user-defined functions can invoke user-defined functions that return a scalar data type such as the return_type_spec, provided the value returned by the invoked user-defined function can be implicitly converted to the return data type of the invoking function.