SlideShare a Scribd company logo
2
Most read
5
Most read
6
Most read
Prof. Neeraj Bhargava
Pooja Dixit
Department of Computer Science
School of Engineering & System Science
MDS, University Ajmer, Rajasthan, India
1
 Like a procedure, a function can accept multiple
parameters, and the data type of the return value
must be declared in the header of the function.
CREATE [OR REPLACE] FUNCTION <function_name>
(<parameter1_name> <mode> <data type>,
<parameter2_name> <mode> <data type>, ...)
RETURN <function return value data type> {AS|IS}
<Variable declarations>
BEGIN
Executable Commands
RETURN (return_value);
. . .
[EXCEPTION
Exception handlers]
END;
 The general syntax of the RETURN statement is:
RETURN <expression>;
2
CREATE OR REPLACE FUNCTION RetrieveSalary
RETURN NUMBER
IS
v_Salary NUMBER(10,2);
BEGIN
SELECT Salary INTO v_Salary
FROM Employee
WHERE EmployeeID = '01885';
RETURN v_Salary;
END RetrieveSalary;
/
3
SQL> @RetrieveSalary
Function created.
SQL> var v_SalaryOutput NUMBER;
SQL> EXEC :v_SalaryOutput :=
RetrieveSalary;
PL/SQL procedure successfully completed.
SQL> print v_SalaryOutput;
V_SALARYOUTPUT
--------------
16250
4
 PL/SQL Example illustrates a function that has a
single IN parameter and that returns a VARCHAR2
data type.
CREATE OR REPLACE FUNCTION FullName (p_EmployeeID IN
employee.EmployeeID%TYPE)
RETURN VARCHAR2 IS
v_FullName VARCHAR2(100);
v_FirstName employee.FirstName%TYPE;
v_MiddleName employee.MiddleName%TYPE;
v_LastName employee.LastName%TYPE;
BEGIN
SELECT FirstName, MiddleName, LastName INTO
v_FirstName, v_MiddleName, v_LastName
FROM Employee
WHERE EmployeeID = p_EmployeeID;
5
-- Store last name, comma and blank and first name
to variable
v_FullName := v_LastName||', '||v_FirstName;
-- Check for existence of a middle name
IF LENGTH(v_MiddleName) > 0 THEN
v_FullName := v_FullName|| ' '
||SUBSTR(v_MiddleName,1,1)||'.';
END IF;
RETURN v_FullName;
END FullName;
/
6
 A simple SELECT statement executed
within SQL*Plus can return the full name
for any employee identifier value as
shown in PL/SQL Example.
/* PL SQL Example 13.10 */
SQL> SELECT FullName('01885')
2 FROM Employee
3 WHERE EmployeeID = '01885';
FULLNAME('01885')
-----------------------------------
Bock, Douglas B.
7
SQL> SELECT FullName(EmployeeID)
2 FROM Employee
3 ORDER BY FullName(EmployeeID);
FULLNAME(EMPLOYEEID)
-------------------------------------
Adams, Adam A.
Barlow, William A.
Becker, Robert B.
Becker, Roberta G.
Bock, Douglas B.
... more rows will display
8
 As with the DROP PROCEDURE statement,
the DROP FUNCTION <functionName> is
also straight-forward.
 As with DROP PROCEDURE, the DROP
FUNCTION statement is a DDL command
that causes execution of an implicit
commit prior to and immediately after
the command.
SQL> DROP FUNCTION FullName;
Function dropped.
9

More Related Content

PPTX
Sql commands
PPT
05 Creating Stored Procedures
PPT
SQL select clause
PPTX
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
PPT
Oracle PLSQL Step By Step Guide
PPTX
pl/sql Procedure
PPT
1 - Introduction to PL/SQL
PPT
Using the set operators
Sql commands
05 Creating Stored Procedures
SQL select clause
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
Oracle PLSQL Step By Step Guide
pl/sql Procedure
1 - Introduction to PL/SQL
Using the set operators

What's hot (20)

