0% found this document useful (0 votes)
6 views10 pages

Develop PLSQL Program Units - sg3 (1-10)

This document is the student guide for the Oracle Database 10g PL/SQL Program Units course, edition 3.0, published in April 2009. It contains proprietary information and is intended for classroom use only, prohibiting any unauthorized copying or distribution. The guide covers various topics related to PL/SQL programming, including procedures, functions, packages, dynamic SQL, and managing large objects.

Uploaded by

jaimeescalantep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Develop PLSQL Program Units - sg3 (1-10)

This document is the student guide for the Oracle Database 10g PL/SQL Program Units course, edition 3.0, published in April 2009. It contains proprietary information and is intended for classroom use only, prohibiting any unauthorized copying or distribution. The guide covers various topics related to PL/SQL programming, including procedures, functions, packages, dynamic SQL, and managing large objects.

Uploaded by

jaimeescalantep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Oracle University and ORACLE CORPORATION use only

Oracle Database 10g: Develop


PL/SQL Program Units

Volume III • Student Guide

D17169GC30
Edition 3.0
April 2009
D59278

THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS
COMPUTER IS STRICTLY PROHIBITED
Authors Copyright © 2009, Oracle. All rights reserved.

Salome Clement Disclaimer

Tulika Srivastava This document contains proprietary information and is protected by copyright and
Glenn Stokol other intellectual property laws. You may copy and print this document solely for your
own use in an Oracle training course. The document may not be modified or altered in
any way. Except where your use constitutes "fair use" under copyright law, you may
Technical Contributors not use, share, download, upload, copy, print, display, perform, reproduce, publish,
license, post, transmit, or distribute this document in whole or in part without the
and Reviewers express authorization of Oracle.
Don Bates
The information contained in this document is subject to change without notice. If you
Brian Boxx find any problems in the document, please report them in writing to: Oracle University,
Dr. Christoph Burandt 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not

Oracle University and ORACLE CORPORATION use only


Zarko Cesljas warranted to be error-free.

Yanti Chang Restricted Rights Notice


Kathryn Cunningham
If this documentation is delivered to the United States Government or anyone using
Brent Dayley the documentation on behalf of the United States Government, the following notice is
Burt Demchick applicable:
Laurent Dereac
U.S. GOVERNMENT RIGHTS
Peter Driver The U.S. Government’s rights to use, modify, reproduce, release, perform, display, or
Laura Garza disclose these training materials are restricted by the terms of the applicable Oracle
Nancy Greenberg license agreement and/or the applicable U.S. Government contract.

Craig Hollister Trademark Notice


Thomas Hoogerwerf
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other
Taj-Ul Islam names may be trademarks of their respective owners.
Yash Jain
Inger Joergensen
Chaitanya Koratamaddi
Eric Lee
Bryn Llewellyn
Malika Marghadi
Hildegard Mayr
Timothy Mcglue
Anita Mukundan
Nagavalli Pataballa
Sunitha Patel Graphic Designer
Srinivas Putrevu Priya Saxena
Denis Raphaely
Bryan Roberts Editors
Helen Robertson Joyce Raftery
Grant Spencer
Nita Pavitran
Glenn Stokol
Tone Thomas
Publishers
Priya Vennapusa
Michael Versaci Jobi Varghese
Lex Van Der Werff Sheryl Domingue

THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS
COMPUTER IS STRICTLY PROHIBITED
Contents

Preface

I Introduction

Oracle University and ORACLE CORPORATION use only


Lesson Objectives I-2
Course Objectives I-3
Course Agenda I-4
Human Resources (HR) Schema I-7
Creating a Modularized and Layered Subprogram Design I-8
Modularizing Development with PL/SQL Blocks I-9
Review of Anonymous Blocks I-10
Introduction to PL/SQL Procedures I-11
Introduction to PL/SQL Functions I-12
Introduction to PL/SQL Packages I-13
Introduction to PL/SQL Triggers I-14
PL/SQL Execution Environment I-15
PL/SQL Development Environments I-16
What Is Oracle SQL Developer? I-17
Creating a Database Connection I-18
Creating an Anonymous Block I-19
Coding PL/SQL in SQL*Plus I-20
Coding PL/SQL in Oracle JDeveloper I-21
Summary I-22
Practice I: Overview I-23

