Table of Contents
- Introduction
- Why we use function
- Type of function
- System defined function
- User defined function
Introduction
We all are from a programming background and we know what function is and why we need it. But I am going to recap what function is. Function provides us with encapsulated reusable logic. In other words function hides the steps and complexity of code and we use that function where we want.

Why we use function
There are benefits of using function. Some of them are following.
- We can use user defined function in select as well as where class.
- We can use user defined function used as rowset.
- We can use user defined function as parameterize views.
Type of function
SQL provide two type of functions.
- System defined function
- User defined function
System defined function
System defined functions are provided by SQL itself. You can find the details of these functions on the following. For more details click here.
User defined function
User defined functions are two types. First is Scalar and second is Table value.
Scalar Function
These functions return only a single value. These functions are any data type; it could be return int , varchar or datetime. Let’s understand by example. Let’s create a table and name it myTable.
- CREATE TABLE [dbo].[myTable](
- [id] [int] IDENTITY(1,1) NOT NULL,
- [myDate] [date] NOT NULL,
- [data] [int] NOT NULL
- )
Let’s insert some records into it.
- INSERT [dbo].[myTable] ( [myDate], [data]) VALUES (‘2015-01-01’, 10)
- INSERT [dbo].[myTable] ( [myDate], [data]) VALUES (‘2015-02-01’, 15)
- INSERT [dbo].[myTable] ( [myDate], [data]) VALUES (‘2015-03-01’, 20)
- INSERT [dbo].[myTable] ( [myDate], [data]) VALUES (‘2015-04-01’, 15)
- select * from myTable





Pramod ThakurPosted Sep 20, 2016, 3:43 AM
Thanks Subash
SubashPosted Sep 19, 2016, 7:42 AM
Nice share sir