PL/SQL User’s Guide and Reference - Oracle

PL/SQL

User's Guide and Reference 10g Release 1 (10.1)

Part No. B10807-01

December 2003

PL/SQL User's Guide and Reference, 10g Release 1 (10.1)

Part No. B10807-01

Copyright ? 1996, 2003 Oracle. All rights reserved.

Primary Author: John Russell

Contributors: Shashaanka Agrawal, Cailein Barclay, Dmitri Bronnikov, Sharon Castledine, Thomas Chang, Ravindra Dani, Chandrasekharan Iyer, Susan Kotsovolos, Neil Le, Warren Li, Bryn Llewellyn, Chris Racicot, Murali Vemulapati, Guhan Viswanathan, Minghui Yang

The Programs (which include both the software and documentation) contain proprietary information; they are provided under a license agreement containing restrictions on use and disclosure and are also protected by copyright, patent, and other intellectual and industrial property laws. Reverse engineering, disassembly, or decompilation of the Programs, except to the extent required to obtain interoperability with other independently created software or as specified by law, is prohibited.

The information contained in this document is subject to change without notice. If you find any problems in the documentation, please report them to us in writing. This document is not warranted to be error-free. Except as may be expressly permitted in your license agreement for these Programs, no part of these Programs may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose.

If the Programs are delivered to the United States Government or anyone licensing or using the Programs on behalf of the United States Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data delivered to U.S. Government customers are "commercial computer software" or "commercial technical data" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the Programs, including documentation and technical data, shall be subject to the licensing restrictions set forth in the applicable Oracle license agreement, and, to the extent applicable, the additional rights set forth in FAR 52.227-19, Commercial Computer Software--Restricted Rights (June 1987). Oracle Corporation, 500 Oracle Parkway, Redwood City, CA 94065

The Programs are not intended for use in any nuclear, aviation, mass transit, medical, or other inherently dangerous applications. It shall be the licensee's responsibility to take all appropriate fail-safe, backup, redundancy and other measures to ensure the safe use of such applications if the Programs are used for such purposes, and we disclaim liability for any damages caused by such use of the Programs.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

The Programs may provide links to Web sites and access to content, products, and services from third parties. Oracle is not responsible for the availability of, or any content provided on, third-party Web sites. You bear all risks associated with the use of such content. If you choose to purchase any products or services from a third party, the relationship is directly between you and the third party. Oracle is not responsible for: (a) the quality of third-party products or services; or (b) fulfilling any of the terms of the agreement with the third party, including delivery of products or services and warranty obligations related to purchased products or services. Oracle is not responsible for any loss or damage of any sort that you may incur from dealing with any third party.

Contents

Send Us Your Comments ...................................................................................................................... xv

Preface .............................................................................................................................................................. xvii

Audience.................................................................................................................................................... xvii How This Book Is Organized.................................................................................................................. xvii Related Documentation ........................................................................................................................... xix Conventions ............................................................................................................................................... xx Sample Database Tables .......................................................................................................................... xxi Documentation Accessibility .................................................................................................................. xxii Reading the Syntax Diagrams ................................................................................................................ xxii

What's New in PL/SQL?......................................................................................................................... xxiii

New Features in PL/SQL for Oracle Database 10g ........................................................................... xxiii New Features in PL/SQL for Oracle9i ................................................................................................ xxvi

1 Overview of PL/SQL

Advantages of PL/SQL ............................................................................................................................ 1-1 Tight Integration with SQL .............................................................................................................. 1-1 Support for SQL.................................................................................................................................. 1-2 Better Performance............................................................................................................................. 1-2 Higher Productivity ........................................................................................................................... 1-3 Full Portability .................................................................................................................................... 1-3 Tight Security...................................................................................................................................... 1-3 Support for Object-Oriented Programming ................................................................................... 1-3

Understanding the Main Features of PL/SQL .................................................................................... 1-4 Block Structure.................................................................................................................................... 1-4 Variables and Constants.................................................................................................................... 1-5 Processing Queries with PL/SQL.................................................................................................... 1-6 Declaring PL/SQL Variables............................................................................................................ 1-6 Control Structures .............................................................................................................................. 1-7 Writing Reusable PL/SQL Code...................................................................................................... 1-9 Data Abstraction.............................................................................................................................. 1-10 Error Handling ................................................................................................................................ 1-12

PL/SQL Architecture ............................................................................................................................. 1-12 In the Oracle Database Server ....................................................................................................... 1-13

iii

In Oracle Tools................................................................................................................................. 1-14

2 Fundamentals of the PL/SQL Language

Character Set.............................................................................................................................................. 2-1 Lexical Units .............................................................................................................................................. 2-1

