Notes Chapter 3.1 Lecture 1.1 (Procedures)
Notes Chapter 3.1 Lecture 1.1 (Procedures)
UNIT-3
CHAPTER – 3.1
Procedures and Functions are the subprograms which can be created and saved in the
database as database objects. They can be called or referred inside the other blocks also.
Before we learn about PL/SQL subprograms, we will discuss the various terminologies that
are the part of these subprograms. Below are the terminologies that we are going to discuss.
Parameter:
The parameter is variable or placeholder of any valid PL/SQL datatype through which the
PL/SQL subprogram exchange the values with the main code. This parameter allows to give
input to the subprograms and to extract from these subprograms.
● These parameters should be defined along with the subprograms at the time of
creation.
● These parameters are included n the calling statement of these subprograms to interact
the values with the subprograms.
● The datatype of the parameter in the subprogram and the calling statement should be
same.
● The size of the datatype should not mention at the time of parameter declaration, as
the size is dynamic for this type.
1. IN Parameter
2. OUT Parameter
3. IN OUT Parameter
IN Parameter:
OUT Parameter:
● It is a read-write variable inside the subprograms. Their values can be changed inside
the subprograms.
● In the calling statement, these parameters should always be a variable to hold the
value from the current subprograms.
IN OUT Parameter:
● This parameter is used for both giving input and for getting output from the
subprograms.
● It is a read-write variable inside the subprograms. Their values can be changed inside
the subprograms.
● In the calling statement, these parameters should always be a variable to hold the
value from the subprograms.
These parameter type should be mentioned at the time of creating the subprograms.
RETURN
RETURN is the keyword that instructs the compiler to switch the control from the
subprogram to the calling statement. In subprogram RETURN simply means that the control
needs to exit from the subprogram. Once the controller finds RETURN keyword in the
subprogram, the code after this will be skipped.
Normally, parent or main block will call the subprograms, and then the control will shift from
those parent block to the called subprograms. RETURN in the subprogram will return the
control back to their parent block. In the case of functions RETURN statement also returns
the value. The datatype of this value is always mentioned at the time of function declaration.
The datatype can be of any valid PL/SQL data type.
Note: Subprogram is nothing but a procedure, and it needs to be created manually as per the
requirement. Once created they will be stored as database objects.
● Procedures are standalone blocks of a program that can be stored in the database.
● Call to these PLSQL procedures can be made by referring to their name, to execute
the PL/SQL statements.
● It can have nested blocks, or it can be defined and nested inside the other blocks or
packages.
● The values can be passed into Oracle procedure or fetched from the procedure
through parameters.
● A Procedure in SQL can have a RETURN statement to return the control to the
calling block, but it cannot return any values through the RETURN statement.
● Procedures cannot be called directly from SELECT statements. They can be called
from another block or through EXEC keyword.
Syntax:
<procedure_name>
(
..
[ IS | AS ]
<declaration_part>
BEGIN
<execution part>
EXCEPTION
END;
● Keyword 'IS' will be used, when the stored procedure in Oracle is nested into some
other blocks. If the procedure is standalone then 'AS' will be used. Other than this
coding standard, both have the same meaning.
In this example, we are going to create an Oracle procedure that takes the name as input and
prints the welcome message as output. We are going to use EXEC command to call
procedure.
IS
BEGIN
Code Explanation:
● Code line 1: Creating the procedure with name 'welcome_msg' and with one
parameter 'p_name' of 'IN' type.
● Code line 4: Printing the welcome message by concatenating the input name.
● Code line 7: Calling the procedure using EXEC command with the parameter
'Guru99'. Procedure is executed, and the message is printed out as "Welcome
Guru99".
What is Function?
● Functions are a standalone block that is mainly used for calculation purpose.
● Function use RETURN keyword to return the value, and the datatype of this is
defined at the time of creation.
● A Function should either return a value or raise the exception, i.e. return is mandatory
in functions.
● Function with no DML statements can be directly called in SELECT query whereas
the function with DML operation can only be called from other PL/SQL blocks.
● It can have nested blocks, or it can be defined and nested inside the other blocks or
packages.
● The values can be passed into the function or fetched from the procedure through the
parameters.
● These parameters should be included in the calling statement.
● A PLSQL function can also return the value through OUT parameters other than
using RETURN.
● Since it will always return the value, in calling statement it always accompanies with
assignment operator to populate the variables.
Syntax
<procedure_name>
RETURN <datatype>
[ IS | AS ]
<declaration_part>
BEGIN
<execution part>
EXCEPTION
END;
● 3CREATE FUNCTION instructs the compiler to create a new function. Keyword 'OR
REPLACE' instructs the compiler to replace the existing function (if any) with the
current one.
● Keyword 'IS' will be used, when the procedure is nested into some other blocks. If the
procedure is standalone then 'AS' will be used. Other than this coding standard, both
have the same meaning.
Example1: Creating Function and calling it using Anonymous Block
In this program, we are going to create a function that takes the name as input and returns the
welcome message as output. We are going to use anonymous block and select statement to
call the function.
IS
BEGIN
END;
DECLARE
lv_msg VARCHAR2(250);
BEGIN
dbms_output.put_line(lv_msg);
END;
Code Explanation:
● Code line 1: Creating the Oracle function with name 'welcome_msg_func' and with
one parameter 'p_name' of 'IN' type.
● Code line 5: Returning the concatenated value 'Welcome' and the parameter value.
● Code line 9: Declaring the variable with datatype same as the return datatype of the
function.
● Code line 11: Calling the function and populating the return value to the variable
'lv_msg'.
● Code line 12: Printing the variable value. The output you will get here is "Welcome
Guru99"
● Code line 14: Calling the same function through SELECT statement. The return value
is directed to the standard output directly.
● If the exception raised in the subprogram is not handled in the subprogram exception
handling section, then it will propagate to the calling block.
Creating a Procedure
{IS | AS}
BEGIN
END procedure_name;
Where,
● The optional parameter list contains name, mode and types of the
parameters. IN represents the value that will be passed from outside and OUT
represents the parameter that will be used to return a value outside of the procedure.
9*Example
The following example creates a simple procedure that displays the string 'Hello World!' on
the screen when executed.
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
When the above code is executed using the 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;
/
Hello World
A standalone procedure is deleted with the DROP PROCEDURE statement. Syntax for
deleting a procedure is −
You can drop the greetings procedure by using the following statement −
The following table lists out the parameter modes in PL/SQL subprograms −
1 IN
An IN parameter lets you pass a value to the subprogram. It is a read-only parameter. Inside
the subprogram, an IN parameter acts like a constant. It cannot be assigned a value. You can
pass a constant, literal, initialized variable, or expression as an IN parameter. You can also
initialize it to a default value; however, in that case, it is omitted from the subprogram call. It
is the default mode of parameter passing. Parameters are passed by reference.
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.
3 IN OUT
An IN OUT parameter passes an initial value to a subprogram and returns an updated value to
the caller. It can be assigned a value and the value can be read. The actual parameter
corresponding to an IN OUT formal parameter must be a variable, not a constant or an
expression. Formal parameter must be assigned a value. Actual parameter is passed by value.
IN & OUT Mode Example 1
This program finds the minimum of two values. Here, the procedure takes two numbers using
the IN mode and returns their minimum using the OUT parameters.
DECLARE
a number;
b number;
c number;
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
END;
When the above code is executed at the SQL prompt, it produces the following result −
DECLARE
a number;
BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
END;
When the above code is executed at the SQL prompt, it produces the following result −
● Positional notation
● Named notation
● Mixed notation
Positional Notation
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 ( => ). The procedure call will be like the following −
Mixed Notation
In mixed notation, you can mix both notations in procedure call; however, the positional
notation should precede the named notation.
OTHER REFRENCES