Plsql - ProceduresA 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. A subprogram can be created:
A subprogram created inside a package is a packaged subprogram. It is stored in the database and can be deleted only when the package is deleted with the DROP PACKAGE statement. We will discuss packages in the chapter 'PL/SQL - Packages'. PL/SQL subprograms are named PL/SQL blocks that can be invoked with a set of parameters. PL/SQL provides two kinds of subprograms:
Parts of a PL/SQL Subprogram Each PL/SQL subprogram has a name, and may have a parameter list. Like anonymous PL/SQL blocks and, the named blocks a subprograms will also have following three parts:
A procedure is created with the CREATE OR REPLACE PROCEDURE statement. The simplified syntax for the CREATE OR REPLACE PROCEDURE statement is as follows:
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END procedure_name; Where,
The following example creates a simple procedure that displays the string 'Hello World!' on the screen when executed.
CREATE OR REPLACE PROCEDURE greetings
AS BEGIN dbms_output.put_line('Hello World!'); END; / When above code is executed using SQL prompt, it will produce the following result: Procedure created. Executing a Standalone Procedure A standalone procedure can be called in two ways:
EXECUTE greetings;
The above call would display:
Hello World
PL/SQL procedure successfully completed. The procedure can also be called from another PL/SQL block:
BEGIN
greetings ; END; / The above call would display:
Hello World
PL/SQL procedure successfully completed. Deleting a Standalone Procedure A standalone procedure is deleted with the DROP PROCEDURE statement. Syntax for deleting a procedure is:
DROP PROCEDURE procedure-name;
you can drop greetings procedure by using the following statement:
BEGIN
DROP PROCEDURE greetings; END; / Parameter Modes in PL/SQL Subprograms
This program finds the minimum of two values, here procedure takes two numbers using IN mode and returns their minimum using OUT parameters.
DECLARE
a number; b number; c number; PROCEDURE findMin(x IN number, y IN number, z OUT number) IS BEGIN IF x < y THEN z:= x; ELSE z:= y; END IF; END; 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:
Minimum of (23, 45) : 23
PL/SQL procedure successfully completed. IN & OUT Mode Example 2 This procedure computes the square of value of a passed value. This example shows how we can use same parameter to accept a value and then return another 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:
Square of (23): 529
PL/SQL procedure successfully completed. Methods for Passing Parameters Actual parameters could be passed in three ways:
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:
findMin(x=>a, y=>b, z=>c, m=>d);
MIXED NOTATION In mixed notation, you can mix both notations in procedure call; however, the positional notation should precede the named notation. The following call is legal:
findMin(a, b, c, m=>d);
But this is not legal:
findMin(x=>a, b, c, d);
|