Creating A Function
Creating A Function
Introduction
In Transact-SQL, the primary formula of creating a function is:
For a function to be useful, it must produce a result. This is also said that the function returns a
result or a value. When creating a function, you must specify the type of value that the
function would return. To provide this information, after the name of the function, type the
RETURNS keyword followed by a definition for a data type. Here is a simple example:
CREATE FUNCTION Addition()
RETURNS Decimal(6,3)
After specifying the type of value that the function would return, you can create a body for the
function. The body of a function starts with the BEGIN and ends with the END keywords. Here is an
example:
END
Optionally, you can type the AS keyword before the BEGIN keyword:
END
Between the BEGIN and END keywords, which is the section that represents the body of the function,
you can define the assignment the function must perform. After performing this assignment, just
before the END keyword, you must specify the value that the function returns. This is done by typing
the RETURN keyword followed by an expression. A sample formula is:
Here is an example
CREATE FUNCTION GetFullName()
RETURNS varchar(100)
AS
BEGIN
RETURN 'Doe, John'
END
Function Calling
After a function has been created, you can use the value it returns. Using a function is also referred to
as calling it. To call a function, you must qualify its name. To do this, type the name of the database in
which it was created, followed by the period operator, followed by dbo, followed by the period
operator, followed by the name of the function, and its parentheses. The formula to use is:
DatabaseName.dbo.FunctionName()
Because a function returns a value, you can use that value as you see fit. For example, you can use
either PRINT or SELECT to display the function's value in a query window. Here is an example that
calls the above Addition() function:
PRINT Exercise.dbo.GetFullName();
As an alternative, to call a function, in the Object Explorer, right-click its name, position the mouse on
Script Function As, SELECT To, and click New Query Editor Window.
Renaming a Function
To rename a function, in the Object Explorer, right-click it and click Rename. Type the desired new
name and press Enter.
Modifying a Function
As mentioned already, in the body of the function, you define what the function is supposed to take
care of. As a minimum, a function can return a simple number, typed on the right side of the
RETURN keyword. Here is an example:
A Parameterized Function
To create a function that takes a parameter, specify a name and the type of value of the parameter(s) in
its parentheses. Here is an example:
When a function takes a parameter, in the body of the function, you can use the parameter as if you
knew its value, as long as you respect the type of that value. Here is an example:
Once again, in the body of the function, you can use the parameters as if you already knew their value.
You can also declare local variables and involve them with parameters as you see fit. Here is an
example:
When calling a function that takes more than one parameter, in the parentheses of the function, provide
a value for each parameter, in the exact order they appear in the parentheses of the function. Here is an
example:
You can also pass the names of already declared and initialized variables. Here is an example that calls
the above function: