SlideShare a Scribd company logo
Procedures and Functions
Function and Procedure
Parameter Types
 Named PL/SQL programs (Procedures and Functions) can take
parameters. Parameters are optional on both procedures and
functions.
 Keep the parameter name under 30 characters, they must start
with a letter and contain no spaces.
 There are 3 types of parameter-
 IN, OUT and IN OUT
2
Function and Procedure
Parameter Types Cont…
 An IN parameter is used as an input only. An IN parameter
cannot be changed by the called program.
 An OUT parameter is initially NULL. The program assigns the
parameter a value and that value is returned to the calling
program.
 An IN OUT parameter may or may not have an initial value. That
initial value may or may not be modified by the called program.
Any changes made to the parameter are returned to the calling
program.
3
Function and Procedure
Parameter Types Cont…
 Parameters are declared with data types but without data type
length or precision.
 A parameter may be declared as VARCHAR2 but it will not be
declared with a length component (VARCHAR2(30) would not
be valid).
 Parameters may also be assigned a default value.
 You can either use the assignment operator (:=) or use the
DEFAULT keyword. When a parameter has a default value, you
do not need to include that parameter in the call.
4
Sample Parameter Declarations
 ( parameter_1 IN VARCHAR2 := 'ABC',
 parameter_2 IN VARCHAR2 DEFAULT 'ABC',
 parameter_3 IN OUT NUMBER,
parameters_can_be_named_anything OUT DATE )
5
Procedure Example
 CREATE OR REPLACE PROCEDURE my_first_proc (p_name IN
VARCHAR2 := 'Lewis', p_address IN VARCHAR2 := '123
Mockingbird Ln', p_an_in_out_parameter IN OUT NUMBER,
p_an_out_parameter OUT DATE ) IS v_a_variable
VARCHAR2(30);
BEGIN
IF p_name = 'Lewis' THEN
DBMS_OUTPUT.PUT_LINE( p_name || ': ' || p_address );
END IF;
v_a_variable := 99;
p_an_in_out_parameter := v_a_variable; p_an_out_parameter :=
SYSDATE;
END;
6
Function Example
 CREATE OR REPLACE FUNCTION my_first_func (p_name IN
VARCHAR2 := 'Lewis', p_address IN VARCHAR2 := '123
Mockingbird Ln', p_an_in_out_parameter IN OUT NUMBER,
p_an_out_parameter OUT DATE ) RETURN VARCHAR2 IS
v_a_variable VARCHAR2(30);
BEGIN
IF p_name = 'Lewis' THEN
RETURN -1;
END IF;
v_a_variable := 99;
p_an_in_out_parameter := v_a_variable;
p_an_out_parameter := SYSDATE;
RETURN v_a_variable;
END;
7
Procedures VS Functions
 Procedures are traditionally the workhorse of the coding world.
 Functions are traditionally the smaller, more specific pieces of
code.
 In general, if you need to update the chart of accounts, you would
write a procedure.
 If you need to retrieve the organization code for a particular GL
account, you would write a function.
 A Procedure cannot return a value but a Function MUST return a
value.
8
Procedures VS Functions Cont…
 The return statement in a function returns control to the calling
program and returns the results of the function.
 The return statement of a procedure returns control to the calling
program and cannot return a value.
 Functions can be called from SQL, Procedure cannot.
 Functions are considered expressions, Procedure are not.
 Procedures and functions can both return data in OUT and IN
OUT parameters.
9

More Related Content

PPTX
Procedure n functions
PPTX
Function and types
PPT
Ch3 selection
PPTX
Procedure and Functions in pl/sql
DOCX
Functions oracle (pl/sql)
DOCX
Clauses in sql server
PPTX
Triggers
PPTX
c++ programming Unit 4 operators
Procedure n functions
Function and types
Ch3 selection
Procedure and Functions in pl/sql
Functions oracle (pl/sql)
Clauses in sql server
Triggers
c++ programming Unit 4 operators

Viewers also liked (10)

