0% found this document useful (0 votes)
5 views

Reading material Experiment 3.1

The document explains PL/SQL subprograms, specifically procedures and functions, which are stored in the database and can be called within other blocks. It details the types of parameters (IN, OUT, IN OUT) used for data exchange between the main code and subprograms, as well as the syntax for creating, executing, and deleting procedures. Additionally, it discusses the advantages and disadvantages of using stored procedures in PL/SQL.

Uploaded by

Abhishek Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Reading material Experiment 3.1

The document explains PL/SQL subprograms, specifically procedures and functions, which are stored in the database and can be called within other blocks. It details the types of parameters (IN, OUT, IN OUT) used for data exchange between the main code and subprograms, as well as the syntax for creating, executing, and deleting procedures. Additionally, it discusses the advantages and disadvantages of using stored procedures in PL/SQL.

Uploaded by

Abhishek Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Procedures:

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.

Based on their purpose parameters are classified as

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

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,

 procedure-name specifies the name of the procedure.


 [OR REPLACE] option allows the modification of an existing procedure.
 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.
 procedure-body contains the executable part.
 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.
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

A standalone procedure can be called in two ways −

 Using the EXECUTE keyword


 Calling the name of the procedure from a PL/SQL block

The above procedure named 'greetings' can be called with the EXECUTE keyword as −

EXECUTE greetings;

The above call will 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 will display −

Hello World

PL/SQL procedure successfully completed.

Deleting a Procedure

A standalone procedure is deleted with the DROP PROCEDURE statement. Syntax for
deleting a procedure is −

DROP PROCEDURE procedure-name;

You can drop the greetings procedure by using the following statement −

DROP PROCEDURE greetings;

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;

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 the SQL prompt, it produces the following result −

Square of (23): 529

PL/SQL procedure successfully completed.

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:

 They result in performance improvement of the application. If a procedure is being


called frequently in an application in a single connection, then the compiled version of
the procedure is delivered.
 They reduce the traffic between the database and the application, since the lengthy
statements are already fed into the database and need not be sent again and again via
the application.
 They add to code reusability, similar to how functions and methods work in other
languages such as C/C++ and Java.

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

 Oracle PL/SQL Stored Procedure & Functions with Examples (guru99.com)

 PL/SQL - Procedures - Tutorialspoint

 SQL | Procedures in PL/SQL - GeeksforGeeks

Video References
 #14. Oracle PL/SQL for Beginners - Stored Procedure (Hindi) - YouTube

You might also like