SlideShare a Scribd company logo
A function is a Transact-SQL or common language runtime (CLR) routine
that accepts parameters, performs an action, such as a complex calculation,
and returns the result of that action as a value. The return value can either
be a scalar (single) value or a table.
The following is some important information about functions:
 Functions only work with select statements.
 Functions can be used anywhere in SQL, like AVG, COUNT, SUM, MIN,
DATE and so on with select statements.
 Functions compile every time.
 Functions must return a value or result.
 Functions only work with input parameters.
 Try and catch statements are not used in functions.
Types of Function:
There is basically two tyes of functions in SQL server:-
1. User Defined Function
These functions are created by user in system database or in user
defined database. We two types of user defined functions.
1. Table Valued Function.
2. Scalar Valued Function.
1.1 Table Valued Function
In this type of function we select table data using a user created
function.
CREATE TABLE UserRegistration
(
C_Id int IDENTITY(1,1) NOT NULL,
C_Name varchar(100) NULL,
C_Age varchar(100) NULL,
C_Country varchar(100) NULL
);
Now Insert records in UserRegistration Table :-
Insert into UserRegistration(C_Name,C_Age,C_Country)
values('Ramesh','27','India')
Now we have to create one function like this:
create function GetUserRecords()
returns table
as return(select * from UserRegistration)
Now Call that function like this:
select * from GetUserRecords()
Note:This will return all records from table UserRegistration
1.2 Scalar Valued Function:
User-defined scalar functions return a single data value of the type
defined in the RETURNS clause.
For an inline scalar function, there is no function body; the scalar value is
the result of a single statement.
create function GetUserAge
(
@name nvarchar(500)
)
returns table
As
return
(
select C_Age from UserRegistration where C_Name =
@name
)
Now call this function like this way
select * from GetUserAge('Ramesh')
For a multistatement scalar function, the function body, defined in a
BEGIN...END block, contains a series of Transact-SQL statements that
return the single value.
Create function MultistatementScalarFunction
(
@name varchar(100),
@Age varchar(100),
@Country varchar(100)
)
returns nvarchar(100)
As
Begin
Return
(
select (@name+'' +@Age+''+ @Country)
)
End
Now Call that function like this way:
select dbo.MultistatementScalarFunction(C_Name,C_Age,C_Country)as Info
from UserRegistration where C_Id=2
2.System Defined Function
These functionsaredefined by Sql Server for differentpurpose. Wehave
two types of system defined function in Sql Server
1. Scalar Function
2. Aggregate Function
2.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.
2.1 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.

More Related Content

What's hot (20)

Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
Scrollable Updatable
Scrollable UpdatableScrollable Updatable
Scrollable Updatable
 
Scrollable Updatable
Scrollable UpdatableScrollable Updatable
Scrollable Updatable
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
Trigger
TriggerTrigger
Trigger
 
Function & procedure
Function & procedureFunction & procedure
Function & procedure
 
React basics
React basicsReact basics
React basics
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Data Binding - Android by Harin Trivedi
Data Binding - Android by Harin TrivediData Binding - Android by Harin Trivedi
Data Binding - Android by Harin Trivedi
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
codeigniter
codeignitercodeigniter
codeigniter
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
 
MYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-havingMYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-having
 
View, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - ThaiptView, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - Thaipt
 
Query hierarchical data the easy way, with CTEs
Query hierarchical data the easy way, with CTEsQuery hierarchical data the easy way, with CTEs
Query hierarchical data the easy way, with CTEs
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
SQL
SQLSQL
SQL
 

Viewers also liked

โครงงานคอมพิวเตอร์
โครงงานคอมพิวเตอร์โครงงานคอมพิวเตอร์
โครงงานคอมพิวเตอร์uoropo01
 
Charged inc investment prospectus - artie
Charged inc   investment prospectus - artieCharged inc   investment prospectus - artie
Charged inc investment prospectus - artieDrinkCharged
 
How to create Store Procedure
How to create Store ProcedureHow to create Store Procedure
How to create Store ProcedureDurgaprasad Yadav
 
