Vous êtes sur la page 1sur 10

Oracle9i: Advanced PL/SQL

Student Guide

40061GC10
Production 1.0
June 2002
D34761
Authors Copyright © Oracle Corporation, 2002. All rights reserved.

Nancy Greenberg This documentation contains proprietary information of Oracle Corporation. It is


provided under a license agreement containing restrictions on use and disclosure and
is also protected by copyright law. Reverse engineering of the software is prohibited.
If this documentation is delivered to a U.S. Government Agency of the Department of
Technical Contributors Defense, then it is delivered with Restricted Rights and the following legend is
and Reviewers applicable:

Laszlo Czinkoczki Restricted Rights Legend


Dan Gabel
Use, duplication or disclosure by the Government is subject to restrictions for
Ron Gosser commercial computer software and shall be deemed to be Restricted Rights software
Elizabeth Hall under Federal law, as set forth in subparagraph (c)(1)(ii) of DFARS 252.227-7013,
Craig Hollister Rights in Technical Data and Computer Software (October 1988).
Mozhe Jalali
This material or any portion of it may not be copied in any form or by any means
Thomas Kerstann without the express prior written permission of Oracle Corporation. Any other copying
Peter Van Leeuwen is a violation of copyright law and may result in civil and/or criminal penalties.
Bryn Llewellyn
If this documentation is delivered to a U.S. Government Agency not within the
Yi L. Lu Department of Defense, then it is delivered with “Restricted Rights,” as defined in
Nagavalli Pataballa FAR 52.227-14, Rights in Data-General, including Alternate III (June 1987).
Jasmin Robayo
The information in this document is subject to change without notice. If you find any
Helen Robertson
problems in the documentation, please report them in writing to Education Products,
John Soltani Oracle Corporation, 500 Oracle Parkway, Box SB-6, Redwood Shores, CA 94065.
Oracle Corporation does not warrant that this document is error-free.

All references to Oracle and Oracle products are trademarks or registered trademarks
Publisher of Oracle Corporation.

Hamsa Venkatachalam All other products or company names are used for identification purposes only, and
may be trademarks of their respective owners.
Contents

Preface
I Introduction
Course Objectives I-2
Oracle Complete Solution I-3
Course Agenda I-4
Course Agenda I-5
Tables Used in this Course I-6

1 PL/SQL Programming Concepts Review


Objectives 1-2
PL/SQL Program Constructs 1-3
Quiz Page 1 1-4
Summary 1-8

2 Design Considerations for PL/SQL Program Units


Objectives 2-2
Cursor Variables 2-3
Using a Cursor Variable 2-4
Step 1: Defining a REF CURSOR Type 2-5
Strong Versus Weak Cursors 2-6
Step 1: Declaring a Cursor Variable 2-7
Step 1: Declaring a REF CURSOR Return Type 2-8
Step 2: Opening a Cursor Variable 2-9
Step 3: Fetching from a Cursor Variable 2-11
Step 4: Closing a Cursor Variable 2-12
Passing Cursor Variables As Arguments 2-13
Rules for Cursor Variables 2-16
Comparing Cursor Variables to Static Cursors 2-17
Predefined Data Types 2-18
Subtypes 2-19
Benefits of Subtypes 2-21
Declaring Subtypes 2-22
Using Subtypes 2-23
Subtype Compatibility 2-24
Local Subprograms 2-25
The NOCOPY Compiler Hint 2-27
NOCOPY 2-28
Effects of NOCOPY 2-29
Restrictions on NOCOPY 2-30
More on Function Definitions 2-31
The DETERMINISTIC Hint 2-32
DETERMINISTIC 2-33
PARALLEL_ENABLE 2-34
Definer Rights Versus Invoker Rights 2-35
Invoker Rights 2-36
Autonomous Transactions 2-39
Summary 2-43
Practice Overview 2-44

iii
3 Design Considerations for PL/SQL Packages
Objectives 3-2
System Global Area (SGA) 3-3
The Shared Pool 3-4
Package Memory Issues 3-6
Reusing Package Memory 3-8
Rules for Pragma SERIALLY_REUSABLE 3-9
Restrictions on Package Functions Used in SQL 3-10
TRUST Keyword 3-12
Benefits of Overloading in Packages 3-14
Using Overloading 3-15
Guidelines for Cursor Design 3-16
Standardizing Constants 3-21
Standardizing Exceptions 3-22
PL/SQL Wrapper 3-26
Running the Wrapper 3-27
Examples of Wrapping 3-28
Guidelines for Wrapping 3-29
Summary 3-30
Practice Overview 3-31

4 Advanced Interface Methods


