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

PPTX
Mysql creating stored function
PPTX
PDF
PL/SQL TRIGGERS
PPTX
Stored procedure in sql server
PPTX
Sql Functions And Procedures
PPT
statement interface
PPTX
PPTX
Chapter 5 transactions and dcl statements
Mysql creating stored function
PL/SQL TRIGGERS
Stored procedure in sql server
Sql Functions And Procedures
statement interface
Chapter 5 transactions and dcl statements

What's hot (20)

PPS
Procedures/functions of rdbms
PPT
Scrollable Updatable
PPT
Scrollable Updatable
PPT
Executing Sql Commands
PPTX
PPTX
Function & procedure
PPTX
React basics
PPTX
Sql tutorial
PPTX
Data Binding - Android by Harin Trivedi
PPTX
Cursors, triggers, procedures
PPTX
Procedures and triggers in SQL
PPTX
codeigniter
PPTX
PPTX
MYSQL single rowfunc-multirowfunc-groupby-having
PPTX
View, Store Procedure & Function and Trigger in MySQL - Thaipt
PPTX
Query hierarchical data the easy way, with CTEs
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Procedures/functions of rdbms
Scrollable Updatable
Scrollable Updatable
Executing Sql Commands
Function & procedure
React basics
Sql tutorial
Data Binding - Android by Harin Trivedi
Cursors, triggers, procedures
Procedures and triggers in SQL
codeigniter
MYSQL single rowfunc-multirowfunc-groupby-having
View, Store Procedure & Function and Trigger in MySQL - Thaipt
Query hierarchical data the easy way, with CTEs
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Ad

Viewers also liked (15)

PDF
Anuario estadístico de Salud 2009
DOCX
โครงงานคอมพิวเตอร์
PDF
Charged inc investment prospectus - artie
DOCX
DOCX
How to create Store Procedure
PPTX
Fetal chest
DOCX
Index in sql server
PDF
Gineth o
PDF
Ficha de Santa rosa del Abuna
PPTX
Lady Ehrenburger case study
PDF
Sunburn Treatment - Layton Chiropractor
PDF
Absolute beginnersseriesforwindowsphone8
PPT
PPTX
merrick differential peice rate plan..(piece wage plan)
Anuario estadístico de Salud 2009
โครงงานคอมพิวเตอร์
Charged inc investment prospectus - artie
How to create Store Procedure
Fetal chest
Index in sql server
Gineth o
Ficha de Santa rosa del Abuna
Lady Ehrenburger case study
Sunburn Treatment - Layton Chiropractor
Absolute beginnersseriesforwindowsphone8
merrick differential peice rate plan..(piece wage plan)
Ad

Similar to Function (20)

PPT
Day1Structured_Query_Lang3For PL SQL Notes.ppt
PPTX
Scalar user defined function in sap hana
PDF
Sql functions
ODP
ODP
PPTX
Lesson-5-Complex-Queries-Aggregate-Function-Nested-Queries-Triggers.pptx
PPTX
Lab Session for sql programming language 1.pptx
PPTX
Oracle: Functions
PPTX
Oracle: Functions
PPTX
Web Developer make the most out of your Database !
PPTX
Basics of SQL understanding the database.pptx
PDF
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1
PPT
Sqlapi0.1
PDF
SQL Functions and Operators
PPTX
Developers' New features of Sql server express 2012
PPT
PHP mysql Aggregate functions
PPTX
ADVANCED MODELLING.pptx
PPT
Functional Programming
PPTX
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
PDF
Oracle_Analytical_function.pdf
Day1Structured_Query_Lang3For PL SQL Notes.ppt
Scalar user defined function in sap hana
Sql functions
Lesson-5-Complex-Queries-Aggregate-Function-Nested-Queries-Triggers.pptx
Lab Session for sql programming language 1.pptx
Oracle: Functions
Oracle: Functions
Web Developer make the most out of your Database !
Basics of SQL understanding the database.pptx
03 2017Emea_RoadshowMilan-WhatsNew-Mariadbserver10_2andmaxscale 2_1
Sqlapi0.1
SQL Functions and Operators
Developers' New features of Sql server express 2012
PHP mysql Aggregate functions
ADVANCED MODELLING.pptx
Functional Programming
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
Oracle_Analytical_function.pdf

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
master seminar digital applications in india
PDF
Classroom Observation Tools for Teachers
PPTX
Cell Types and Its function , kingdom of life
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Module 4: Burden of Disease Tutorial Slides S2 2025
GDM (1) (1).pptx small presentation for students
Weekly quiz Compilation Jan -July 25.pdf
Microbial disease of the cardiovascular and lymphatic systems
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
human mycosis Human fungal infections are called human mycosis..pptx
RMMM.pdf make it easy to upload and study
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Orientation - ARALprogram of Deped to the Parents.pptx
master seminar digital applications in india
Classroom Observation Tools for Teachers
Cell Types and Its function , kingdom of life
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE

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.