PDF
【資料】ハンズオンセミナー①クイックスタート
PDF
LA QUÍMICA -Yourprezi
PDF
Session 12 Kenneth Alston
PPT
Stress management
DOCX
PDF
Human Resource Management as a Strategic Business Partner
PPTX
Short-term joint staff training event - Erasmus+ - 13th Highschool of Kallith...
PPT
Circulos de calidad capacitacion fr-cal-ing-03 pp2
PDF
【IoT入門】スマホで加速度と位置情報を取得してクラウドに保存しよう!~ハンズオン資料②~
PDF
Cuadro explicativo miguelparra
【資料】ハンズオンセミナー①クイックスタート
LA QUÍMICA -Yourprezi
Session 12 Kenneth Alston
Stress management
Human Resource Management as a Strategic Business Partner
Short-term joint staff training event - Erasmus+ - 13th Highschool of Kallith...
Circulos de calidad capacitacion fr-cal-ing-03 pp2
【IoT入門】スマホで加速度と位置情報を取得してクラウドに保存しよう!~ハンズオン資料②~
Cuadro explicativo miguelparra
Ad

Similar to Functions (20)

PPT
Chapter 05 (Built-in SQL Functions) functions
PPTX
9. DBMS Experiment Laboratory PresentationPPT
ODP
Open Gurukul Language PL/SQL
PPT
Part36 parameter,form success
PPTX
pl/sql Procedure
PDF
SQL Procedures & Functions
PPTX
Pl/SQL Procedure,Function & Packages - sub programs
DOCX
Methods in Java
PPSX
Algorithms, Structure Charts, Corrective and adaptive.ppsx
PPTX
Detailed concept of function in c programming
PPTX
Subprogramms
PPTX
PL_SQL - II.pptx
PPTX
Understanding pass by value and pass by reference is essential for effective ...
PPTX
Functions in C.pptx
PPTX
Programming-in-C
PPT
Functions
DOCX
Trig
PPTX
Programming in C
PPT
Module04
PPTX
Stored procedures
Chapter 05 (Built-in SQL Functions) functions
9. DBMS Experiment Laboratory PresentationPPT
Open Gurukul Language PL/SQL
Part36 parameter,form success
pl/sql Procedure
SQL Procedures & Functions
Pl/SQL Procedure,Function & Packages - sub programs
Methods in Java
Algorithms, Structure Charts, Corrective and adaptive.ppsx
Detailed concept of function in c programming
Subprogramms
PL_SQL - II.pptx
Understanding pass by value and pass by reference is essential for effective ...
Functions in C.pptx
Programming-in-C
Functions
Trig
Programming in C
Module04
Stored procedures
Ad

Recently uploaded (20)

PPTX
DPT-MAY24.pptx for review and ucploading
PPTX
Opioid_Analgesics_Presentation (1).pptxp
PPTX
Theory of Change. AFH-FRDP OCEAN ToCpptx
PDF
Women’s Talk Session 1- Talking about women
PPTX
Digital Education Presentation for students.
PPTX
430838499-Anaesthesiiiia-Equipmenooot.pptx
PPTX
GPAT Presentation PPT and details about imp topics.pptx
PPTX
Unit 3 Presentation Etiquette Business and Corporate Etiquette
PPT
notes_Lecture2 23l3j2 dfjl dfdlkj d 2.ppt
PPT
pwm ppt .pdf long description of pwm....
PPTX
Final Second DC Messeting PPT-Pradeep.M final.pptx
PPTX
_Dispute Resolution_July 2022.pptxmhhghhhh
PDF
Sheri Ann Lowe Compliance Strategist Resume
PPTX
Slideham presentation for the students a
DOCX
PRACTICE-TEST-12 is specially designed for those
PDF
CollegePresentation.pdf hsjsisjsjsjsssoo
PDF
Parts of Speech Quiz Presentation in Orange Blue Illustrative Style.pdf.pdf
PPTX
employee on boarding for jobs for freshers try it
PPT
NO000387 (1).pptsbsnsnsnsnsnsnsmsnnsnsnsjsnnsnsnsnnsnnansnwjwnshshshs
PDF
servsafecomprehensive-ppt-full-140617222538-phpapp01.pdf
DPT-MAY24.pptx for review and ucploading
Opioid_Analgesics_Presentation (1).pptxp
Theory of Change. AFH-FRDP OCEAN ToCpptx
Women’s Talk Session 1- Talking about women
Digital Education Presentation for students.
430838499-Anaesthesiiiia-Equipmenooot.pptx
GPAT Presentation PPT and details about imp topics.pptx
Unit 3 Presentation Etiquette Business and Corporate Etiquette
notes_Lecture2 23l3j2 dfjl dfdlkj d 2.ppt
pwm ppt .pdf long description of pwm....
Final Second DC Messeting PPT-Pradeep.M final.pptx
_Dispute Resolution_July 2022.pptxmhhghhhh
Sheri Ann Lowe Compliance Strategist Resume
Slideham presentation for the students a
PRACTICE-TEST-12 is specially designed for those
CollegePresentation.pdf hsjsisjsjsjsssoo
Parts of Speech Quiz Presentation in Orange Blue Illustrative Style.pdf.pdf
employee on boarding for jobs for freshers try it
NO000387 (1).pptsbsnsnsnsnsnsnsmsnnsnsnsjsnnsnsnsnnsnnansnwjwnshshshs
servsafecomprehensive-ppt-full-140617222538-phpapp01.pdf

