You are on page 1of 35

chapter-4

Procedure and function

(PL/SQL object)
PROCEDURES/FUNCTIONS

 Set of SQL or PL/SQL statements which


are logically grouped together to perform
a specific task is known as Procedure or
Function.
 Procedures and Functions are named
PL/SQL programs that are stored in a
compiled form in the database. At the
time of creation, Oracle compiles the
procedure or function.
 Functions take zero or more parameters
and return a value. Procedures take zero
or more parameters and return no values.
Both functions and procedures can
receive or return zero or more values
through their parameter lists.
 The primary difference between
procedures and functions, other than the
return value, is how they are called.
Procedures are called as stand-alone
executable statements.
Advantages/Benefits of using Procedure and
Function :-

1. Security : Procedure and Function is


stored in the form of objects, so who are
granted to access those functions is
permit only.
2. Performance :
1. Amount of information sent over a network is less.
2. No need to compile each and every time whenever
it is called.
3. Reduction in disk I/O.
3. Memory Allocation/Requirement : It
provides reusability of code, so memory
requirement is less. Only one copy of
procedure needs to be loaded for
execution by multiple users. Once a copy
of a procedure or function is opened in
oracle engine’s memory, other users who
have appropriate permission may access
it when required.
4. Productivity : Redundant(
unnecessary) coding can be avoided,
increasing productivity.
[1] CREATING PROCEDURE :
Syntax (Procedure) :
CREATE [OR REPLACE] PROCEDURE
<Procedure Name>
(<Parameter List>) {is/as}
<declaration section>
BEGIN
<PL/SQL Code>
EXCEPTION section
<PL/SQL Code>
END;
 Where Procedure Name is a user-supplied
defined name that follows the rules used in
naming identifiers.
 The Parameter List has the names of
parameters passed to the procedure by the
calling program as well as the information
passed from the procedure to the calling
program.
 The local constants and variables are
declared after the reserved word IS/AS. If
there are no local identifiers to declare,
there is nothing between the reserved
words IS and BEGIN.
 The Executable statements are written after
BEGIN and before EXCEPTION or END.
Calling a Procedure :
 The procedure is called by specifying its
name along with the list of parameters (if
any) in parenthesis.

Procedurename [(parameter1,…..)];
For example :
p_sum(20,30);
Calling a procedure
Also, procedure can be called using
EXECUTE (EXEC) command:

For Example :

EXEC p_sum(20,30);
 SQL> create or replace procedure p1
 (n1 in number,n2 in number,n3 out
number)
 2 is PERAMETERS
PROCEDURE
 3 begin HEAD
 4 n3:=n1+n2;
 5 dbms_output.put_line('sum is = '||n3);
 6 end; PROCEDURE
 7 / BODY

 Procedure created.
CALLING A PROCEDURE
 Set serveroutput on

 SQL> declare
 2 n3 number;
 3 begin
 4 p1(10,20,n3);
 5 end; PROCEDURE
 6 / CALL
 SUM IS = 50

 PL/SQL procedure successfully completed.


CALLING A PROCEDURE
 Set serveroutput on

 declare
 2 a number:=&a;
 3 b number:=&b;
 4 n3 number;
 5 begin
 6 p1(a,b,n3);
 7 end;
 8 /
 Enter value for a: 30
 old 2: a number:=&a;
 new 2: a number:=30;
 Enter value for b: 70
 old 3: b number:=&b;
 new 3: b number:=70;
 sum is = 100

 PL/SQL procedure successfully completed.

 SQL> spool off


Procedure Header :
 The procedure definition that comes
before the reserved word IS or AS is
called the procedure header.
 The procedure header contains the name
of the procedure and the parameter list
with data types(if any).
 For example :
 create or replace procedure
p_sum(n1 in number, n2 in
number,n3
 out number)
 The parameter list in the header contains
the name of a parameter along with its
type.
 The parameter names used in the
procedure header do not have to be the
same as the names used in the call.
 The number of parameters in the call and
in the header must match, and the
parameters must be in the same order.
Procedure Body :

 The procedure body contains declarations,


executables, and exception-handling sections.
The declaration and exceptional sections are
optional. The executable section contains
action statements, and it must contain at
least one.
 The procedure body starts after the
reserved word IS or AS. If there is no local
declaration, IS/AS is followed by the
reserved word BEGIN. The body ends with
the reserved word END.
Parameters :
 Parameters are required to pass and to
