User-defined functions
• Purpose: Encapsulate processing logic prescribed by the user;
specify a set of instructions used to process table data and other
queryable objects or variables in order to return a result in form of
a table or a scalar value.
• Represent stand-alone objects that can be reused (called)
inside definitions of other SQL Server objects by specifying
function name and input parameters, if applicable.
• May be called inside views, stored procedures and other
functions, which helps reducing code volume and leads to
better structured code.
• Recommended when a certain block of SQL code must be
used by multiple queries /views / procedures / functions.
• May be used to produce virtual tables that can be queried
similarly to database tables.
Types of user-defined functions
• Scalar-valued functions
o Return a single value represented by a scalar data type,
either built-in or user-defined
• Table-valued functions
o Return a set of records which emulate a virtual table
o Depending on function definition:
• Inline functions: return a table as a result of a single SQL
statement without requiring explicit definition of that
table;
• Multi-statement functions: require explicit definition of
the resulting table.
Scalar-valued functions
• Return a single value
• Accept multiple parameters
• Consist of 2 parts:
o Function header – Specifies function name, parameter list
and the data type of the result;
o Function body – Defines the internal logic responsible for
producing the result; it ends with the RETURN command
which provides the value or the expression representing
the result.
• May be called inside SQL statements in clauses
such as SELECT, WHERE , GROUP BY, HAVING or
ORDER BY.
Scalar-valued functions
• Creation:
• CREATE FUNCTION function_name ([@parameter_1 AS data_type],
. . . [@parameter_n AS data_type])
RETURNS data_type
AS
BEGIN
SQL commands
RETURN value/expression_to_return
END
• Alteration:
•
ALTER FUNCTION ...
• Removal:
• DROP FUNCTION ...
Scalar-valued functions - Example
Table-valued user defined functions
A. INLINE functions
• return a table, without explicitly defining its structure
• a single SELECT statement is the only accepted
contents
• BEGIN ….END structures are not allowed
• each field in the source query must have an explicit
name
• the columns returned by the function are provided by
the SELECT statement on which it relies
• The ORDER BY clause is only allowed when
accompanied by TOP
Table-valued user defined functions
A. INLINE functions – The syntax:
CREATE FUNCTION function_name ( [@parameter_1 AS data_type], …
[@parameter_n AS data_type] )
RETURNS TABLE
AS
RETURN (SQL_SELECT_Statement)
INLINE functions - Example
Table-valued functions
B. MULTI-STATEMENT functions:
• return a table as a result
• may encapsulate complex processing logic
• the structure of the resulting table must be explicitly
defined
Table-valued functions
B. MULTI-STATEMENT functions – The syntax:
CREATE FUNCTION function_name ([@parameter_1 AS date_type],
. . . [@parameter_n AS data_type])
Table variables
• Allocate memory space to store record sets defined
as virtual tables.
• Allow temporary storage of data organized in
multiple predefined fields so that it can be used for
various processing operations.
• Enable efficient data processing if subjected to
relational algebra operators.
• May by subjected to SELECT queries & support JOIN
operator relative to other tables.
• May be used in INSERT, UPDATE or DELETE statements.