What is the correct syntax to create an inline scalar function in SQL Server?
Books Online, in the Types of Functions chapter (2005 and up), talks about Inline Scalar Functions as if they exist and as if no BEGIN...END
block is required (in contrast with multiline functions):
For an inline scalar function, there is no function body; the scalar value is the result of a single statement. For a multistatement scalar function, the function body, defined in a BEGIN...END block, contains a series of Transact-SQL statements that return the single value.
I also noticed a row for "IS: inline scalar function" in the list of object types in the spt_values table:
SELECT name
FROM master..spt_values
WHERE type = 'O9T'
AND name LIKE '%function%'
I have tried to create such a function with no success:
CREATE FUNCTION AddOne(@n int) RETURNS int
AS
RETURN @n + 1
The error message is
Msg 102, Level 15, State 31, Procedure AddOne, Line 3 Incorrect syntax near 'RETURN'.
Am I missing something or is there an error in Books Online?
A scalar function returns a single value. It might not even be related to tables in your database. A tabled-valued function returns your specified columns for rows in your table meeting your selection criteria.
If a table value function could be said to work like a stored procedure, an inline function is similar to a view. This means that an inline function can only contain a single SELECT statement, and the columns in the SELECT statement implicitly define the columns of the returned table set of the function.
In SQL Server, a scalar UDF, or scalar user-defined function, is a user-defined function that returns a single value. This is in contrast to a table-valued function, which returns a result set in the form of a table. User-defined functions can be written as either T-SQL UDFs or CLR (Common Language Runtime) UDFs.
Well, AFAIK, none exist (not even in the hidden [mssqlsystemresource] database) and there's no syntax to create one. So it appears that this is something that Microsoft must have anticipated in the run-up to SQL Server 2005 by adding a type for it (and doc!), but never actually implemented for some reason.
Though it is one of the single most requested features for all of Ms Sql Server. Primarily because the default UDF's are so slow and we end up having to back-end ITVF's to get the same effect. (difficult and clumsy, but it works).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With