Skip to main content

Different Types of SQL Server Functions

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

Description

abs(-10.67)

This returns absolute number of the given number means 10.67.

rand(10)

This will generate random number of 10 characters.

round(17.56719,3)

This will round off the given number to 3 places of decimal means 17.567

upper('dotnet')

This will returns upper case of given string means 'DOTNET'

lower('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

Description

max()

This returns maximum value from a collection of values.

min()

This returns minimum value from a collection of values.

avg()

This returns average of all values in a collection.

count()

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.

1.   --Create a table

2.  CREATE TABLE Employee

3.  (

4.   EmpID int PRIMARY KEY,

5.   FirstName varchar(50) NULL,

6.   LastName varchar(50) NULL,

7.   Salary int NULL,

8.   Address varchar(100) NULL,

9.  )

10.--Insert Data

11.Insert into Employee(EmpID,FirstName,LastName,Salary,Address) Values(1,'Satish','Chauahn',22000,'Delhi');

12.Insert into Employee(EmpID,FirstName,LastName,Salary,Address) Values(2,'Harif','Khan',15000,'Delhi');

13.Insert into Employee(EmpID,FirstName,LastName,Salary,Address) Values(3,'Ganesh','Shakya',19000,'Noida');

14.Insert into Employee(EmpID,FirstName,LastName,Salary,Address) Values(4,'Syam','Kumar',19000,'Noida');

15.--See created table

16.Select * from Employee


17. --Create function to get emp full name

18.Create function fnGetEmpFullName

19.(

20. @FirstName varchar(50),

21. @LastName varchar(50)

22.)

23.returns varchar(101)

24.As

25.Begin return (Select @FirstName + ' '+ @LastName);

26.end



27. --Calling the above created function

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

1.   --Create function to get employees

2.  Create function fnGetEmployee()

3.  returns Table

4.  As

5.   return (Select * from Employee)



6.   --Now call the above created function

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

1.   --Create function for EmpID,FirstName and Salary of Employee

2.  Create function fnGetMulEmployee()

3.  returns @Emp Table

4.  (

5.  EmpID int,

6.  FirstName varchar(50),

7.  Salary int

8.  )

9.  As

10.begin

11. Insert into @Emp Select e.EmpID,e.FirstName,e.Salary from Employee e;

12.--Now update salary of first employee

13. update @Emp set Salary=25000 where EmpID=1;

14.--It will update only in @Emp table not in Original Employee table

15.return

16.end



17. --Now call the above created function

18.Select * from fnGetMulEmployee()



19. --Now see the original table. This is not affected by above function update command

20.Select * from Employee


Note

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.




Comments

Popular posts from this blog

.NET Core API With Dapper, Repository And UnitOfWork

  Introduction In this tutorial, you will use ASP.NET Core Web API to create a web API that returns a list of brands. This article focuses on creating web API using dapper, repository pattern, and UnitOfWork pattern in .NET 8. I will guide you through the steps by step to create web API. HTTP is not just for serving up web pages. HTTP is also a powerful platform for building APIs that expose services and data. HTTP is simple, flexible, and ubiquitous. Almost any platform that you can think of has an HTTP library, so HTTP services can reach a broad range of clients, including browsers, mobile devices, and traditional desktop applications. ASP.NET Core Web API is a framework for building web APIs on top of the .NET Framework. Creating a New Project in Visual Studio Launch Visual Studio and select Create a New Project. Creating a Core Web API Start Visual Studio and choose Create a new project. In the Create a new project dialog, select ASP.NET Core Web API Click Next. Configuring You...

.Net framework interview questions

What is the .NET Framework? The .NET Framework is a set of technologies that form an integral part of the .NET Platform. It is Microsoft’s managed code programming model for building applications. The .NET Framework has two main components: Common Language Runtime (CLR): The CLR is one of the foundation in the .NET framework and provides a common set of services for applications developed on Microsoft .Net Technologies. .NET Framework class library: The .NET framework class library is a collection of reusable types and exposes features of the runtime. It contains of a set of classes that is used to access common functionality. What is CTS (Common Type System)? The common type system (CTS) defines how types are declared, used, and managed in the runtime, and is also an important part of the runtime’s support for cross-language integration. The common type system performs the following functions: ·          Establishes a framework that...

Move Selected Gridview Rows to Another Gridview in Asp.net

Introduction :  Here I will explain how to move or transfer selected checkbox gridview rows to another  grid v iew in asp.net using c#, vb.net or copy one gridview row to another gridview in asp.net using c#, vb.net. Description :  In previous posts I explained Display images in gridview from database in asp.net, Enable/Disable checkbox in gridview based on condition,  asp.net gridview examples  and  bind data to textbox control in gridview ,  Bind data to dropdownlist  in gridview  and many articles relating to  asp.net,  c#, vb.net, c#,vb.net. Now I will explain how to move selected rows from one gridview to another gridview in asp.netusing c#, vb.net. To implement this first we need to write the code in aspx page like this  < html   xmlns ="http://www.w3.org/1999/xhtml"> < head > ...