PLSQL

Parts of PL/SQL Subprogram

Parts of PL/SQL Subprogram

Each subprogram will have a name, and also has parameter list same like that of the anonymous PL/SQL blocks, these have three parts:

  1. Declarative part – This is said to be an optional part, also the declarative part of the subprograms will not start with the keyword DECLARE. Declarative part also consists of types, cursors, constants, variables, exceptions and also nested subprograms. These are said to be the local for any subprogram and they also cease to exist when the subprogram completes execution.
  2. Executable part – Executable part is said to be the mandatory part and also has statements that will perform the designated actions.
  3. Exception-handling – Exception-handling is said to be an optional part and also contains code that can handle run-time errors.

 Creating a Procedure

CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | INOUT] type [, ….])]
{IS | AS}
BEGIN
<procedure_Body>
END procedure_name;
Where procedure_name specifies the name of the procedure
[OR REPLACE] allows us for modification of an existing procedure.

procedure_Body contains executable part

AS keyword is used for creating standalone procedure instead of IS.

Executing the Standalone Procedure

This procedure can be called in two ways

  1. Using the keyword EXECUTE
  2. Calling the name of the procedure from the PL/SQL block.

 Deleting the standalone Procedure

 It is deleted by using the statement DROP PROCEDURE.

Syntax: DROP PROCEDURE procedure_name;

Parameter Modes in Subprograms

There are three parameter modes:

  1. IN – This parameter lets us to pass a value to the subprogram. This is known to be as read only parameter. It acts like a constant inside the subprogram. We cannot assign a value, we can pass a constant, literal, initialized variable or expression as an IN parameter. We can also initialize to the default value and omitted from the subprogram call. It is said to be default mode of parameter passing and parameters are said to be passes by reference.
  2. OUT – This returns a value to the calling program. This acts like a variable inside the subprogram. We are allowed to change its value and also the reference of the value after assigning it. Actual parameter is said to be a variable and it is passed by value.
  3. IN OUT – This passes an initial value to the subprogram and then returns an updated value to the caller. We can assign a value and this value can be read as well. Actual parameter should be a variable, not a constant or expression and also value should be assigned. Actual parameter is passed by a value.
Example: 
DECLARE 
   y number; 
PROCEDURE cubeNum(x IN OUT number) IS 
BEGIN 
  x := x * x*x; 
END;  
BEGIN 
   y:= 3; 
   squareNum(y); 
   dbms_output.put_line(' cube of (3): ' || y); 
END; 
/
Output:
cube of (3): 9 
PL/SQL procedure successfully completed.
Executing procedure
Execute cubeNum;
Output:
cube of (3): 9 
PL/SQL procedure successfully completed.
  • Deleting Procedure

DROP PROCEDURE cubeNum;

Example: 
DECLARE 
   y number; 
PROCEDURE cubeNum(x IN OUT number) IS 
BEGIN 
  x := x * x*x; 
END;  
BEGIN 
   y:= 3; 
   squareNum(y); 
   dbms_output.put_line(' cube of (3): ' || y); 
END; 
/
Output:
cube of (3): 9 
PL/SQL procedure successfully completed.
Executing procedure
Execute cubeNum;
Output:
cube of (3): 9 
PL/SQL procedure successfully completed.

Deleting Procedure
DROP PROCEDURE cubeNum;

Methods of Passing Parameters

Passing parameters can be done by three ways:

1.Positional Notation – in this 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. 

Example: findavg(a, b, c, d);

2.Named Notation – in this actual parameter is associated with the formal parameter using the arrow symbol (=>). Example: findavg(m=>a, n=>b, o=>c, p=>d);

3.Mixed Notation – in this notation we can mix both the above notations, positional notation will precede the named notations.

Example: findavg(a, b, c, p=>d); or findavg(m=>a, b, c, d);