PLSQL Procedures
PLSQL Procedures
A subprogram is a program unit/module that performs a particular task. These subprograms are
combined to form larger programs. This is basically called the 'Modular design'. A subprogram can
be invoked by another subprogram or program which is called the calling program.
At schema level
Inside a package
PL/SQL subprograms are named PL/SQL blocks that can be invoked with a set of parameters.
PL/SQL provides two kinds of subprograms:
Functions: these subprograms return a single value, mainly used to compute and return a
value.
Procedures: these subprograms do not return a value directly, mainly used to perform an
action.
This chapter is going to cover important aspects of a PL/SQL procedure and we will cover PL/SQL
function in next chapter.
1 Declarative Part
It is an optional part. However, the declarative part for a subprogram does not start with
the DECLARE keyword. It contains declarations of types, cursors, constants, variables,
exceptions, and nested subprograms. These items are local to the subprogram and cease
to exist when the subprogram completes execution.
2 Executable Part
This is a mandatory part and contains statements that perform the designated action.
3 Exception-handling
This is again an optional part. It contains the code that handles run-time errors.
Creating a Procedure
A procedure is created with the CREATE OR REPLACE PROCEDURE statement. The simplified
syntax for the CREATE OR REPLACE PROCEDURE statement is as follows:
Where,
The optional parameter list contains name, mode and types of the parameters. IN represents
that value will be passed from outside and OUT represents that this parameter will be used to
return a value outside of the procedure.
The AS keyword is used instead of the IS keyword for creating a standalone procedure.
Example:
The following example creates a simple procedure that displays the string 'Hello World!' on the
screen when executed.
When above code is executed using SQL prompt, it will produce the following result:
Procedure created.
The above procedure named 'greetings' can be called with the EXECUTE keyword as:
EXECUTE greetings;
Hello World
BEGIN
greetings;
END;
/
The above call would display:
Hello World
1 IN
2 OUT
An OUT parameter returns a value to the calling program. Inside the subprogram, an OUT
parameter acts like a variable. You can change its value and reference the value after
assigning it. The actual parameter must be variable and it is passed by value.
2 IN OUT
DECLARE
a number;
b number;
c number;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
/
When the above code is executed at SQL prompt, it produces the following result:
DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS
BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a);
END;
/
When the above code is executed at SQL prompt, it produces the following result:
Positional notation
Named notation
Mixed notation
POSITIONAL NOTATION
In positional notation, you can call the procedure as:
findMin(a, b, c, d);
In positional notation, the first actual parameter is substituted for the first formal parameter; the
second actual parameter is substituted for the second formal parameter, and so on. So, a is
substituted for x, b is substituted for y, c is substituted for z and d is substituted for m.
NAMED NOTATION
In named notation, the actual parameter is associated with the formal parameter using the arrow
symbol => . So the procedure call would look like:
MIXED NOTATION
In mixed notation, you can mix both notations in procedure call; however, the positional notation
should precede the named notation.
findMin(a, b, c, m=>d);
findMin(x=>a, b, c, d);
Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js