Function is a database object in Sql Server. Basically it is a set of sql statements that accepts only input parameters, perform actions and return the result. Function can return only single value or a table. We can’t use function to Insert, Update, Delete records in the database table(s).
These functions are defined by Sql Server for different purpose. We have two types of system defined function in Sql Server
Scalar functions operates on a single value and returns a single value. Below is the list of some useful Sql Server Scalar functions.
Aggregate functions operates on a collection of values and returns a single value. Below is the list of some useful Sql Server Aggregate functions.
These functions are created by user in system database or in user defined database. We three types of user defined functions.
User defined scalar function also returns single value as a result of actions perform by function. We return any datatype value from function.
--Create a table CREATE TABLE Employee ( EmpID int PRIMARY KEY, FirstName varchar(50) NULL, LastName varchar(50) NULL, Salary int NULL, Address varchar(100) NULL, ) --Insert Data Insert into Employee(EmpID,FirstName,LastName,Salary,Address) Values(1,'Mohan','Chauahn',22000,'Delhi'); Insert into Employee(EmpID,FirstName,LastName,Salary,Address) Values(2,'Asif','Khan',15000,'Delhi'); Insert into Employee(EmpID,FirstName,LastName,Salary,Address) Values(3,'Bhuvnesh','Shakya',19000,'Noida'); Insert into Employee(EmpID,FirstName,LastName,Salary,Address) Values(4,'Deepak','Kumar',19000,'Noida'); --See created table Select * from Employee
--Create function to get emp full name Create function fnGetEmpFullName ( @FirstName varchar(50), @LastName varchar(50) ) returns varchar(101) As Begin return (Select @FirstName + ' '+ @LastName); end
--Now call the above created function Select dbo.fnGetEmpFullName(FirstName,LastName) as Name, Salary from Employee
User defined inline table-valued function returns a table variable as a result of actions perform by function. The value of table variable should be derived from a single SELECT statement.
--Create function to get employees Create function fnGetEmployee() returns Table As return (Select * from Employee)
--Now call the above created function Select * from fnGetEmployee()
User defined multi-statement table-valued function returns a table variable as a result of actions perform by function. In this a table variable must be explicitly declared and defined whose value can be derived from a multiple sql statements.
--Create function to EmpID,FirstName and Salary of Employee Create function fnGetMulEmployee() returns @Emp Table ( EmpID int, FirstName varchar(50), Salary int ) As begin Insert @Emp Select e.EmpID,e.FirstName,e.Salary from Employee e; --Now update salary of first employee update @Emp set Salary=25000 where EmpID=1; --It will update only in @Emp table not in Original Employee table return end
--Now call the above created function Select * from fnGetMulEmployee()
--Now see the original table. This is not affected by above function update command Select * from Employee
Unlike Stored Procedure, Function returns only single value.
Unlike Stored Procedure, Function accepts only input parameters.
Unlike Stored Procedure, Function is not used to Insert, Update, Delete data in database table(s).
Like Stored Procedure, Function can be nested up to 32 level.
User Defined Function can have upto 1023 input parameters while a Stored Procedure can have upto 21000 input parameters.
User Defined Function can't returns XML Data Type.
User Defined Function doesn't support Exception handling.
User Defined Function can call only Extended Stored Procedure.
User Defined Function doesn't support set options like set ROWCOUNT etc.
In this article I try to explain the types of function in sql server with example. I hope after reading this article you will be aware about function in Sql Server. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.