Tutorial USD
Tutorial USD
Scalar Functions
Scalar functions return a single value of the type defined in the RETURNS clause. The body of the
function is between a BEGIN and END block.
Example 1
The following statement shows how to create a function that accepts two input parameters, sums
them together and then returns the sum to the calling statement.
The structure of the CREATE FUNCTION statement is fairly straightforward. You provide an object
name (udf_SumTwoValues), input parameters (@Val1 and @Val2), the type of data the function will
return () and the statement(s) the function executes are located between the BEGIN…END block. The
following SELECT statement calls the function. Note that the two-part name (owner.object_name) is
required when calling this function.
When the SELECT is executed, the input parameters 1 and 2 are added together and the sum 3 is
returned. You can use any values that either are, or can be, implicitly converted to an int data type
for the input parameters. Keep in mind, though, that only an int can be returned, so the following
statement will not produce the desired results.
The function returns a 3, which indicates the decimal portion of the parameters are truncated before
the calculation occurs.
Lecturer : Damien Kettle Database Administration
Example 2
Before you start this Example set up the SalesHistory table and load data into it:
DECLARE @i SMALLINT
SET @i = 1
SET @i = @i + 1
END
The script below defines a function named dbo.udf_GetProductSales that accepts three parameters
and returns a DECIMAL value. The function uses the three input parameters as criteria in calculating
the total sales from the SalesHistory table.
RETURN(@Sales)
END
Lecturer : Damien Kettle Database Administration
The script below calls the UDF created in the above script. Note: The schema the function belongs
to must be used in the call. In this case, the function belongs to the dbo schema.
Example 1
Use below DDL statement to create a Contacts table:
Use below DML statement to insert some data into the Contacts table:
Create the below User Defined Function by copying and pasting the SQL code into SQL Server
Management Studio and clicking Execute:
After creating this you will see the new UDF under the Functions -> Table-valued Functions folder:
This example receives a parameter called @Title. It returns a table containing selected fields from
the Contacts table where the JobTitle equals the @Title parameter value. You would call the
function like this:
Example 2
The following function accepts the particular product for which we were searching, along with the
SaleID from the SalesHistory table. From the function definition, you can see that the function
returns a table named @SalesTable that contains two columns: SalesTotal and SalesCount. The body
of the function inserts aggregate values into the @SalesTable table variable based upon the input
parameters.
BEGIN
RETURN
END
GO
The following code uses the APPLY operator to invoke the table-valued function with the values from
the SalesHistory table.