1 Creating Stored Procedures


Objectives 1-2
What Is a Procedure? 1-3
Syntax for Creating Procedures 1-4
Developing Procedures 1-5
What Are Parameters? 1-6
Formal and Actual Parameters 1-7
Procedural Parameter Modes 1-8
Using IN Parameters: Example 1-9
Using OUT Parameters: Example 1-10
Viewing OUT Parameters 1-11

iii
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS
COMPUTER IS STRICTLY PROHIBITED
Using IN OUT Parameters: Example 1-12
Syntax for Passing Parameters 1-13
Parameter Passing: Examples 1-14
Using the DEFAULT Option for Parameters 1-15
Summary of Parameter Modes 1-17
Invoking Procedures 1-18
Handled Exceptions 1-19
Handled Exceptions: Example 1-20

Oracle University and ORACLE CORPORATION use only


Exceptions Not Handled 1-21
Exceptions Not Handled: Example 1-22
Removing Procedures 1-23
Viewing Procedures in the Data Dictionary 1-24
Benefits of Subprograms 1-25
Summary 1-26
Practice 1: Overview 1-28

2 Creating Stored Functions


Objectives 2-2
Overview of Stored Functions 2-3
Syntax for Creating Functions 2-4
Developing Functions 2-5
Stored Function: Example 2-6
Ways to Execute Functions 2-7
Advantages of User-Defined Functions in SQL Statements 2-8
Function in SQL Expressions: Example 2-9
Locations to Call User-Defined Functions 2-10
Restrictions on Calling Functions from SQL Expressions 2-11
Controlling Side Effects When Calling Functions from SQL Expressions 2-12
Restrictions on Calling Functions from SQL: Example 2-13
Removing Functions 2-14
Viewing Functions in the Data Dictionary 2-15
Procedures Versus Functions 2-16
Summary 2-17
Practice 2: Overview 2-18

3 Creating Packages
Objectives 3-2
PL/SQL Packages: Overview 3-3
Components of a PL/SQL Package 3-4
Visibility of Package Components 3-5
Developing PL/SQL Packages 3-6

iv
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS
COMPUTER IS STRICTLY PROHIBITED
Creating the Package Specification 3-7
Example of Package Specification: comm_pkg 3-8
Creating the Package Body 3-9
Example of Package Body: comm_pkg 3-10
Invoking Package Subprograms 3-11
Creating and Using Bodiless Packages 3-12
Removing Packages 3-13
Viewing Packages in the Data Dictionary 3-14

Oracle University and ORACLE CORPORATION use only


Guidelines for Writing Packages 3-15
Advantages of Using Packages 3-16
Summary 3-18
Practice 3: Overview 3-20

4 Using More Package Concepts


Objectives 4-2
Overloading Subprograms 4-3
Overloading: Example 4-5
Overloading and the STANDARD Package 4-7
Using Forward Declarations 4-8
Package Initialization Block 4-10
Using Package Functions in SQL and Restrictions 4-11
Package Function in SQL: Example 4-12
Persistent State of Packages 4-13
Persistent State of Package Variables: Example 4-14
Persistent State of a Package Cursor 4-15
Executing CURS_PKG 4-16
Using PL/SQL Tables of Records in Packages 4-17
PL/SQL Wrapper 4-18
Running the Wrapper 4-19
Results of Wrapping 4-20
Guidelines for Wrapping 4-21
Summary 4-22
Practice 4: Overview 4-23

5 Using Oracle-Supplied Packages in Application Development


Objectives 5-2
Using Oracle-Supplied Packages 5-3
List of Some Oracle-Supplied Packages 5-4
How the DBMS_OUTPUT Package Works 5-5
Interacting with Operating System Files 5-6

v
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS
COMPUTER IS STRICTLY PROHIBITED
File Processing Using the UTL_FILE Package 5-7
Exceptions in the UTL_FILE Package 5-8
FOPEN and IS_OPEN Function Parameters 5-9
Using UTL_FILE: Example 5-10
Generating Web Pages with the HTP Package 5-12
Using the HTP Package Procedures 5-13
Creating an HTML File 5-14
Using UTL_MAIL 5-15

Oracle University and ORACLE CORPORATION use only


