Scalar Functions
A scalar function is like the standard built-in functions provided with SQL Server. It returns a single scalar value that can be used anywhere a constant expression can be used in a query. (You saw an example of this in the earlier description of the getonlydate()
function.)
A scalar function typically takes one or more arguments and returns a value of a specified data type. Every T-SQL function must return a result using the RETURN
statement. The value to be returned can be contained in a local variable defined within the function, or the value can be computed in the RETURN
statement. The following two functions are variations of a function that returns the average price for a specified type of book from the titles
table:
Get Microsoft® SQL Server 2008 R2 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.