Objectives 4-2
Multi-Language Programs 4-3
Calling External Procedures from PL/SQL 4-4
Benefits of External Procedures 4-5
External C Procedure Components 4-6
How PL/SQL Calls a C External Procedure 4-7
The extproc Process 4-8
The Listener Process 4-9
Development Steps for External C Procedures 4-10
The Call Specification 4-14
Publishing an External C Routine 4-17
Executing the External Procedure 4-18
Creating Packages for External C Procedures 4-19
Overview of Java 4-22
How PL/SQL Calls a Java Class Method 4-23
Development Steps for Java Class Methods 4-24
Loading Java Class Methods 4-25
Publishing a Java Class Method 4-26
Executing the Java Routine 4-28
Creating Packages for Java Class Methods 4-29
Summary 4-30
Practice Overview 4-31

iv
5 Object-Oriented Principles Overview
Objectives 5-2
Overview 5-3
Definition of an Object 5-4
Objects Perform Operations 5-5
Objects Remember Values 5-6
Objects Are an Abstraction 5-7
Encapsulation 5-8
Guided Practice: Spot the Operations and Attributes 5-9
Object Hierarchies 5-10
Definition of an Object Type 5-11
Why Do We Need Object Types? 5-12
Identifying an Object Type 5-13
Object Types Versus Objects 5-14
Inheritance 5-15
The “Is-a-Kind-of” Relationship 5-17
Polymorphism 5-18
Summary 5-19
Practice 5 Overview 5-20

6 Working with Oracle Objects


Objectives 6-2
An Object Type 6-3
The Structure of an Object Type 6-5
Creating an Object Type Specification 6-7
Creating an Object Type Body 6-8
Creating an Object Type Specification 6-9
Creating an Object Type Body 6-10
SELF Parameter 6-11
Calling Object Methods 6-13
Using the Constructor Method 6-14
Creating a Complex Object Type Specification 6-15
Using an Object Type Specification 6-16
Creating a Complex Object Type Body 6-17
Using Methods in PL/SQL 6-19
Selecting from Object Tables 6-21
Using REF Type Modifier and the REF and DEREF Functions 6-23
Using REF and DEREF 6-24
The INSERT Statement with Object Tables 6-25
The UPDATE Statement with Object Tables 6-26
The DELETE Statement with Object Tables 6-27
Type Inheritance 6-28
Creating Object Subtypes 6-30

v
Using Object Subtypes 6-32
Data Dictionary Views for Objects 6-34
Data Dictionary Views for Object Type Methods 6-35
Summary 6-36
Practice Overview 6-37

7 Working with Collections


Objectives 7-2
Collections 7-3
Collection Types 7-4
Nested Tables and VARRAYs Characteristics 7-5
Creating Collection Types 7-6
Declaring Collections: Nested Table 7-7
Nested Table Storage 7-8
Declaring Collections: VARRAY 7-9
Working with Collections in PL/SQL 7-10
Initializing Collections 7-11
Referencing Collection Elements 7-12
Using Collection Methods 7-13
Manipulating Individual Elements 7-16
Avoiding Collection Exceptions 7-17
Working with Collections in SQL 7-19
Using Collections Effectively 7-23
Comparing PL/SQL Collections to PL/SQL Index-By Tables 7-24
Guidelines for Index-By Tables 7-25
Summary 7-26
Practice Overview 7-27

8 Performance and Tuning


Objectives 8-2
Tuning PL/SQL Code 8-3
The RETURNING Clause 8-4
Bulk Binding 8-6
Handling FORALL Exceptions 8-11
Rephrase Conditional Control Statements 8-14
Avoid Implicit Data Type Conversion 8-15
Use PLS_INTEGER Data Type for Integers 8-17
Use Index-By Tables of Records and Objects 8-18
Use User-Defined Records As Parameters 8-19
Use Collections As Arguments 8-21
The NOT NULL Constraint 8-22
Native Compilation of PL/SQL 8-23

vi
Native Dynamic SQL 8-25
Advantages of Native Dynamic SQL Over dbms_sql 8-28
SQL Versus PL/SQL 8-30
Tracing PL/SQL Execution 8-33
Tracing PL/SQL: Steps 8-36
Step 1: Enable Specific Subprograms 8-37
Steps 2 and 3: Identify a Trace Level and Start Tracing 8-38
Step 4: Turn Off Tracing 8-39
Step 5: Examine the Trace Information 8-40
plsql_trace_runs and plsql_trace_events 8-41
Profiling PL/SQL Applications 8-43
Profiling PL/SQL: Steps 8-45
Profiling Example 8-46
Identify and Tune Memory Issues 8-49
Pinning Objects 8-50
Network Issues 8-54
Summary 8-57
Practice Overview 8-58

9 Using the Advanced Features of the Oracle Supplied Packages