PPT
Sequences and indexes
DOCX
Joins in dbms and types
PPTX
Unit 5 composite datatypes
PPTX
Key and its different types
PPT
SQL select statement and functions
PPTX
sql function(ppt)
PPT
Oracle Database Trigger
PPTX
Introduction to Oracle Database
PPTX
Structured Query Language (SQL)
PPTX
Codd’s rule.pptx
PPT
Constraints In Sql
PPTX
SQL Functions
PPT
02 Writing Executable Statments
PPTX
SQL Queries Information
PPTX
Sql operator
PPTX
Presentation slides of Sequence Query Language (SQL)
PPTX
Integrity Constraints
PPTX
DATABASE CONSTRAINTS
PPTX
Packages in PL/SQL
Sequences and indexes
Joins in dbms and types
Unit 5 composite datatypes
Key and its different types
SQL select statement and functions
sql function(ppt)
Oracle Database Trigger
Introduction to Oracle Database
Structured Query Language (SQL)
Codd’s rule.pptx
Constraints In Sql
SQL Functions
02 Writing Executable Statments
SQL Queries Information
Sql operator
Presentation slides of Sequence Query Language (SQL)
Integrity Constraints
DATABASE CONSTRAINTS
Packages in PL/SQL
Ad

Similar to Function in PL/SQL (20)

PPTX
Procedure and Functions in pl/sql
PPTX
Lecture 3.2_Subprogrammm - Function.pptx
PPT
DOC
Plsql task answers
PPTX
9. DBMS Experiment Laboratory PresentationPPT
PDF
Lecture Notes Unit5 chapter17 Stored procedures and functions
PDF
SQL Procedures & Functions
PPTX
PL_SQL - II.pptx
PPTX
PL_SQL_1.pptx fvbxcfbhxdfgh .
PPTX
DBMS: Week 11 - Stored Procedures and Functions
DOCX
PLSQL.docx
PPTX
Plsql coding conventions
DOC
Plsql task
PPT
plsql les02
PPTX
Functions
PPT
PDF
February0504 pm
PPTX
Pl-sql blocks and block types and variablesdeclaring.pptx
PPT
PPTX
PL/SQL___________________________________
Procedure and Functions in pl/sql
Lecture 3.2_Subprogrammm - Function.pptx
Plsql task answers
9. DBMS Experiment Laboratory PresentationPPT
Lecture Notes Unit5 chapter17 Stored procedures and functions
SQL Procedures & Functions
PL_SQL - II.pptx
PL_SQL_1.pptx fvbxcfbhxdfgh .
DBMS: Week 11 - Stored Procedures and Functions
PLSQL.docx
Plsql coding conventions
Plsql task
plsql les02
Functions
February0504 pm
Pl-sql blocks and block types and variablesdeclaring.pptx
PL/SQL___________________________________
Ad

More from Pooja Dixit (20)

PPTX
Combinational circuit.pptx
PPTX
number system.pptx
PPTX
Multiplexer.pptx
PPTX
Logic Gates.pptx
PPTX
K-Map.pptx
PPTX
Karnaugh Map Simplification Rules.pptx
PPTX
Half Subtractor.pptx
PPTX
Gray Code.pptx
PPTX
Flip Flop.pptx
PPTX
Encoder.pptx
PPTX
De-multiplexer.pptx
PPTX
DeMorgan’s Theory.pptx
PPTX
Combinational circuit.pptx
PPTX
Boolean Algebra.pptx
PPTX
Binary Multiplication & Division.pptx
PPTX
Binary addition.pptx
PPTX
Basics of Computer Organization.pptx
PPTX
Decoders
PPTX
Three Address code
PPTX
Cyrus beck line clipping algorithm
Combinational circuit.pptx
number system.pptx
Multiplexer.pptx
Logic Gates.pptx
K-Map.pptx
Karnaugh Map Simplification Rules.pptx
Half Subtractor.pptx
Gray Code.pptx
Flip Flop.pptx
Encoder.pptx
De-multiplexer.pptx
DeMorgan’s Theory.pptx
Combinational circuit.pptx
Boolean Algebra.pptx
Binary Multiplication & Division.pptx
Binary addition.pptx
Basics of Computer Organization.pptx
Decoders
Three Address code
Cyrus beck line clipping algorithm

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Classroom Observation Tools for Teachers
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Lesson notes of climatology university.
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Trump Administration's workforce development strategy
Final Presentation General Medicine 03-08-2024.pptx
Final Presentation General Medicine 03-08-2024.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Classroom Observation Tools for Teachers
O7-L3 Supply Chain Operations - ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Lesson notes of climatology university.
202450812 BayCHI UCSC-SV 20250812 v17.pptx
human mycosis Human fungal infections are called human mycosis..pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Computing-Curriculum for Schools in Ghana
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Cell Types and Its function , kingdom of life
A systematic review of self-coping strategies used by university students to ...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Trump Administration's workforce development strategy

