Please enable Javascript to correctly display the contents on Dot Net Tricks!

Different Types of SQL Server Functions

  Author : Shailendra Chauhan
Posted On : 19 Apr 2012
Total Views : 290,500   
Updated On : 24 Sep 2016

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). For more about stored procedure and function refer the article Difference between Stored Procedure and Function

Types of Function

  1. System Defined Function

    These functions are defined by Sql Server for different purpose. We have two types of system defined function in Sql Server

    1. Scalar Function

      Scalar functions operates on a single value and returns a single value. Below is the list of some useful Sql Server Scalar functions.

      System Scalar Function
      Scalar Function
      This returns absolute number of the given number means 10.67.
      This will generate random number of 10 characters.
      This will round off the given number to 3 places of decimal means 17.567
      This will returns upper case of given string means 'DOTNET'
      This will returns lower case of given string means 'dotnet'
      ltrim(' dotnet')
      This will remove the spaces from left hand side of 'dotnet' string.
      convert(int, 15.56)
      This will convert the given float value to integer means 15.

    2. Aggregate Function

      Aggregate functions operates on a collection of values and returns a single value. Below is the list of some useful Sql Server Aggregate functions.

      System Aggregate Function
      Aggregate Function
      This returns maximum value from a collection of values.
      This returns minimum value from a collection of values.
      This returns average of all values in a collection.
      This returns no of counts from a collection of values.

  2. User Defined Function

    These functions are created by user in system database or in user defined database. We three types of user defined functions.

    1. Scalar Function

      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)
      Begin return (Select @FirstName + ' '+ @LastName);
       --Calling the above created function
      Select dbo.fnGetEmpFullName(FirstName,LastName) as Name, Salary from Employee 
    2. Inline Table-Valued Function

      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
       return (Select * from Employee) 
       --Now call the above created function
      Select * from fnGetEmployee() 
    3. Multi-Statement Table-Valued Function

      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 for EmpID,FirstName and Salary of Employee
      Create function fnGetMulEmployee()
      returns @Emp Table
      EmpID int, 
      FirstName varchar(50),
      Salary int
       Insert into @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
       --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 


  1. Unlike Stored Procedure, Function returns only single value.

  2. Unlike Stored Procedure, Function accepts only input parameters.

  3. Unlike Stored Procedure, Function is not used to Insert, Update, Delete data in database table(s).

  4. Like Stored Procedure, Function can be nested up to 32 level.

  5. User Defined Function can have upto 1023 input parameters while a Stored Procedure can have upto 2100 input parameters.

  6. User Defined Function can't returns XML Data Type.

  7. User Defined Function doesn't support Exception handling.

  8. User Defined Function can call only Extended Stored Procedure.

  9. 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.

Free Interview Books
24 SEP
Angular2 and Angular4 (Classroom)
08:30 AM-11:30 AM IST
23 SEP
MEAN Stack (Classroom)
8:30 Am To 11:30 Am
23 SEP
Microsoft Azure Infrastructure Solutions (Online)
08:00 PM-11:00 PM IST / 9:30 AM-12:30 PM CST
20 SEP
MEAN Stack (Online)
07:00 AM-09:00 AM IST
20 SEP
ASP.NET MVC with Angular4 (Online)
9:00PM- 11:00PM IST(+5:30GMT)
16 SEP
Angular2 and Angular4 (Online)
08:00 AM-10:00 AM IST(+5.30 GMT)
ASP.NET MVC with Angular4 (Online)
09:00 PM to 11:00 PM IST (+5:30 GMT)
22 AUG
ASP.NET Core with Angular4 (Online)
07:00 AM - 9:00 AM IST(+5:30 GMT)
19 AUG
MEAN Stack 2 (Online)
5:30 Pm - 7:00 PM IST (+5:30 GMT)
19 AUG
ASP.NET MVC with Angular4 (Online)
10:30 Am to 12:30 PM IST (+ 5:30 GMT)
19 AUG
NodeJS with Angular4 (Classroom)
04:00 PM to 07:00 PM IST (+5:30 GMT)
12 AUG
ASP.NET MVC with Angular4 (Classroom)
11:30 AM - 1:30 PM IST (+5:30GMT)
15 JUL
ASP.NET MVC with Angular4 (Online)
03:30 PM - 05:30 PM IST(+5.30 GMT)