Objectives 9-2
Oracle Supplied Packages 9-3
Using dbms_session 9-4
dbms_session Subprograms 9-5
Verifying and Changing Roles 9-6
Alter Session Example 9-7
Managing Resources 9-8
The dbms_lock Package 9-13
Using dbms_lock 9-14
dbms_lock Programs 9-15
Lock Modes 9-16
Allocating a Lock 9-17
Requesting a Lock 9-19
Converting a Lock 9-22
Releasing a Lock 9-23
Suspending a Session 9-24
Sending E-mail Programmatically 9-25
utl_smtp Subprograms 9-26
Setting Up E-mail Basics 9-27
Setting the Subject, To, and From Parameters 9-28

vii
utl_smtp Exceptions and Limitations 9-30
Summary 9-31
Practice Overview 9-32

10 PL/SQL Server Pages


Objectives 10-2
PSP: Uses and Features 10-3
Format of the PSP File 10-4
Development Steps for PSP 10-6
Printing the Table Using a Loop 10-12
Specifying a Parameter 10-13
Using an HTML Form to Call a PSP 10-16
Debugging PSP Problems 10-18
Summary 10-20
Practice Overview 10-21

11 Fine-Grained Access Control


Objectives 11-2
Overview 11-3
Features 11-4
How Fine Grained Access Works 11-5
Application Context 11-7
Creating a Context 11-9
Setting a Context 11-10
DBMS_RLS Package 11-11
Implementing a Policy 11-12
Step 2: Create the Package 11-13
Step 3: Define the Policy 11-16
Step 4: Set up a Logon Trigger 11-19
Example Results 11-20
Data Dictionary Views 11-21
USER_CONTEXT Dictionary View 11-22
Policy Groups 11-23
More on Policies 11-24
Summary 11-26
Practice Overview 11-27

viii
Appendix A - PL/SQL Quiz Solutions

Appendix B - PL/SQL Programming Concepts: Review


Objectives B-2
Block Structure for PL/SQL Subprograms B-3
Creating Procedures B-4
Creating Functions B-6
Restrictions on Calling Functions from SQL Expressions B-8
Calling PL/SQL Functions in SQL Statements B-10
Creating the Package Specification B-11
Creating the Package Body B-13
Cursors B-15
Processing Explicit Cursors B-16
Explicit Cursor Attributes B-17
Using Cursors B-18
Cursor FOR Loops B-19
Using Cursor FOR Loops B-20
Handling Exceptions B-21
Trapping Exceptions B-23
Trapping Predefined Oracle Server Errors B-24
Trapping a Nonpredefined Error B-26
Trapping a User-Defined Exception B-28
raise_application_error Procedure B-30
Understanding Dependencies B-32
Dependencies B-33
Displaying Direct Dependencies Using user_dependencies B-34
Displaying Direct and Indirect Dependencies B-35
Oracle Supplied Packages B-37
Summary B-38

Appendix C - Practice Solutions

ix
Appendix D – Table Descriptions

Appendix E - Advanced Queuing


Objectives E-2
Overview E-3
Storage Management for Messages E-4
Example: Trading of Shares Without Messaging E-5
Example: Trading of Shares with Messaging E-6
Features of Oracle Advanced Queuing E-7
Elements of Advanced Queuing E-8
Queue Tables E-10
Message Queues E-11
Messages E-12
Agents E-13
Time Manager E-14
DBMS_AQ Package E-15
DBMS_AQ.ENQUEUE Procedure E-16
DBMS_AQ.ENQUEUE Parameters: QUEUE_NAME E-17
DBMS_AQ.ENQUEUE Parameters: ENQUEUE_OPTIONS E-18
DBMS_AQ.ENQUEUE Parameters: Example for ENQUEUE_OPTIONS E-19
DBMS_AQ.ENQUEUE Parameters: MESSAGE_PROPERTIES_T E-20
DBMS_AQ.ENQUEUE Parameters: Example for MESSAGE_PROPERTIES_T E-22
DBMS_AQ.ENQUEUE Arguments: PAYLOAD and MSGID E-23
DBMS_AQ.DEQUEUE Procedure E-24
DBMS_AQ.DEQUEUE Parameters: QUEUE_NAME E-26
DBMS_AQ.DEQUEUE Parameters: DEQUEUE_OPTIONS E-27
DBMS_AQ.DEQUEUE Parameters: Example for DEQUEUE_OPTIONS E-29
DBMS_AQ.DEQUEUE Parameters: MESSAGE_PROPERTIES E-30
DBMS_AQ.DEQUEUE Parameters: PAYLOAD and MSGID E-31
DBMS_AQADM Package E-32
DBMS_AQADM Basics E-33
Data Dictionary Views E-35
Steps to Using Advanced Queuing E-36
Example E-37
Summary E-39
Practice Overview E-40

Vous aimerez peut-être aussi