Installing and Using UTL_MAIL 5-16
Sending Email with a Binary Attachment 5-17
Sending Email with a Text Attachment 5-19
DBMS_SCHEDULER Package 5-21
Creating a Job 5-23
Creating a Job with In-Line Parameters 5-24
Creating a Job Using a Program 5-25
Creating a Job for a Program with Arguments 5-26
Creating a Job Using a Schedule 5-27
Setting the Repeat Interval for a Job 5-28
Creating a Job Using a Named Program and Schedule 5-29
Managing Jobs 5-30
Data Dictionary Views 5-31
Summary 5-32
Practice 5: Overview 5-33

6 Dynamic SQL and Metadata


Objectives 6-2
Execution Flow of SQL 6-3
Dynamic SQL 6-4
Native Dynamic SQL 6-5
Using the EXECUTE IMMEDIATE Statement 6-6
Dynamic SQL with a DDL Statement 6-7
Dynamic SQL with DML Statements 6-8
Dynamic SQL with a Single-Row Query 6-9
Dynamic SQL with a Multirow Query 6-10
Declaring Cursor Variables 6-11
Dynamically Executing a PL/SQL Block 6-12
Using Native Dynamic SQL to Compile PL/SQL Code 6-13
Using the DBMS_SQL Package 6-14
Using DBMS_SQL with a DML Statement 6-15
Using DBMS_SQL with a Parameterized DML Statement 6-16

vi
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS
COMPUTER IS STRICTLY PROHIBITED
Comparison of Native Dynamic SQL and the DBMS_SQL Package 6-17
DBMS_METADATA Package 6-18
Metadata API 6-19
Subprograms in DBMS_METADATA 6-20
FETCH_xxx Subprograms 6-21
SET_FILTER Procedure 6-22
Filters 6-23
Examples of Setting Filters 6-24

Oracle University and ORACLE CORPORATION use only


Programmatic Use: Example 1 6-25
Programmatic Use: Example 2 6-27
Browsing APIs 6-29
Browsing APIs: Examples 6-30
Summary 6-32
Practice 6: Overview 6-33

7 Design Considerations for PL/SQL Code


Objectives 7-2
Standardizing Constants and Exceptions 7-3
Standardizing Exceptions 7-4
Standardizing Exception Handling 7-5
Standardizing Constants 7-6
Local Subprograms 7-7
Definer’s Rights Versus Invoker’s Rights 7-8
Specifying Invoker’s Rights 7-9
Autonomous Transactions 7-10
Features of Autonomous Transactions 7-11
Using Autonomous Transactions 7-12
RETURNING Clause 7-13
Bulk Binding 7-14
Using Bulk Binding 7-15
Bulk Binding FORALL: Example 7-16
Using BULK COLLECT INTO with Queries 7-18
Using BULK COLLECT INTO with Cursors 7-19
Using BULK COLLECT INTO with a RETURNING Clause 7-20
Using the NOCOPY Hint 7-21
Effects of the NOCOPY Hint 7-22
NOCOPY Hint Can Be Ignored 7-23
PARALLEL_ENABLE Hint 7-24
Summary 7-25
Practice 7: Overview 7-26

vii
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS
COMPUTER IS STRICTLY PROHIBITED
8 Managing Dependencies
Objectives 8-2
Understanding Dependencies 8-3
Dependencies 8-4
Local Dependencies 8-5
A Scenario of Local Dependencies 8-7
Displaying Direct Dependencies by Using USER_DEPENDENCIES 8-8
Displaying Direct and Indirect Dependencies 8-9

Oracle University and ORACLE CORPORATION use only


Displaying Dependencies 8-10
Another Scenario of Local Dependencies 8-11
A Scenario of Local Naming Dependencies 8-12
Understanding Remote Dependencies 8-13
Concepts of Remote Dependencies 8-15
REMOTE_DEPENDENCIES_MODE Parameter 8-16
Remote Dependencies and Time Stamp Mode 8-17
Remote Procedure B Compiles at 8:00 AM 8-19
Local Procedure A Compiles at 9:00 AM 8-20
Execute Procedure A 8-21
Remote Procedure B Recompiled at 11:00 AM 8-22
Execute Procedure A 8-23
Signature Mode 8-24
Recompiling a PL/SQL Program Unit 8-25
Unsuccessful Recompilation 8-26
Successful Recompilation 8-27
Recompilation of Procedures 8-28
Packages and Dependencies 8-29
Summary 8-31
Practice 8: Overview 8-32

