Oracle9: Program With PL/SQL: Student Guide - Volume 1
Oracle9: Program With PL/SQL: Student Guide - Volume 1
Oracle9: Program With PL/SQL: Student Guide - Volume 1
40054GC11
Production 1.1
October 2001
D34004
Authors Copyright © Oracle Corporation, 1999, 2000, 2001. All rights reserved.
Publisher
Sheryl Domingue
Contents
Curriculum Map
I Introduction
Course Objectives I-2
About PL/SQL I-3
PL/SQL Environment I-4
Benefits of PL/SQL I-5
Benefits of Subprograms I-10
Invoking Stored Procedures and Functions I-11
Summary I-12
1 Declaring Variables
Objectives 1-2
PL/SQL Block Structure 1-3
Executing Statements and PL/SQL Blocks 1-4
Block Types 1-5
Program Constructs 1-6
Use of Variables 1-7
Handling Variables in PL/SQL 1-8
Types of Variables 1-9
Using iSQL*Plus Variables Within PL/SQL Blocks 1-10
Types of Variables 1-11
Declaring PL/SQL Variables 1-12
Guidelines for Declaring PL/SQL Variables 1-13
Naming Rules 1-14
Variable Initialization and Keywords 1-15
Scalar Data Types 1-17
Base Scalar Data Types 1-18
Scalar Variable Declarations 1-22
The %TYPE Attribute 1-23
Declaring Variables with the %TYPE Attribute 1-24
Declaring Boolean Variables 1-25
Composite Data Types 1-26
LOB Data Type Variables 1-27
Bind Variables 1-28
Using Bind Variables 1-30
Referencing Non-PL/SQL Variables 1-31
DBMS_OUTPUT.PUT_LINE 1-32
Summary 1-33
Practice 1 Overview 1-35
iii
2 Writing Executable Statements
Objectives 2-2
PL/SQL Block Syntax and Guidelines 2-3
Identifiers 2-5
PL/SQL Block Syntax and Guidelines 2-6
Commenting Code 2-7
SQL Functions in PL/SQL 2-8
SQL Functions in PL/SQL: Examples 2-9
Data Type Conversion 2-10
Nested Blocks and Variable Scope 2-13
Identifier Scope 2-15
Qualify an Identifier 2-16
Determining Variable Scope 2-17
Operators in PL/SQL 2-18
Programming Guidelines 2-20
Indenting Code 2-21
Summary 2-22
Practice 2 Overview 2-23
iv
4 Writing Control Structures
Objectives 4-2
Controlling PL/SQL Flow of Execution 4-3
IF Statements 4-4
Simple IF Statements 4-5
Compound IF Statements 4-6
IF-THEN-ELSE Statement Execution Flow 4-7
IF-THEN-ELSE Statements 4-8
IF-THEN-ELSIF Statement Execution Flow 4-9
IF-THEN-ELSIF Statements 4-11
CASE Expressions 4-12
CASE Expressions: Example 4-13
Handling Nulls 4-15
Logic Tables 4-16
Boolean Conditions 4-17
Iterative Control: LOOP Statements 4-18
Basic Loops 4-19
WHILE Loops 4-21
FOR Loops 4-23
Guidelines While Using Loops 4-26
Nested Loops and Labels 4-27
Summary 4-29
Practice 4 Overview 4-30
v
6 Writing Explicit Cursors
Objectives 6-2
About Cursors 6-3
Explicit Cursor Functions 6-4
Controlling Explicit Cursors 6-5
Declaring the Cursor 6-9
Opening the Cursor 6-11
Fetching Data from the Cursor 6-12
Closing the Cursor 6-14
Explicit Cursor Attributes 6-15
The %ISOPEN Attribute 6-16
Controlling Multiple Fetches 6-17
The %NOTFOUND and %ROWCOUNT Attributes 6-18
Example 6-20
Cursors and Records 6-21
Cursor FOR Loops 6-22
Cursor FOR Loops Using Subqueries 6-24
Summary 6-26
Practice 6 Overview 6-27
8 Handling Exceptions
Objectives 8-2
Handling Exceptions with PL/SQL 8-3
Handling Exceptions 8-4
Exception Types 8-5
Trapping Exceptions 8-6
Trapping Exceptions Guidelines 8-7
Trapping Predefined Oracle Server Errors 8-8
Predefined Exceptions 8-11
Trapping Nonpredefined Oracle Server Errors 8-12
Nonpredefined Error 8-13
Functions for Trapping Exceptions 8-14
Trapping User-Defined Exceptions 8-16
User-Defined Exceptions 8-17
vi
Calling Environments 8-18
Propagating Exceptions 8-19
The RAISE_APPLICATION_ERROR Procedure 8-20
RAISE_APPLICATION_ERROR 8-22
Summary 8-23
Practice 8 Overview 8-24
9 Creating Procedures
Objectives 9-2
PL/SQL Program Constructs 9-4
Overview of Subprograms 9-5
Block Structure for Anonymous PL/SQL Blocks 9-6
Block Structure for PL/SQL Subprograms 9-7
PL/SQL Subprograms 9-8
Benefits of Subprograms 9-9
Developing Subprograms by Using iSQL*Plus 9-10
Invoking Stored Procedures and Functions 9-11
What Is a Procedure? 9-12
Syntax for Creating Procedures 9-13
Developing Procedures 9-14
Formal Versus Actual Parameters 9-15
Procedural Parameter Modes 9-16
Creating Procedures with Parameters 9-17
IN Parameters: Example 9-18
OUT Parameters: Example 9-19
Viewing OUT Parameters 9-21
IN OUT Parameters 9-22
Viewing IN OUT Parameters 9-23
Methods for Passing Parameters 9-24
DEFAULT Option for Parameters 9-25
Examples of Passing Parameters 9-26
Declaring Subprograms 9-27
Invoking a Procedure from an Anonymous PL/SQL Block 9-28
Invoking a Procedure from Another Procedure 9-29
Handled Exceptions 9-30
Unhandled Exceptions 9-32
Removing Procedures 9-34
Summary 9-35
Practice 9 Overview 9-37
10 Creating Functions
Objectives 10-2
Overview of Stored Functions 10-3
vii
Syntax for Creating Functions 10-4
Creating a Function 10-5
Creating a Stored Function by Using iSQL*Plus 10-6
Creating a Stored Function by Using iSQL*Plus: Example 10-7
Executing Functions 10-8
Executing Functions: Example 10-9
Advantages of User-Defined Functions in SQL Expressions 10-10
Invoking Functions in SQL Expressions: Example 10-11
Locations to Call User-Defined Functions 10-12
Restrictions on Calling Functions from SQL Expressions 10-13
Restrictions on Calling from SQL 10-15
Removing Functions 10-16
Procedure or Function? 10-17
Comparing Procedures and Functions 10-18
Benefits of Stored Procedures and Functions 10-19
Summary 10-20
Practice 10 Overview 10-21
11 Managing Subprograms
Objectives 11-2
Required Privileges 11-3
Granting Access to Data 11-4
Using Invoker's-Rights 11-5
Managing Stored PL/SQL Objects 11-6
USER_OBJECTS 11-7
List All Procedures and Functions 11-8
USER_SOURCE Data Dictionary View 11-9
List the Code of Procedures and Functions 11-10
USER_ERRORS 11-11
Detecting Compilation Errors: Example 11-12
List Compilation Errors by Using USER_ERRORS 11-13
List Compilation Errors by Using SHOW ERRORS 11-14
ESCRIBE in iSQL*Plus 11-15
Debugging PL/SQL Program Units 11-16
Summary 11-17
Practice 11 Overview 11-19
12 Creating Packages
Objectives 12-2
Overview of Packages 12-3
Components of a Package 12-4
Referencing Package Objects 12-5
Developing a Package 12-6
viii
Creating the Package Specification 12-8
Declaring Public Constructs 12-9
Creating a Package Specification: Example 12-10
Creating the Package Body 12-11
Public and Private Constructs 12-12
Creating a Package Body: Example 12-13
Invoking Package Constructs 12-15
Declaring a Bodiless Package 12-17
Referencing a Public Variable from a Stand-Alone Procedure 12-18
Removing Packages 12-19
Guidelines for Developing Packages 12-20
Advantages of Packages 12-21
Summary 12-23
Practice 12 Overview 12-26
x
Selecting CLOB Values by Using DBMS_LOB 15-29
Selecting CLOB Values in PL/SQL 15-30
Removing LOBs 15-31
Temporary LOBs 15-32
Creating a Temporary LOB 15-33
Summary 15-34
Practice 15 Overview 15-35
18 Managing Dependencies
Objectives 18-2
Understanding Dependencies 18-3
Dependencies 18-4
Local Dependencies 18-5
A Scenario of Local Dependencies 18-7
Displaying Direct Dependencies by Using USER_DEPENDENCIES 18-8
Displaying Direct and Indirect Dependencies 18-9
Displaying Dependencies 18-10
Another Scenario of Local Dependencies 18-11
A Scenario of Local Naming Dependencies 18-12
Understanding Remote Dependencies 18-13
Concepts of Remote Dependencies 18-15
REMOTE_DEPENDENCIES_MODE Parameter 18-16
Remote Dependencies and Time Stamp Mode 18-17
xii
Remote Procedure B Compiles at 8:00 a.m. 18-19
Local Procedure A Compiles at 9:00 a.m. 18-20
Execute Procedure A 18-21
Remote Procedure B Recompiled at 11:00 a.m. 18-22
Signature Mode 18-24
Recompiling a PL/SQL Program Unit 18-25
Unsuccessful Recompilation 18-26
Successful Recompilation 18-27
Recompilation of Procedures 18-28
Packages and Dependencies 18-29
Summary 18-31
Practice 18 Overview 18-32
A Practice Solutions
B Table Descriptions and Data
C Creating Program Units by Using Procedure Builder
D REF Cursors
Index
Additional Practices
Additional Practice Solutions
Additional Practices: Table Descriptions and Data
xiii
xiv