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

Procedures

Uploaded by

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

Procedures

Uploaded by

devileela921
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Sub program & Procedures

abhirami t
• A 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 −

• At the schema level


• Inside a package
• Inside a PL/SQL block
Schema level

• At the schema level, subprogram is a standalone subprogram.


• schema typically refers to the collection of database objects (such
as tables, views, procedures, functions, triggers, etc.)
• It is created with the CREATE PROCEDURE or the CREATE
FUNCTION statement.
• It is stored in the database and can be deleted with the DROP
PROCEDURE or DROP FUNCTION statement.
Inside Package

• 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.
Inside a PL/SQL block

• PL/SQL subprograms are named PL/SQL blocks that can


be invoked with a set of parameters.
PL/SQL provides two kinds of subprograms −

• Functions − These subprograms return a single value;


mainly used to compute and return a value.

• Procedures − These subprograms do not return a value


directly; mainly used to perform an action.
Parts of a PL/SQL Subprogram
• Each PL/SQL subprogram has a name, and may also
have a parameter list. Like anonymous PL/SQL blocks,
the named blocks will also have the following three
parts −

• 1 Declarative Part
• 2 Executable Part
• 3 Exception-handling
PROCEDURE
• Procedure is a named PL/SQL block that performs one or more
specific tasks.
• It is a stored subprogram that can be called explicitly by other
programs or procedures.
• Procedures in PL/SQL help in modularizing and organizing
code, promoting code reusability, and enhancing
maintainability.
• A procedure is created with the CREATE OR REPLACE
PROCEDURE statement.
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
END procedure_name;
• 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.An INOUT
parameter passes an initial value to a subprogram and returns an updated value
to the caller

• procedure-body contains the executable part.

• The AS keyword is used instead of the IS keyword for creating a standalone


procedure.
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.
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 complete
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 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 the greetings procedure by using the following


statement −

DROP PROCEDURE greetings;


Methods for Passing Parameters

Actual parameters can be passed in three ways −

• Positional notation
• Named notation
• Mixed notation
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 ( =>
).

• The procedure call will be like the following −

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);

However, this is not legal:

• findMin(x => a, b, c, d);


THANKYOU

You might also like