You are on page 1of 13

1

Overview of PL/SQL Subprograms

Copyright © Oracle Corporation, 2001. All rights


Course Objectives

After completing this course, you should be able to


do the following:
• Create, execute, and maintain procedures,
functions, packages, and database triggers
• Manage PL/SQL subprograms and triggers
• Describe Oracle-supplied packages
• Manipulate large objects (LOBs)

1-2 Copyright © Oracle Corporation, 2001. All rights


Lesson Objectives

After completing this lesson, you should be able to


do the following:
• Distinguish anonymous PL/SQL blocks from
named PL/SQL blocks (subprograms)
• Describe subprograms
• List the benefits of using subprograms
• List the different environments from which
subprograms can be invoked

1-3 Copyright © Oracle Corporation, 2001. All rights


Oracle Internet Platform
Clients Any mail
Any browser client Any FTP client
System management

Development tools
Internet applications

SQL
SQL
Business logic Presentation and
and data business logic
PL/SQL
PL/SQL

Application
Databases servers
Java
Java

Network services

1-4 Copyright © Oracle Corporation, 2001. All rights


PL/SQL Program Constructs
<header> IS|AS
or DECLARE

BEGIN

EXCEPTION
Database Server
Tools Constructs Constructs
Anonymous blocks END;
Anonymous blocks
Application procedures or
Stored procedures or
functions
functions
Application packages
Stored packages
Application triggers
Database triggers
Object types
Object types

1-5 Copyright © Oracle Corporation, 2001. All rights


Overview of Subprograms

A subprogram:
• Is a named PL/SQL block that can accept parameters
and be invoked from a calling environment
• Is of two types:
– A procedure that performs an action
– A function that computes a value
• Is based on standard PL/SQL block structure
• Provides modularity, reusability, extensibility,
and maintainability
• Provides easy maintenance, improved data security
and integrity, improved performance, and improved
code clarity

1-6 Copyright © Oracle Corporation, 2001. All rights


Block Structure for Anonymous
PL/SQL Blocks

DECLARE (optional)
Declare PL/SQL objects to be used
within this block
BEGIN (mandatory)
Define the executable statements
EXCEPTION (optional)
Define the actions that take place if
an error or exception arises
END; (mandatory)

1-7 Copyright © Oracle Corporation, 2001. All rights


Block Structure for PL/SQL Subprograms

<header>
Subprogram Specification
IS | AS
Declaration section
BEGIN
Executable section
Subprogram Body
EXCEPTION (optional)
Exception section
END;

1-8 Copyright © Oracle Corporation, 2001. All rights


PL/SQL Subprograms

----- --- --- ---


-----xxx
---xxx
--- xxx
--- ----- --- --- ---
xxx xxx xxx ----- --- --- ---
----- --- --- --- P
----- --- --- --- ----- --- --- ---
----- --- --- --- ----- --- --- ---
xxx xxx xxx xxx xxx xxx ----- --- --- ---
xxx xxx xxx xxx xxx xxx
----- --- --- --- P
----- --- --- --- Subprogram P, ----- --- --- ---
xxx xxx xxx which contains the ----- --- --- ---
xxx xxx xxx repeated code P
----- --- --- --- ----- --- --- ---
----- --- --- ---
----- --- --- --- PL/SQL program invoking
the subprogram at multiple
Code repeated more than locations
once in a PL/SQL program

1-9 Copyright © Oracle Corporation, 2001. All rights


Benefits of Subprograms

• Easy maintenance
• Improved data security and integrity
• Improved performance
• Improved code clarity

1-10 Copyright © Oracle Corporation, 2001. All rights


Developing Subprograms by Using
iSQL*Plus

2 3

1-11 Copyright © Oracle Corporation, 2001. All rights


Invoking Stored Procedures
and Functions

Scott LOG_EXECUTION
procedure
1 xxxxxxxxxxxxxx
vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
2 vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
vvvvvvvvvvvvvv
xxxxxxxxxxxxxx

xxxxxxxxxxxxxx
vvvvvvvvvvvvvv
3 vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
xxxxxxxxxxxxxx vvvvvvvvvvvvvv
Oracle Oracle Oracle vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
Portal Discoverer Forms vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
Developer vvvvvvvvvvvvvv
xxxxxxxxxxxxxx
vvvvvvvvvvvvvv

4
Scott

1-12 Copyright © Oracle Corporation, 2001. All rights


Summary

In this lesson, you should have learned that:


• Anonymous blocks are unnamed PL/SQL blocks.
• Subprograms are named PL/SQL blocks, declared
as either procedures or functions.
• You can create subprograms in iSQL*Plus using a
text editor.
• You can invoke subprograms from different
environments.

1-13 Copyright © Oracle Corporation, 2001. All rights

You might also like