Вы находитесь на странице: 1из 1

When you can't find a built-in function that meets your needs, you can write you

r own. This fifth article continues Baya Pavliashvili's series on system-supplie


d functions by introducing you to various types of user-defined functions (UDFs)
. Learn about their syntax and situations in which each one is useful.
User-defined functions are a long-awaited and widely demanded addition to SQL Se
rver functionality. Previous articles in this series showed you the most useful
system supplied functions. When you can't find a built-in function that meets yo
ur needs, you can write your own. This article will introduce you to various typ
es of user-defined functions (UDFs), show you their syntax, and discuss situatio
ns in which each one is useful.
UDF Types
UDFs come in three flavors: scalar, in-line and multi-statement functions.
Scalar UDFs
Scalar UDFs return a single value. They are similar to built-in functions such a
s DB_NAME(), GETDATE(), or USER_ID(), which return a single string, date, or int
eger. The value returned by a scalar UDF can be based on the parameters passed,
although UDFs don't have to accept parameters.
Scalar UDFs can return any scalar system-supplied data type, except TIMESTAMP. Y
ou cannot return values with a user-defined data type from scalar UDFs. If you w
ant to return a value with a user-defined data type, you must specify the underl
ying system-supplied data type instead.
Scalar UDFs also prohibit returning values with non-scalar data types such as TA
BLE or CURSOR.
Scalar functions are excellent for lookups, such as returning the greatest quant
ity of sales for a particular title. Alternatively, scalar functions can be used
successfully for calculations: Determining a number of business days between tw
o dates would be an excellent task for a UDF, for instance.
In-line UDFs
In-line UDFs return a single row or multiple rows and can contain a single SELEC
T statement. Because in-line UDFs are limited to a single SELECT, they can't con
tain much logic. They can be effective, however, for lookups that return multipl
e values, such as the top five best-selling books with title, author, and public
ation date.
Multi-statement UDFs
Finally, the multi-statement UDFs can contain any number of statements that popu
late the table variable to be returned. Notice that although you can use INSERT,
UPDATE, and DELETE statements against the table variable being returned, a func
tion cannot modify data in permanent tables. Multi-statement UDFs come in handy
when you need to return a set of rows, but you can't enclose the logic for getti
ng this rowset in a single SELECT statement.
Reasons for using a multi-statement UDF, as opposed to an in-line UDF, can vary.
In some cases, the large number of joins against big tables (having more than 1
00,000 rows) warrants poor performance for a single SELECT statement. If this is
the case, you can bring the initial set of rows into the table variable with fe
wer joins. Then you could join the table variable to the rest of the tables in a
second SELECT statement. In other instances, you might need to perform conditio
nal logic and apply UPDATE or DELETE statements to the rows in the table variabl
e prior to returning the rowset to the user.