receive value to and from the Procedure
and Functions.
 They are declared while defining
procedure and function
What is Mode in Procedure/Function :

 The mode describes whether the


parameter can be read from or written to
formal parameters.
 It can have 3 Modes:
1. IN
2. OUT
3. IN OUT
IN :
 For Read Only Access
 Parameter will accept value from the user.
Inside the procedure, it is READ ONLY
and cannot be changed. In assignment
statement, IN parameter can be put on
right side only. It is default mode.
OUT :
 For Write Only Access
 Parameter will return a value to the user
and value is copied to actual parameter. It
is WRITE ONLY and cannot be read from.
In Assignment statement, OUT parameter
can be put on left side only.
IN OUT :
 For Read – Write Access
 Parameter can either accept or return a
value.
USING IN MODE:-
USING OUT MODE:-
CALL THAT PROCEDURE:-
Take Note :
 IN parameter mode cannot be used at the left side of
the: = (equal to) sign means one cannot assign value to
it but can use its value. For ex. When variable “N1” is
IN parameter, one cannot write this way: N1:=10+10;
but can write N3:=N1+10;
 OUT parameter mode cannot be used at the right
side of the: = (equal to) sign means one cannot use its
value but can assign value to it. For ex. When variable
“N3” is OUT parameter, one cannot write like this
way: ANS: =N3+10; but can write N3:=10+10;
 INOUT parameter can be used at the both side of
the: = sign. For ex. When variable “N3” is INOUT
parameter, one can write N3:=10+10 and can also
write ANS: =N3+10;
[2] CREATING FUNCTION :

Syntax :
CREATE OR REPLACE FUNCTION <Fun
Name>
(<Parameter List>) RETURN <Data Type>
{is/as}
<declaration section>
BEGIN
<PL/SQL Code>
RETURN <expression>
EXCEPTION section
<PL/SQL Code>
END;
 The main difference between a function
and a procedure is that a function always
returns a value to the calling block.
 Primary use of a Function is to return a
value with an explicit RETURN statement.
 The body can contain more than one
RETURN statement, but only one is
executed with each function call.
Example:-
 SQL> create or replace FUNCTION f1 (n1 in
number,n2 in number) return number
 2 is
 3 n3 number; parameter Function
header
 4 begin
 5 n3:=n1+n2; Local variable
declaration
 6 return n3;
 7 end; Function
 8 / body

 Function created.
 Set serveroutput on
 SQL> declare
 2 x number:=&x;
3 y number:=&y;

 4 z number; Function
 5 begin
call
 6 z:=f1(x,y);
 7 dbms_output.put_line('total is ='||z);
 8 end;
 9 /
 Enter value for x: 10
 old 2: x number:=&x;
 new 2: x number:=10;
 Enter value for y: 20
 old 3: y number:=&y;
 new 3: y number:=20;

 PL/SQL procedure successfully completed.


 declare
 2 z number;
 3 begin
 4 z:=f1(10,20);
 5 dbms_output.put_line('total is = '||z);
 6 end;
 7 /
 total is = 30

 PL/SQL procedure successfully completed.

 SQL> spool off


Function Header :
 The function header comes before the
reserved word IS/AS. The header contains
the name of the function, the list of
parameters (if any), and the RETURN data
type.
 For example : create or replace function
emp_chk(eno in number) return varchar2
is enm emp.empname%type
Function Body :
 The body must contain at least one
executable statement. If there is no
declaration, the reserved word BEGIN
follows IS/AS.
 There can be more than one return
statement, but only one RETURN is
executed in a function call.
Calling a Function :
 You call a function by mentioning its name
along with its parameters (if any). A
Procedure does not have an explicit
RETURN statement,
 so a procedure call can be an independent
statement on a separate line.
 A function does return a value, so the
function call is made via an executable
statement, such as an assignment, selection,
or output statement.
 For example:
 total:=f_sum(&n1,&n2);
DROPPING PROCEDURE / FUNCTION

 DROP PROCEDURE
<Procedure_name>;
 DROP FUNCTION
<Function_name>;

 TAKE NOTE: USER_OBJECTS view


contains information about all the objects,
including subprograms.
Assignment question:-
1. What is Procedure? Explain it with
different Parameter Modes.
OR
Explain Stored Procedure in detail.
OR
1. What is Stored Procedure? How to create
and execute procedure? Explain with
example.
2. Explain Function in Oracle with
appropriate example.
3. Differentiate Procedure and Function.

You might also like