Reading material Experiment 3.1
Reading material Experiment 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.
PL/SQL is a block-structured language that enables developers to combine the power of SQL
with procedural statements.
Terminologies:
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:
This parameter is used for giving input to the subprograms.
It is a read-only variable inside the subprograms. Their values cannot be changed
inside the subprogram.
In the calling statement, these parameters can be a variable or a literal value or an
expression, for example, it could be the arithmetic expression like '5*8' or 'a/b' where
'a' and 'b' are variables.
By default, the parameters are of IN type.
OUT Parameter:
This parameter is used 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 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.
Creating a Procedure
{IS | AS}
BEGIN
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 the above code is executed using the SQL prompt, it will produce the following result
−
Procedure created.
Executing a Procedure
The above procedure named 'greetings' can be called with the EXECUTE keyword as −
EXECUTE greetings;
Hello World
greetings;
END;
Hello World
Deleting a Procedure
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 −
Example:
This procedure computes the square of value of a passed value. This example shows how we
can use the same parameter to accept a value and then return another 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 −
A stored procedure in PL/SQL is nothing but a series of declarative SQL statements which
can be stored in the database catalogue. A procedure can be thought of as a function or a
method. They can be invoked through triggers, other procedures, or applications on Java,
PHP etc.
All the statements of a block are passed to Oracle engine all at once which increases
processing speed and decreases the traffic.
Advantages:
Disadvantages:
Stored procedures can cause a lot of memory usage. The database administrator
should decide an upper bound as to how many stored procedures are feasible for a
particular application.
MySQL does not provide the functionality of debugging the stored procedures.
References
Video References
#14. Oracle PL/SQL for Beginners - Stored Procedure (Hindi) - YouTube