What is a Function?
What is a Function
• A function is a set of SQL statements that perform a specific task.
• Basically, it is a set of SQL statements that accept only input parameters, perform actions and return the result.
Function can return an only single value or a table.
• We can’t use a function to Insert, Update, Delete records in the database table(s).
• If you have to repeatedly write large SQL scripts to perform the same task, you can create a function that performs
that task.
• A function accepts inputs in the form of parameters and returns a value.
• Of course, you could create a stored procedure to group a set of SQL statements and execute them, however,
stored procedures cannot be called within SQL statements. Functions, on the other hand, can be.
Basic Syntax of Function
• CREATE FUNCTION [database_name.]function_name (parameters)
RETURNS data_type AS
BEGIN
SQL statements
RETURN value
END;
Built-In Functions
• SQL server adds some built-in functions to every database.
• Built-in functions are grouped into different types depending upon their functionality.
• Scalar Function: Scalar functions operate on a single value and return a single value
• upper('dotnet'), lower('DOTNET'), convert(int, 15.56)
• Aggregate Functions: Aggregate functions operate on a collection of values and return a single value.
• max(), min(), avg(), count()
• Date and Time Functions: Related to date and time
• GETDATE(), Datediff(), DateAdd(), Day(),Month(), Year()
User-Defined Functions
• These functions are created by the user in the system database or in a user-defined database.
• Scalar Function: The user-defined scalar function also returns a single value as a result of actions performed by
the function.
• Inline Table-Valued Function: The user-defined inline table-valued function returns a table variable as a result of
actions performed by the function.
• Multi-Statement Table-Valued Function: A user-defined multi-statement table-valued function returns a table
variable as a result of actions performed by the function. In this, a table variable must be explicitly declared and
defined whose value can be derived from multiple SQL statements.
Difference between Functions and Stored Procedures
• The function must return a value but in Stored Procedure it is optional. Even a procedure can return zero or n
values.
• Functions can have only input parameters for it whereas Procedures can have input or output parameters.
• Functions can be called from Procedure whereas Procedures cannot be called from a Function.
• The procedure allows SELECT as well as DML(INSERT/UPDATE/DELETE) statement in it whereas Function
allows only SELECT statement in it.
• Procedures cannot be utilized in a SELECT statement whereas Function can be embedded in a SELECT
statement.
• Stored Procedures cannot be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section
whereas Function can be.
• An exception can be handled by try-catch block in a Procedure whereas try-catch block cannot be used in a
Function.
• We can use Transactions in Procedure whereas we can't use Transactions in Function.