Lady Ehrenburger case study
Lady Ehrenburger   case studyLady Ehrenburger   case study
Lady Ehrenburger case studyvcavsnc
 
Sunburn Treatment - Layton Chiropractor
Sunburn Treatment  - Layton ChiropractorSunburn Treatment  - Layton Chiropractor
Sunburn Treatment - Layton ChiropractorKathy Gordon
 
Absolute beginnersseriesforwindowsphone8
Absolute beginnersseriesforwindowsphone8Absolute beginnersseriesforwindowsphone8
Absolute beginnersseriesforwindowsphone8Mahisa Dyan Diptya
 
merrick differential peice rate plan..(piece wage plan)
merrick differential peice rate plan..(piece wage plan)merrick differential peice rate plan..(piece wage plan)
merrick differential peice rate plan..(piece wage plan)kulmitsingh
 

Viewers also liked (15)

Anuario estadístico de Salud 2009
Anuario estadístico de Salud 2009Anuario estadístico de Salud 2009
Anuario estadístico de Salud 2009
 
โครงงานคอมพิวเตอร์
โครงงานคอมพิวเตอร์โครงงานคอมพิวเตอร์
โครงงานคอมพิวเตอร์
 
Charged inc investment prospectus - artie
Charged inc   investment prospectus - artieCharged inc   investment prospectus - artie
Charged inc investment prospectus - artie
 
Trigger
TriggerTrigger
Trigger
 
How to create Store Procedure
How to create Store ProcedureHow to create Store Procedure
How to create Store Procedure
 
Fetal chest
Fetal chestFetal chest
Fetal chest
 
Index in sql server
Index in sql serverIndex in sql server
Index in sql server
 
Gineth o
Gineth oGineth o
Gineth o
 
Ficha de Santa rosa del Abuna
Ficha de Santa rosa del AbunaFicha de Santa rosa del Abuna
Ficha de Santa rosa del Abuna
 
Beni
BeniBeni
Beni
 
Lady Ehrenburger case study
Lady Ehrenburger   case studyLady Ehrenburger   case study
Lady Ehrenburger case study
 
Sunburn Treatment - Layton Chiropractor
Sunburn Treatment  - Layton ChiropractorSunburn Treatment  - Layton Chiropractor
Sunburn Treatment - Layton Chiropractor
 
Absolute beginnersseriesforwindowsphone8
Absolute beginnersseriesforwindowsphone8Absolute beginnersseriesforwindowsphone8
Absolute beginnersseriesforwindowsphone8
 
Chorio
ChorioChorio
Chorio
 
merrick differential peice rate plan..(piece wage plan)
merrick differential peice rate plan..(piece wage plan)merrick differential peice rate plan..(piece wage plan)
merrick differential peice rate plan..(piece wage plan)
 

Similar to Function

Scalar user defined function in sap hana
Scalar user defined function in sap hanaScalar user defined function in sap hana
Scalar user defined function in sap hanakabilarasan R
 
Lab Session for sql programming language 1.pptx
Lab Session for sql programming language 1.pptxLab Session for sql programming language 1.pptx
Lab Session for sql programming language 1.pptxmeharikiros2
 
Web Developer make the most out of your Database !
Web Developer make the most out of your Database !Web Developer make the most out of your Database !
Web Developer make the most out of your Database !Jean-Marc Desvaux
 
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_103 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1mlraviol
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and OperatorsMohan Kumar.R
 
Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Ziaur Rahman
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functionsMudasir Syed
 
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapperSF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapperChester Chen
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdfKalyankumarVenkat1
 
Functions in sap hana
Functions in sap hanaFunctions in sap hana
Functions in sap hanakabilarasan R
 
React table tutorial use filter (part 2)
React table tutorial use filter (part 2)React table tutorial use filter (part 2)
React table tutorial use filter (part 2)Katy Slemon
 

Similar to Function (20)

Scalar user defined function in sap hana
Scalar user defined function in sap hanaScalar user defined function in sap hana
Scalar user defined function in sap hana
 