Functions

  • 2. Function and Procedure Parameter Types  Named PL/SQL programs (Procedures and Functions) can take parameters. Parameters are optional on both procedures and functions.  Keep the parameter name under 30 characters, they must start with a letter and contain no spaces.  There are 3 types of parameter-  IN, OUT and IN OUT 2
  • 3. Function and Procedure Parameter Types Cont…  An IN parameter is used as an input only. An IN parameter cannot be changed by the called program.  An OUT parameter is initially NULL. The program assigns the parameter a value and that value is returned to the calling program.  An IN OUT parameter may or may not have an initial value. That initial value may or may not be modified by the called program. Any changes made to the parameter are returned to the calling program. 3
  • 4. Function and Procedure Parameter Types Cont…  Parameters are declared with data types but without data type length or precision.  A parameter may be declared as VARCHAR2 but it will not be declared with a length component (VARCHAR2(30) would not be valid).  Parameters may also be assigned a default value.  You can either use the assignment operator (:=) or use the DEFAULT keyword. When a parameter has a default value, you do not need to include that parameter in the call. 4
  • 5. Sample Parameter Declarations  ( parameter_1 IN VARCHAR2 := 'ABC',  parameter_2 IN VARCHAR2 DEFAULT 'ABC',  parameter_3 IN OUT NUMBER, parameters_can_be_named_anything OUT DATE ) 5
  • 6. Procedure Example  CREATE OR REPLACE PROCEDURE my_first_proc (p_name IN VARCHAR2 := 'Lewis', p_address IN VARCHAR2 := '123 Mockingbird Ln', p_an_in_out_parameter IN OUT NUMBER, p_an_out_parameter OUT DATE ) IS v_a_variable VARCHAR2(30); BEGIN IF p_name = 'Lewis' THEN DBMS_OUTPUT.PUT_LINE( p_name || ': ' || p_address ); END IF; v_a_variable := 99; p_an_in_out_parameter := v_a_variable; p_an_out_parameter := SYSDATE; END; 6
  • 7. Function Example  CREATE OR REPLACE FUNCTION my_first_func (p_name IN VARCHAR2 := 'Lewis', p_address IN VARCHAR2 := '123 Mockingbird Ln', p_an_in_out_parameter IN OUT NUMBER, p_an_out_parameter OUT DATE ) RETURN VARCHAR2 IS v_a_variable VARCHAR2(30); BEGIN IF p_name = 'Lewis' THEN RETURN -1; END IF; v_a_variable := 99; p_an_in_out_parameter := v_a_variable; p_an_out_parameter := SYSDATE; RETURN v_a_variable; END; 7
  • 8. Procedures VS Functions  Procedures are traditionally the workhorse of the coding world.  Functions are traditionally the smaller, more specific pieces of code.  In general, if you need to update the chart of accounts, you would write a procedure.  If you need to retrieve the organization code for a particular GL account, you would write a function.  A Procedure cannot return a value but a Function MUST return a value. 8
  • 9. Procedures VS Functions Cont…  The return statement in a function returns control to the calling program and returns the results of the function.  The return statement of a procedure returns control to the calling program and cannot return a value.  Functions can be called from SQL, Procedure cannot.  Functions are considered expressions, Procedure are not.  Procedures and functions can both return data in OUT and IN OUT parameters. 9