9 Manipulating Large Objects


Objectives 9-2
What Is a LOB? 9-3
Contrasting LONG and LOB Data Types 9-5
Anatomy of a LOB 9-6
Internal LOBs 9-7
Managing Internal LOBs 9-8
What Are BFILEs? 9-9
Securing BFILEs 9-10
A New Database Object: DIRECTORY 9-11
Guidelines for Creating DIRECTORY Objects 9-12

viii
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS
COMPUTER IS STRICTLY PROHIBITED
Managing BFILEs 9-13
Preparing to Use BFILEs 9-14
Populating BFILE Columns with SQL 9-15
Populating a BFILE Column with PL/SQL 9-16
Using DBMS_LOB Routines with BFILEs 9-17
Migrating from LONG to LOB 9-18
DBMS_LOB Package 9-20
DBMS_LOB.READ and DBMS_LOB.WRITE 9-23

Oracle University and ORACLE CORPORATION use only


Initializing LOB Columns Added to a Table 9-24
Populating LOB Columns 9-25
Updating LOB by Using DBMS_LOB in PL/SQL 9-26
Selecting CLOB Values by Using SQL 9-27
Selecting CLOB Values by Using DBMS_LOB 9-28
Selecting CLOB Values in PL/SQL 9-29
Removing LOBs 9-30
Temporary LOBs 9-31
Creating a Temporary LOB 9-32
Summary 9-33
Practice 9: Overview 9-34

10 Creating Triggers
Objectives 10-2
Types of Triggers 10-3
Guidelines for Designing Triggers 10-4
Creating DML Triggers 10-5
Types of DML Triggers 10-6
Trigger Timing 10-7
Trigger-Firing Sequence 10-8
Trigger Event Types and Body 10-10
Creating a DML Statement Trigger 10-11
Testing SECURE_EMP 10-12
Using Conditional Predicates 10-13
Creating a DML Row Trigger 10-14
Using OLD and NEW Qualifiers 10-15
Using OLD and NEW Qualifiers: Example Using AUDIT_EMP 10-16
Restricting a Row Trigger: Example 10-17
Summary of the Trigger Execution Model 10-18
Implementing an Integrity Constraint with a Trigger 10-19
INSTEAD OF Triggers 10-20

ix
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS
COMPUTER IS STRICTLY PROHIBITED
Creating an INSTEAD OF Trigger 10-21
Comparison of Database Triggers and Stored Procedures 10-24
Comparison of Database Triggers and Oracle Forms Triggers 10-25
Managing Triggers 10-26
Removing Triggers 10-27
Testing Triggers 10-28
Summary 10-29
Practice 10: Overview 10-30

Oracle University and ORACLE CORPORATION use only


11 Applications for Triggers
Objectives 11-2
Creating Database Triggers 11-3
Creating Triggers on DDL Statements 11-4
Creating Triggers on System Events 11-5
LOGON and LOGOFF Triggers: Example 11-6
CALL Statements 11-7
Reading Data from a Mutating Table 11-8
Mutating Table: Example 11-9
Benefits of Database Triggers 11-11
Managing Triggers 11-12
Business Application Scenarios for Implementing Triggers 11-13
Viewing Trigger Information 11-14
Using USER_TRIGGERS 11-15
Listing the Code of Triggers 11-16
Summary 11-17
Practice 11: Overview 11-18

12 Understanding and Influencing the PL/SQL Compiler


Objectives 12-2
Native and Interpreted Compilation 12-3
Features and Benefits of Native Compilation 12-4
Considerations When Using Native Compilation 12-5
Parameters Influencing Compilation 12-6
Switching Between Native and Interpreted Compilation 12-7
Viewing Compilation Information in the Data Dictionary 12-8
Using Native Compilation 12-9
Compiler Warning Infrastructure 12-10
Setting Compiler Warning Levels 12-11
Guidelines for Using PLSQL_WARNINGS 12-12
DBMS_WARNING Package 12-13

x
THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY. COPYING eKIT MATERIALS FROM THIS
COMPUTER IS STRICTLY PROHIBITED

You might also like