user defined function in sql
DESCRIPTION
User defined Function in SQLTRANSCRIPT
![Page 1: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/1.jpg)
![Page 2: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/2.jpg)
Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
![Page 3: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/3.jpg)
Week Target Achieved
1 ------- 15wpm
2
3
Typing Speed
![Page 4: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/4.jpg)
Jobs Applied# Company Designation Applied Date Current Status
1
2
3
![Page 5: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/5.jpg)
User Defined Functions In SQL
sandeep.narayan.73twitter.com/usernamein.linkedin.com/in/sandeep
v9447532678
![Page 6: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/6.jpg)
What is UDF?
• User defined functions are similar to ordinary functions found in programming languages.
• The main purpose of UDF is code reusability.
![Page 7: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/7.jpg)
Benefits of using UDF
• Modular programming.• Faster execution.• Code reusability.
![Page 8: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/8.jpg)
• 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 table
• UDF can’t return non deterministic values like GETDATE () etc
• 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
![Page 9: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/9.jpg)
Types of UDF…
•Scalar functions
•Returns a single value
•Inline functions
•Returns a table
•Table values functions
• Multiple operations, complex logic just like Stored procedures
![Page 10: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/10.jpg)
FORMAT OF A UDF
CREATE FUNCTION dbo.function_name ( @parameter1 datatype = default value, @parameter2 datatype ) RETURNS datatype AS BEGIN
sql statements foms function body ... RETURN value END
![Page 11: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/11.jpg)
![Page 12: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/12.jpg)
SCALAR FUNCTION
CREATE FUNCTION dbo.teach_name(@teachr_id int)RETURNS varchar(20)ASBEGIN Declare @returnvalue varchar(20)Select @returnvalue=teacher_name from tbl_teachers where teacher_id=@teachr_id RETURN @returnvalueEND
Execution: select dbo.teach_name(1002)as teacher_name
![Page 13: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/13.jpg)
![Page 14: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/14.jpg)
INLINE FUNCTION
CREATE FUNCTION dbo.list_teach(@name varchar(10))RETURNS TABLEASRETURN SELECT *FROM tbl_teachers where teacher_name like '%'+@name+'%'
Execution: select * from dbo.list_teach('i')
![Page 15: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/15.jpg)
![Page 16: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/16.jpg)
Table values functions
CREATE FUNCTION udf_teach_day()RETURNS @Result TABLE ( vchr_teacher_name varchar(20) ,vchr_day varchar(10) )ASBEGIN INSERT INTO @Result (vchr_teacher_name,vchr_day) select teacher_name,datepart(dw,teacher_doj)as vchr_day from tbl_teachers UPDATE @Result SET vchr_day = 'weekday' where vchr_day<>cast(2 as varchar(20)) UPDATE @Result SET vchr_day = 'monday' where vchr_day=cast(2 as varchar(20)) RETURNEND
![Page 17: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/17.jpg)
Execution:Select * from udf_teach_day()
![Page 18: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/18.jpg)
![Page 19: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/19.jpg)
ConclusionUDF are similar to functions in the programming languages, they provide a mechanism for extending the functionality of the database server byadding a function that can be evaluated in SQL statements.
![Page 20: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/20.jpg)
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
![Page 21: User defined Function in SQL](https://reader036.vdocument.in/reader036/viewer/2022062514/558fcf761a28ab4b1b8b45b7/html5/thumbnails/21.jpg)
Contact Us
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Start up VillageEranakulam,Kerala, India.
Email: [email protected]