Professional Documents
Culture Documents
a) Function is a database object. Basically it is a set of sql statements that accepts input parameters
perform action and return result. function can return
1) System defined function: Already system have some predefined functions. it is devided into two
types .
a)scalar functions
b)aggregate functions
a)scalar functions:SQL scalar functions return a single value, based on the input value.
a)scalar functions
b)tablevalued functions
a)scalar functions: User Defined Functions play an important role in SQL Server. User Defined
functions can be used to perform a complex logic, can accept parameters and
return data. Many a times we have to write complex logic which cannot be written using a single
query. In such scenarios, UDFs play an important role. scalar functions return
user defined function may or may not have parameters,but always return a single value.the return
value can be o f any datatype,except(text,ntext,images,cursor and
time stamp)
syntax:
create function functionname
input parameters
as
begin
return(query)
end
example program:
@studentid int
) Returns varchar(20)
as
begin
end
b)tablevalued functions:
Inline table valued functions are using to achive the functionality of parameterized
views.
syntax:
create table function1
@studentid int
) Returns table
as
@studentid int
)returns table
as
b)Multi statement table valued functions: Multi statement table valued function also just like
Inline table valued functions. But Multi statement
@studentid int
as
begin
insert @table
return
end
2 Will allow only Select statements, it will not allow us to use DML statements. Can have select
statements as well as DML statements. such as insert, update, delete and so on
3 It will allow only input parameters, doesn't support output parameters. It can have
both input and output parameters.
4 It will not allow us to use try-catch blocks. For exception handling we can
use try catch blocks.
6 We can use only table variables, it will not allow using temporary tables. Can use
both table variables as well as temporary table in it.