Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
# Company Designation Applied Date Current Status
User Defined Functions
What is UDF?
• User defined functions are similar to ordinary functions
found in programming languages.
• The main purpose of UDF is code reusability.
Benefits of using UDF
• Modular programming.
• Faster execution.
• Code reusability.
• User defined functions can be passed input parameters
• User defined functions are compiled and executed at
runtime so pretty slower than stored procedures
• UDF can’t perform DML (data manipulation language)
operations like Insertion, Update and Deletion on the base
• UDF can’t return non deterministic values like GETDATE ()
• Stored procedure can’t be called from inside a UDF whereas
a stored procedure can call a user defined function or another
stored procedure inside it
Types of UDF…
• Scalar functions
• Returns a single value
• Inline functions
• Returns a table
• Table values functions
• Multiple operations, complex logic just like Stored
FORMAT OF A UDF
CREATE FUNCTION dbo.function_name
@parameter1 datatype = default value,
sql statements foms function body ...
CREATE FUNCTION dbo.teach_name(@teachr_id int)
Declare @returnvalue varchar(20)
Select @returnvalue=teacher_name from tbl_teachers where
Execution: select dbo.teach_name(1002)as teacher_name
CREATE FUNCTION dbo.list_teach(@name varchar(10))
RETURN SELECT *
FROM tbl_teachers where teacher_name like '%'+@name+'%'
Execution: select * from dbo.list_teach('i')
Table values functions
CREATE FUNCTION udf_teach_day()
RETURNS @Result TABLE
INSERT INTO @Result
select teacher_name,datepart(dw,teacher_doj)as vchr_day from tbl_teachers
SET vchr_day = 'weekday' where vchr_day<>cast(2 as varchar(20))
SET vchr_day = 'monday' where vchr_day=cast(2 as varchar(20))
UDF are similar to functions in the programming languages, they provide
a mechanism for extending the functionality of the database server by
adding a function that can be evaluated in SQL statements.
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Ph: + 91 – 495 40 25 550
Start up Village