Delimiters ............................................................................................................................................ 2-2 Identifiers............................................................................................................................................. 2-3 Literals ................................................................................................................................................. 2-4 Comments ........................................................................................................................................... 2-7 Declarations............................................................................................................................................... 2-8 Using DEFAULT ................................................................................................................................ 2-9 Using NOT NULL .............................................................................................................................. 2-9 Using the %TYPE Attribute .............................................................................................................. 2-9 Using the %ROWTYPE Attribute ................................................................................................. 2-10 Restrictions on Declarations .......................................................................................................... 2-12 PL/SQL Naming Conventions ............................................................................................................ 2-12 Scope and Visibility of PL/SQL Identifiers...................................................................................... 2-14 Assigning Values to Variables ............................................................................................................ 2-16 Assigning Boolean Values ............................................................................................................. 2-17 Assigning a SQL Query Result to a PL/SQL Variable .............................................................. 2-17 PL/SQL Expressions and Comparisons............................................................................................. 2-17 Logical Operators ............................................................................................................................ 2-18 Boolean Expressions ....................................................................................................................... 2-21 CASE Expressions ........................................................................................................................... 2-24 Handling Null Values in Comparisons and Conditional Statements ..................................... 2-25 Summary of PL/SQL Built-In Functions........................................................................................... 2-28

3 PL/SQL Datatypes

Overview of Predefined PL/SQL Datatypes........................................................................................ 3-1 PL/SQL Number Types .................................................................................................................... 3-2 PL/SQL Character and String Types .............................................................................................. 3-4 PL/SQL National Character Types ................................................................................................. 3-8 PL/SQL LOB Types ........................................................................................................................ 3-10 PL/SQL Boolean Types.................................................................................................................. 3-11 PL/SQL Date, Time, and Interval Types ..................................................................................... 3-12 Datetime and Interval Arithmetic................................................................................................. 3-15 Avoiding Truncation Problems Using Date and Time Subtypes............................................. 3-16

Overview of PL/SQL Subtypes .......................................................................................................... 3-16 Defining Subtypes .......................................................................................................................... 3-16 Using Subtypes ............................................................................................................................... 3-17

Converting PL/SQL Datatypes............................................................................................................ 3-18 Explicit Conversion......................................................................................................................... 3-18 Implicit Conversion ........................................................................................................................ 3-18 Choosing Between Implicit and Explicit Conversion ................................................................ 3-20 DATE Values.................................................................................................................................... 3-20 RAW and LONG RAW Values ..................................................................................................... 3-20

iv

4 Using PL/SQL Control Structures

Overview of PL/SQL Control Structures ............................................................................................. 4-1 Testing Conditions: IF and CASE Statements .................................................................................... 4-2

Using the IF-THEN Statement ......................................................................................................... 4-2 Using the IF-THEN-ELSE Statement............................................................................................... 4-2 Using the IF-THEN-ELSIF Statement.............................................................................................. 4-3 Using the CASE Statement ............................................................................................................... 4-3 Guidelines for PL/SQL Conditional Statements........................................................................... 4-5 Controlling Loop Iterations: LOOP and EXIT Statements............................................................... 4-6 Using the LOOP Statement............................................................................................................... 4-6 Using the EXIT Statement ................................................................................................................. 4-7 Using the EXIT-WHEN Statement................................................................................................... 4-7 Labeling a PL/SQL Loop .................................................................................................................. 4-7 Using the WHILE-LOOP Statement................................................................................................ 4-8 Using the FOR-LOOP Statement ..................................................................................................... 4-9 Sequential Control: GOTO and NULL Statements ........................................................................ 4-12 Using the GOTO Statement ........................................................................................................... 4-12 Using the NULL Statement............................................................................................................ 4-13

5 Using PL/SQL Collections and Records

What Is a Collection? ............................................................................................................................... 5-1 Understanding Nested Tables.......................................................................................................... 5-2 Understanding Varrays..................................................................................................................... 5-2 Understanding Associative Arrays (Index-By Tables) ................................................................. 5-3 How Globalization Settings Affect VARCHAR2 Keys for Associative Arrays ........................ 5-4

Choosing Which PL/SQL Collection Types to Use ............................................................................ 5-4 Choosing Between Nested Tables and Associative Arrays ......................................................... 5-5 Choosing Between Nested Tables and Varrays............................................................................. 5-5

Defining Collection Types ...................................................................................................................... 5-6 Defining SQL Types Equivalent to PL/SQL Collection Types ................................................... 5-7

Declaring PL/SQL Collection Variables............................................................................................... 5-8 Initializing and Referencing Collections ......................................................................................... 5-10

Referencing Collection Elements .................................................................................................. 5-12 Assigning Collections........................................................................................................................... 5-13 Comparing Collections......................................................................................................................... 5-16 Using PL/SQL Collections with SQL Statements ........................................................................... 5-17

Using PL/SQL Varrays with INSERT, UPDATE, and SELECT Statements .......................... 5-20 Manipulating Individual Collection Elements with SQL.......................................................... 5-21 Using Multilevel Collections .............................................................................................................. 5-21 Using Collection Methods ................................................................................................................... 5-23 Checking If a Collection Element Exists (EXISTS Method) ...................................................... 5-24 Counting the Elements in a Collection (COUNT Method) ....................................................... 5-24 Checking the Maximum Size of a Collection (LIMIT Method) ................................................ 5-24 Finding the First or Last Collection Element (FIRST and LAST Methods) ............................ 5-25 Looping Through Collection Elements (PRIOR and NEXT Methods).................................... 5-26 Increasing the Size of a Collection (EXTEND Method) ............................................................. 5-27

v

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download