Function in PL/SQL

  • 1. Prof. Neeraj Bhargava Pooja Dixit Department of Computer Science School of Engineering & System Science MDS, University Ajmer, Rajasthan, India 1
  • 2.  Like a procedure, a function can accept multiple parameters, and the data type of the return value must be declared in the header of the function. CREATE [OR REPLACE] FUNCTION <function_name> (<parameter1_name> <mode> <data type>, <parameter2_name> <mode> <data type>, ...) RETURN <function return value data type> {AS|IS} <Variable declarations> BEGIN Executable Commands RETURN (return_value); . . . [EXCEPTION Exception handlers] END;  The general syntax of the RETURN statement is: RETURN <expression>; 2
  • 3. CREATE OR REPLACE FUNCTION RetrieveSalary RETURN NUMBER IS v_Salary NUMBER(10,2); BEGIN SELECT Salary INTO v_Salary FROM Employee WHERE EmployeeID = '01885'; RETURN v_Salary; END RetrieveSalary; / 3
  • 4. SQL> @RetrieveSalary Function created. SQL> var v_SalaryOutput NUMBER; SQL> EXEC :v_SalaryOutput := RetrieveSalary; PL/SQL procedure successfully completed. SQL> print v_SalaryOutput; V_SALARYOUTPUT -------------- 16250 4
  • 5.  PL/SQL Example illustrates a function that has a single IN parameter and that returns a VARCHAR2 data type. CREATE OR REPLACE FUNCTION FullName (p_EmployeeID IN employee.EmployeeID%TYPE) RETURN VARCHAR2 IS v_FullName VARCHAR2(100); v_FirstName employee.FirstName%TYPE; v_MiddleName employee.MiddleName%TYPE; v_LastName employee.LastName%TYPE; BEGIN SELECT FirstName, MiddleName, LastName INTO v_FirstName, v_MiddleName, v_LastName FROM Employee WHERE EmployeeID = p_EmployeeID; 5
  • 6. -- Store last name, comma and blank and first name to variable v_FullName := v_LastName||', '||v_FirstName; -- Check for existence of a middle name IF LENGTH(v_MiddleName) > 0 THEN v_FullName := v_FullName|| ' ' ||SUBSTR(v_MiddleName,1,1)||'.'; END IF; RETURN v_FullName; END FullName; / 6
  • 7.  A simple SELECT statement executed within SQL*Plus can return the full name for any employee identifier value as shown in PL/SQL Example. /* PL SQL Example 13.10 */ SQL> SELECT FullName('01885') 2 FROM Employee 3 WHERE EmployeeID = '01885'; FULLNAME('01885') ----------------------------------- Bock, Douglas B. 7
  • 8. SQL> SELECT FullName(EmployeeID) 2 FROM Employee 3 ORDER BY FullName(EmployeeID); FULLNAME(EMPLOYEEID) ------------------------------------- Adams, Adam A. Barlow, William A. Becker, Robert B. Becker, Roberta G. Bock, Douglas B. ... more rows will display 8
  • 9.  As with the DROP PROCEDURE statement, the DROP FUNCTION <functionName> is also straight-forward.  As with DROP PROCEDURE, the DROP FUNCTION statement is a DDL command that causes execution of an implicit commit prior to and immediately after the command. SQL> DROP FUNCTION FullName; Function dropped. 9