You are on page 1of 5

2) what is function & types of functions in sql?

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

single value or table.functions cann't be use insert,delete,update records in a database

Functions two types:

1) System defined function

2)User defined functions

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.

ASCII()-CONVERTS CHARACTERS TO INT

CHAR()- CONVERTS INT TO CHAR

UCASE() - Converts a field to upper case

LCASE() - Converts a field to lower case

MID() - Extract characters from a text field

LEN() - Returns the length of a text field

ROUND() - Rounds a numeric field to the number of decimals specified

NOW() - Returns the current system date and time

FORMAT() - Formats how a field is to be displayed


b)aggregate functions:SQL aggregate functions return a single value, calculated from values in a
column.

AVG() - Returns the average value

COUNT() - Returns the number of rows

FIRST() - Returns the first value

LAST() - Returns the last value

MAX() - Returns the largest value

MIN() - Returns the smallest value

SUM() - Returns the sum

2)User defined functions:

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

single value that value represent datatype (like int,varchar).

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

) returns data type

as

begin

return(query)

end

example program:

create table function1

@studentid int

) Returns varchar(20)

as

begin

return(select name from asha where studentid=@studentid)

end

b)tablevalued functions:

a)Inline table valued functions

b)Multi statement table valued functions

a)Inline table valued functions:

Inline table valued functions returns a table. This body


enclosed between begin and end blocks.

Inline table valued functions are using to achive the functionality of parameterized
views.

syntax:
create table function1

@studentid int

) Returns table

as

select * from asha where studentid=@studentid

Example : create funtion function2

@studentid int

)returns table

as

return (select * from asha where studentid=@studentid)

b)Multi statement table valued functions: Multi statement table valued function also just like
Inline table valued functions. But Multi statement

table valued function can define structure of the table.

example: create table function1

@studentid int

) Returns @table table (studentid int, studentname


varchar(50),studentaddress varchar(50))

as

begin

insert @table

select studentid,studentname,studentaddress from asha where


studentid=@studentid

return
end

User Defined Function Stored Procedure

1 Function must return a value. Stored Procedure may or not return


values.

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.

5 Transactions are not allowed within functions. Can use transactions


within Stored Procedures.

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.

7 Stored Procedures can't be called from a function. Stored Procedures can


call functions.

8 Functions can be called from a select statement. Procedures can't be


called from Select/Where/Having and

so on statements. Execute/Exec statement can


be used to call/

execute Stored Procedure.

9 A UDF can be used in join clause as a result set. Procedures can't be


used in Join clause

You might also like