Sql functions
Sql functionsSql functions
Sql functions
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Lab Session for sql programming language 1.pptx
Lab Session for sql programming language 1.pptxLab Session for sql programming language 1.pptx
Lab Session for sql programming language 1.pptx
 
Oracle: Functions
Oracle: FunctionsOracle: Functions
Oracle: Functions
 
Oracle: Functions
Oracle: FunctionsOracle: Functions
Oracle: Functions
 
Web Developer make the most out of your Database !
Web Developer make the most out of your Database !Web Developer make the most out of your Database !
Web Developer make the most out of your Database !
 
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_103 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1
 
Sqlapi0.1
Sqlapi0.1Sqlapi0.1
Sqlapi0.1
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
 
Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
 
ADVANCED MODELLING.pptx
ADVANCED MODELLING.pptxADVANCED MODELLING.pptx
ADVANCED MODELLING.pptx
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapperSF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdf
 
Functions in sap hana
Functions in sap hanaFunctions in sap hana
Functions in sap hana
 
React table tutorial use filter (part 2)
React table tutorial use filter (part 2)React table tutorial use filter (part 2)
React table tutorial use filter (part 2)
 
Sql functions
Sql functionsSql functions
Sql functions
 

Recently uploaded

INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfbu07226
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxssuserbdd3e8
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfPo-Chuan Chen
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePedroFerreira53928
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFVivekanand Anglo Vedic Academy
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxRaedMohamed3
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfYibeltalNibretu
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfTamralipta Mahavidyalaya
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxDenish Jangid
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfVivekanand Anglo Vedic Academy
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345beazzy04
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 

Function

  • 1. A function is a Transact-SQL or common language runtime (CLR) routine that accepts parameters, performs an action, such as a complex calculation, and returns the result of that action as a value. The return value can either be a scalar (single) value or a table. The following is some important information about functions:  Functions only work with select statements.  Functions can be used anywhere in SQL, like AVG, COUNT, SUM, MIN, DATE and so on with select statements.  Functions compile every time.  Functions must return a value or result.  Functions only work with input parameters.  Try and catch statements are not used in functions. Types of Function: There is basically two tyes of functions in SQL server:- 1. User Defined Function These functions are created by user in system database or in user defined database. We two types of user defined functions. 1. Table Valued Function. 2. Scalar Valued Function.
  • 2. 1.1 Table Valued Function In this type of function we select table data using a user created function. CREATE TABLE UserRegistration ( C_Id int IDENTITY(1,1) NOT NULL, C_Name varchar(100) NULL, C_Age varchar(100) NULL, C_Country varchar(100) NULL ); Now Insert records in UserRegistration Table :- Insert into UserRegistration(C_Name,C_Age,C_Country) values('Ramesh','27','India') Now we have to create one function like this: create function GetUserRecords() returns table as return(select * from UserRegistration) Now Call that function like this: select * from GetUserRecords() Note:This will return all records from table UserRegistration
  • 3. 1.2 Scalar Valued Function: User-defined scalar functions return a single data value of the type defined in the RETURNS clause. For an inline scalar function, there is no function body; the scalar value is the result of a single statement. create function GetUserAge ( @name nvarchar(500) ) returns table As return ( select C_Age from UserRegistration where C_Name = @name ) Now call this function like this way select * from GetUserAge('Ramesh') For a multistatement scalar function, the function body, defined in a BEGIN...END block, contains a series of Transact-SQL statements that return the single value.
  • 4. Create function MultistatementScalarFunction ( @name varchar(100), @Age varchar(100), @Country varchar(100) ) returns nvarchar(100) As Begin Return ( select (@name+'' +@Age+''+ @Country) ) End Now Call that function like this way: select dbo.MultistatementScalarFunction(C_Name,C_Age,C_Country)as Info from UserRegistration where C_Id=2 2.System Defined Function These functionsaredefined by Sql Server for differentpurpose. Wehave two types of system defined function in Sql Server 1. Scalar Function 2. Aggregate Function
  • 5. 2.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. 2.1 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.