1. Trang chủ
  2. » Công Nghệ Thông Tin

Oracle Built−in Packages- P6 docx

5 406 1

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Table of Contents

  • A. What's on the Companion Disk?

    • A.1 Installing the Guide

    • A.2 Using the Guide

  • 1. Introduction

    • 1.1 The Power of Built-in Packages

      • 1.1.1 A Kinder , More Sharing Oracle

    • 1.2 Built-in Packages Covered in This Book

      • 1.2.1 Application Development Packages

      • 1.2.2 Server Management Packages

      • 1.2.3 Distributed Database Packages

    • 1.3 Using Built-in Packages

      • 1.3.1 What Is a Package?

      • 1.3.2 Controlling Access with Packages

      • 1.3.3 Referencing Built-in Package Elements

      • 1.3.4 Exception Handling and Built-in Packages

      • 1.3.5 Encapsulating Access to the Built-in Packages

      • 1.3.6 Calling Built-in Packaged Code from Oracle Developer/2000 Release 1

      • 1.3.7 Accessing Built-in Packaged Technology from Within SQL

    • 1.4 Examining Built-in Package Source Code

      • 1.4.1 The STANDARD Package

      • 1.4.2 The DBMS_STANDARD Package

  • 2. Executing Dynamic SQL and PL/SQL

    • 2.1 Examples of Dynamic SQL

    • 2.2 Getting Started with DBMS_SQL

      • 2.2.1 Creating the DBMS_SQL Package

      • 2.2.2 Security and Privilege Issues

      • 2.2.3 DBMS_SQL Programs

      • 2.2.4 Types of Dynamic SQL

      • 2.2.5 DBMS_SQL Exceptions

      • 2.2.6 DBMS_SQL Nonprogram Elements

    • 2.3 The DBMS_SQL Interface

      • 2.3.1 Processing Flow of Dynamic SQL

      • 2.3.2 Opening the Cursor

      • 2.3.3 Parsing the SQL Statement

      • 2.3.4 Binding Values into Dynamic SQL

      • 2.3.5 Defining Cursor Columns

      • 2.3.6 Executing the Cursor

      • 2.3.7 Fetching Rows

      • 2.3.8 Retrieving Values

      • 2.3.9 Closing the Cursor

      • 2.3.10 Checking Cursor Status

      • 2.3.11 Describing Cursor Columns

    • 2.4 Tips on Using Dynamic SQL

      • 2.4.1 Some Restrictions

      • 2.4.2 Privileges and Execution Authority with DBMS_SQL

      • 2.4.3 Combining Operations

      • 2.4.4 Minimizing Memory for Cursors

      • 2.4.5 Improving the Performance of Dynamic SQL

      • 2.4.6 Problem-Solving Dynamic SQL Errors

      • 2.4.7 Executing DDL in PL/SQL

      • 2.4.8 Executing Dynamic PL/SQL

    • 2.5 DBMS_SQL Examples

      • 2.5.1 A Generic Drop_Object Procedure

      • 2.5.2 A Generic Foreign Key Lookup Function

      • 2.5.3 A Wrapper for DBMS_SQL .DESCRIBE_COLUMNS

      • 2.5.4 Displaying Table Contents with Method 4 Dynamic SQL

      • 2.5.5 Indirect Referencing in PL/SQL

      • 2.5.6 Array Processing with DBMS_SQL

      • 2.5.7 Using the RETURNING Clause in Dynamic SQL

  • 3. Intersession Communication

    • 3.1 DBMS_PIPE: Communicating Between Sessions

      • 3.1.1 Getting Started with DBMS_PIPE

      • 3.1.2 How Database Pipes Work

      • 3.1.3 Managing Pipes and the Message Buffer

      • 3.1.4 Packing and Unpacking Messages

      • 3.1.5 Sending and Receiving Messages

      • 3.1.6 Tips on Using DBMS_PIPE

      • 3.1.7 DBMS_PIPE Examples

    • 3.2 DBMS_ALERT: Broadcasting Alerts to Users

      • 3.2.1 Getting Started with DBMS_ALERT

      • 3.2.2 The DBMS_ALERT Interface

      • 3.2.3 DBMS_ALERT Examples

  • 4. User Lock and Transaction Management

    • 4.1 DBMS_LOCK: Creating and Managing Resource Locks

      • 4.1.1 Getting Started with DBMS_LOCK

      • 4.1.2 The DBMS_LOCK Interface

      • 4.1.3 Tips on Using DBMS_LOCK

      • 4.1.4 DBMS_LOCK Examples

    • 4.2 DBMS_TRANSACTION: Interfacing to SQL Transaction Statements

      • 4.2.1 Getting Started with DBMS_TRANSACTION

      • 4.2.2 Advising Oracle About In-Doubt Transactions

      • 4.2.3 Committing Data

      • 4.2.4 Rolling Back Changes

      • 4.2.5 Setting Transaction Characteristics

      • 4.2.6 Cleaning Up Transaction Details

      • 4.2.7 Returning Transaction Identifiers

  • 5. Oracle Advanced Queuing

    • 5.1 Oracle AQ Concepts

      • 5.1.1 General Features

      • 5.1.2 Enqueue Features

      • 5.1.3 Dequeue Features

      • 5.1.4 Propagation Features

      • 5.1.5 A Glossary of Terms

      • 5.1.6 Components of Oracle AQ

      • 5.1.7 Queue Monitor

      • 5.1.8 Data Dictionary Views

    • 5.2 Getting Started with Oracle AQ

      • 5.2.1 Installing the Oracle AQ Facility

      • 5.2.2 Database Initialization

      • 5.2.3 Authorizing Accounts to Use Oracle AQ

    • 5.3 Oracle AQ Nonprogram Elements

      • 5.3.1 Constants

      • 5.3.2 Object Names

      • 5.3.3 Queue Type Names

      • 5.3.4 Agents Object Type

      • 5.3.5 Recipient and Subscriber List Table Types

      • 5.3.6 Message Properties Record Type

      • 5.3.7 Enqueue Options Record Type

      • 5.3.8 Dequeue Options Record Type

      • 5.3.9 Oracle AQ Exceptions

    • 5.4 DBMS_AQ: Interfacing to Oracle AQ (Oracle8 only)

      • 5.4.1 Enqueuing Messages

      • 5.4.2 Dequeuing Messages

    • 5.5 DBMS_AQADM: Performing AQ Administrative Tasks (Oracle8 only)

      • 5.5.1 Creating Queue Tables

      • 5.5.2 Creating and Starting Queues

      • 5.5.3 Managing Queue Subscribers

      • 5.5.4 Stopping and Dropping Queues

      • 5.5.5 Managing Propagation of Messages

      • 5.5.6 Verifying Queue Types

      • 5.5.7 Starting and Stopping the Queue Monitor

    • 5.6 Oracle AQ Database Objects

      • 5.6.1 Objects Per Queue Table

      • 5.6.2 Data Dictionary Objects

    • 5.7 Oracle AQ Examples

      • 5.7.1 Improving AQ Ease of Use

      • 5.7.2 Working with Prioritized Queues

      • 5.7.3 Building a Stack with AQ Using Sequence Deviation

      • 5.7.4 Browsing a Queue's Contents

      • 5.7.5 Searching by Correlation Identifier

      • 5.7.6 Using Time Delay and Expiration

      • 5.7.7 Working with Message Groups

      • 5.7.8 Working with Multiple Consumers

  • 6. Generating Output from PL/SQL Programs

    • 6.1 DBMS_OUTPUT: Displaying Output

      • 6.1.1 Getting Started with DBMS_OUTPUT

      • 6.1.2 Enabling and Disabling Output

      • 6.1.3 Writing to the DBMS_OUTPUT Buffer

      • 6.1.4 Retrieving Data from the DBMS_OUTPUT Buffer

      • 6.1.5 Tips on Using DBMS_OUTPUT

      • 6.1.6 DBMS_OUTPUT Examples

    • 6.2 UTL_FILE: Reading and Writing Server-side Files

      • 6.2.1 Getting Started with UTL_FILE

      • 6.2.2 Opening Files

      • 6.2.3 Reading from Files

      • 6.2.4 Writing to Files

      • 6.2.5 Closing Files

      • 6.2.6 Tips on Using UTL_FILE

      • 6.2.7 UTL_FILE Examples

  • 7. Defining an Application Profile

    • 7.1 Getting Started with DBMS_APPLICATION_INFO

      • 7.1.1 DBMS_APPLICATION_INFO Programs

      • 7.1.2 The V$ Virtual Tables

      • 7.1.3 DBMS_APPLICATION_INFO Nonprogram Elements

    • 7.2 DBMS_APPLICATION_INFO Interface

      • 7.2.1 The DBMS_APPLICATION_INFO.READ_CLIENT_INFO procedure

      • 7.2.2 The DBMS_APPLICATION_INFO.READ_MODULE procedure

      • 7.2.3 The DBMS_APPLICATION_INFO.SET_ACTION procedure

      • 7.2.4 The DBMS_APPLICATION_INFO.SET_CLIENT_INFO procedure

      • 7.2.5 The DBMS_APPLICATION_INFO.SET_MODULE procedure

      • 7.2.6 The DBMS_APPLICATION_INFO.SET_SESSION_LONGOPS procedure

    • 7.3 DBMS_APPLICATION_INFO Examples

      • 7.3.1 About the register_app Package

      • 7.3.2 The action Procedure

      • 7.3.3 The set_stats Procedure

      • 7.3.4 The Information Procedures

      • 7.3.5 Using the register_app Package

      • 7.3.6 Covering DBMS_APPLICATION_INFO

      • 7.3.7 Monitoring Application SQL Resource Consumption

      • 7.3.8 Session Monitoring and Three-Tier Architectures

      • 7.3.9 Tracking Long-Running Processes

  • 8. Managing Large Objects

    • 8.1 Getting Started with DBMS_LOB

      • 8.1.1 DBMS_LOB Programs

      • 8.1.2 DBMS_LOB Exceptions

      • 8.1.3 DBMS_LOB Nonprogram Elements

      • 8.1.4 About the Examples

    • 8.2 LOB Concepts

      • 8.2.1 LOB Datatypes

      • 8.2.2 BFILE Considerations

      • 8.2.3 Internal LOB Considerations

    • 8.3 DBMS_LOB Interface

      • 8.3.1 Working with BFILEs

      • 8.3.2 Reading and Examining LOBs

      • 8.3.3 Updating BLOBs, CLOBs, and NCLOBs

  • 9. Datatype Packages

    • 9.1 DBMS_ROWID: Working with the ROWID Pseudo-Column (Oracle8 only)

      • 9.1.1 Getting Started with DBMS_ROWID

      • 9.1.2 ROWID Concepts

      • 9.1.3 The DBMS_ROWID Interface

    • 9.2 UTL_RAW: Manipulating Raw Data

      • 9.2.1 Getting Started with UTL_RAW

      • 9.2.2 Raw Data Manipulation Concepts

      • 9.2.3 The UTL_RAW Interface

      • 9.2.4 UTL_REF: Referencing Objects (Oracle8.0.4)

      • 9.2.5 UTL_REF Interface

      • 9.2.6 UTL_REF Example

  • 10. Miscellaneous Packages

    • 10.1 DBMS_UTILITY: Performing Miscellaneous Operations

      • 10.1.1 Getting Started with DBMS_UTILITY

      • 10.1.2 The DBMS_UTILITY Interface

    • 10.2 DBMS_DESCRIBE: Describing PL/SQL Program Headers

      • 10.2.1 Getting Started with DBMS_DESCRIBE

      • 10.2.2 Explaining DBMS_DESCRIBE Results

      • 10.2.3 DBMS_DESCRIBE Example

    • 10.3 DBMS_DDL: Compiling and Analyzing Objects

      • 10.3.1 Getting Started with DBMS_DDL

      • 10.3.2 Compiling PL/SQL Objects

      • 10.3.3 Computing Statistics for an Object

      • 10.3.4 Setting Referenceability of Tables

    • 10.4 DBMS_RANDOM: Generating Random Numbers (Oracle8 Only)

      • 10.4.1 Getting Started with DBMS_RANDOM

      • 10.4.2 DBMS_RANDOM Interface

  • 11. Managing Session Information

    • 11.1 DBMS_SESSION: Managing Session Information

      • 11.1.1 Getting Started with DBMS_SESSION

      • 11.1.2 Modifying Session Settings

      • 11.1.3 Obtaining Session Information

      • 11.1.4 Managing Session Resources

      • 11.1.5 DBMS_SESSION Examples

    • 11.2 DBMS_System: Setting Events for Debugging

      • 11.2.1 Getting Started with DBMS_SYSTEM

      • 11.2.2 DBMS_SYSTEM Interface

      • 11.2.3 DBMS_SYSTEM Examples

  • 12. Managing Server Resources

    • 12.1 DBMS_SPACE: Obtaining Space Information

      • 12.1.1 Getting Started with DBMS_SPACE

      • 12.1.2 The DBMS_SPACE Interface

      • 12.1.3 DBMS_SPACE Examples

    • 12.2 DBMS_SHARED_POOL: Pinning Objects

      • 12.2.1 Getting Started with DBMS_SHARED_POOL

      • 12.2.2 Pinning and Unpinning Objects

      • 12.2.3 Monitoring and Modifying Shared Pool Behavior

      • 12.2.4 DBMS_SHARED_POOL Examples

  • 13. Job Scheduling in the Database

    • 13.1 Getting Started with DBMS_ JOB

      • 13.1.1 DBMS_JOB Programs

      • 13.1.2 Job Definition Parameters

      • 13.2.1 INIT.ORA Parameters and Background Processes

      • 13.2.2 Job Execution and the Job Execution Environment

      • 13.2.3 Miscellaneous Notes

      • 13.2.4 DBMS_JOB Interface

      • 13.2.5 Submitting Jobs to the Job Queue

      • 13.2.6 Modifying Job Characteristics

      • 13.2.7 Removing Jobs and Changing Job Execution Status

      • 13.2.8 Transferring Jobs

    • 13.2 Job Queue Architecture

    • 13.3 Tips on Using DBMS_JOB

      • 13.3.1 Job Intervals and Date Arithmetic

      • 13.3.2 Viewing Job Information in the Data Dictionary

      • 13.3.3 DBMS_IJOB: Managing Other Users' Jobs

    • 13.4 DBMS_JOB Examples

      • 13.4.1 Tracking Space in Tablespaces

      • 13.4.2 Fixing Broken Jobs Automatically

      • 13.4.3 Self-Modifying and Self-Aware Jobs

  • 14. Snapshots

    • 14.1 DBMS_SNAPSHOT: Managing Snapshots

      • 14.1.1 Getting Started with DBMS_SNAPSHOT

      • 14.1.2 Using the I_AM_A_REFRESH Package State Variable

      • 14.1.3 Refreshing Snapshots

      • 14.1.4 Purging the Snapshot Log

      • 14.1.5 Reorganizing Tables

      • 14.1.6 Registering Snapshots

      • 14.2.1 Getting Started with DBMS_REFRESH

      • 14.2.2 Creating and Destroying Snapshot Groups

      • 14.2.3 Adding and Subtracting Snapshots from Snapshot Groups

      • 14.2.4 Altering Properties of a Snapshot Group

      • 14.2.5 Manually Refreshing Snapshot Groups

    • 14.2 DBMS_REFRESH: Managing Snapshot Groups

    • 14.3 DBMS_OFFLINE_SNAPSHOT: Performing Offline Snapshot Instantiation

      • 14.3.1 Getting Started with DBMS_OFFLINE_SNAPSHOT

      • 14.3.2 DBMS_OFFLINE_SNAPSHOT Interface

    • 14.4 DBMS_REPCAT: Managing Snapshot Replication Groups

      • 14.4.1 Getting Started with DBMS_REPCAT

      • 14.4.2 Creating and Dropping Snapshot Replication Groups

      • 14.4.3 Adding and Removing Snapshot Replication Group Objects

      • 14.4.4 Altering a Snapshot Replication Group's Propagation Mode

      • 14.4.5 Manually Refreshing a Snapshot Replication Group

      • 14.4.6 Switching the Master of a Snapshot Replication Group

  • 15. Advanced Replication

    • 15.1 DBMS_REPCAT_AUTH: Setting Up Administrative Accounts

      • 15.1.1 Getting Started with DBMS_REPCAT_AUTH

      • 15.1.2 Granting and Revoking Surrogate SYS Accounts

      • 15.1.3 Granting and Revoking Propagator Accounts (Oracle8)

    • 15.2 DBMS_REPCAT_ADMIN: Setting Up More Administrator Accounts

      • 15.2.1 Getting Started with DBMS_REPCAT_ADMIN

      • 15.2.2 Creating and Dropping Replication Administrator Accounts

    • 15.3 DBMS_REPCAT: Replication Environment Administration

      • 15.3.1 Getting Started with DBMS_REPCAT

      • 15.3.2 Replication Groups with DBMS_REPCAT

      • 15.3.3 Replicated Objects with DBMS_REPCAT

      • 15.3.4 Replication Support with DBMS_REPCAT

      • 15.3.5 Adding and Removing Master Sites with DBMS_REPCAT

      • 15.3.6 Maintaining the Repcatlog Queue with DBMS_REPCAT

      • 15.3.7 Quiescence with DBMS_REPCAT

      • 15.3.8 Miscellaneous DBMS_REPCAT Procedures

    • 15.4 DBMS_OFFLINE_OG: Performing Site Instantiation

      • 15.4.1 Getting Started with DBMS_OFFLINE_OG

      • 15.4.2 DBMS_OFFLINE_OG Interface

    • 15.5 DBMS_RECTIFIER_DIFF: Comparing Replicated Tables

      • 15.5.1 Getting Started with DBMS_RECTIFIER_DIFF

      • 15.5.2 DBMS_RECTIFIER_DIFF Interface

    • 15.6 DBMS_REPUTIL: Enabling and Disabling Replication

      • 15.6.1 Getting Started with DBMS_REPUTIL

      • 15.6.2 DBMS_REPUTIL Interface

  • 16. Conflict Resolution

    • 16.1 Getting Started with DBMS_REPCAT

      • 16.1.1 DBMS_REPCAT Programs

      • 16.1.2 DBMS-REPCAT Exceptions

      • 16.1.3 DBMS-REPCAT Nonprogram Elements

      • 16.1.4 Data Dictionary Views

    • 16.2 Column Groups with DBMS_REPCAT

      • 16.2.1 About Column Groups

      • 16.2.2 Creating and Dropping Column Groups

      • 16.2.3 Modifying Existing Column Groups

    • 16.3 Priority Groups with DBMS_REPCAT

      • 16.3.1 About Priority Groups

      • 16.3.2 Creating, Maintaining, and Dropping Priority Groups

      • 16.3.3 Creating and Maintaining Priorities Within a Priority Group

      • 16.3.4 Dropping Priorities from a Priority Group

    • 16.4 Site Priority Groups with DBMS_REPCAT

      • 16.4.1 About Site Priority Groups

      • 16.4.2 Creating, Maintaining, and Dropping Site Priorities

      • 16.4.3 Maintaining Site Priorities

    • 16.5 Assigning Resolution Methods with DBMS_REPCAT

      • 16.5.1 About Resolution Methods

    • 16.6 Monitoring Conflict Resolution with DBMS_REPCAT

      • 16.6.1 About Monitoring

  • 17. Deferred Transactions and Remote Procedure Calls

    • 17.1 About Deferred Transactions and RPCs

      • 17.1.1 About Remote Destinations

      • 17.1.2 Data Dictionary Views

    • 17.2 DBMS_DEFER_SYS: Managing Deferred Transactions

      • 17.2.1 Getting Started with DBMS_DEFER_SYS

      • 17.2.2 Adding and Deleting Default Destinations

      • 17.2.3 Copying Deferred Transactions to New Destinations

      • 17.2.4 Maintenance Procedures

      • 17.2.5 Propagating Deferred RPCs

      • 17.2.6 Scheduling Propagation (Oracle8 only)

    • 17.3 DBMS_DEFER: Building Deferred Calls

      • 17.3.1 Getting Started with DBMS_DEFER

      • 17.3.2 Basic RPCs

      • 17.3.3 Parameterized RPCs

    • 17.4 DBMS_DEFER_QUERY: Performing Diagnostics and Maintenance

      • 17.4.1 Getting Started with DBMS_DEFER_QUERY

    • Table of Contents

      • Part I: Overview

      • Part II: Application Development Packages

      • Part III: Server Management Packages

      • Part IV: Distributed Database Packages

  • Structure of This Book

Nội dung

PROCEDURE capture IS BEGIN last_timing := DBMS_UTILITY.GET_TIME; END; PROCEDURE show_elapsed IS BEGIN DBMS_OUTPUT.PUT_LINE (DBMS_UTILITY.GET_TIME − last_timing); END; END tmr; The DBMS_UTILITY.GET_TIME program is a function from the built−in package, DBMS_UTILITY, which returns the number of hundredths of seconds that have elapsed since an arbitrary point in time. DBMS_OUTPUT is another built−in package; its PUT_LINE procedure displays output from a PL /SQL program to your screen. Notice that there is another code element defined inside the package body besides the capture and show_elapsed procedures: the last_timing variable. This variable holds the timing value from the last call to tmr.capture. Since last_timing does not appear in the package specification, an external program (i.e., one that is not defined in this package) cannot directly reference that variable. This restriction is illustrated in the Booch diagram[2] Figure 1.1. [2] This diagram is named after Grady Booch, who pioneered many of the ideas of the package, particularly in the context of object−oriented design. Figure 1.1: Booch diagram of tmr package So if I try to access the last_timing variable from outside the tmr package, I get an error. This is shown as follows: SQL> exec tmr.last_timing := 100; begin tmr.last_timing := 100; end; * ERROR at line 1: ORA−06550: line 1, column 14: PLS−00302: component 'LAST_TIMING' must be declared Why should you or anyone else care about where you define the last_timing variable? Because it illustrates a critical aspect of a package's value: integrity. If I had placed the variable in the specification, then a user of the package could write over the value of last_timing −− and completely invalidate the integrity of the package. Suppose my package specification looked like this: PACKAGE tmr IS last_timing NUMBER; PROCEDURE capture; PROCEDURE show_elapsed; [Appendix A] What's on the Companion Disk? 1.3.2 Controlling Access with Packages 16 END tmr; The package compiles and seems to work as before. But consider the following rewrite of my script to time the calc_totals procedure: BEGIN tmr.capture; calc_totals; tmr.last_timing := DBMS_UTILITY.GET_TIME; tmr.show_elapsed; END; / Since tmr.last_timing is now in the package specification, this code will compile, and completely subvert the usefulness of the tmr package. For no matter how much time calc_totals actually takes to execute, the tmr.show_elapsed procedure will always display 0 −− or very close to 0 −− hundredths of seconds for elapsed time. If, on the other hand, I keep last_timing inside the body of the package, only the tmr.capture procedure can modify its value. A user of tmr is, therefore, guaranteed to get dependable results. This absolute control is the reason that the package structure has been so useful to Oracle Corporation −− and one of the reasons the company has constructed dozens of built−in packages. Since you can perform only the operations and access the data structures listed in the package specification, Oracle can make technology available in a highly controlled fashion. As long as its developers write their code properly, there will never be any danger that we can disrupt Oracle Server internals by calling built−in packaged functionality. 1.3.3 Referencing Built−in Package Elements As noted earlier, a package can have up to two parts: the specification and the body. When it comes to built−in packages, you really don't need to concern yourself with the package body. That is the implementation of the package, and something that is the responsibility of Oracle Corporation. With very few exceptions, those package bodies are "wrapped," which means that they are distributed in an encrypted format that you cannot read. This is just as well, because what you really need to do is study the specification to learn about the capabilities offered in that package. There are two ways to use a built−in package in your own code: 1. Run a function or procedure defined in the package specification. 2. Reference a nonprogram element defined in the package specification. Notice that you never actually execute a package itself. The package is simply a "container" for the various code elements defined in the package. Let's take a look at an example to make all this very clear. The DBMS_SQL package (examined at great length in Chapter 2) allows you to execute dynamic SQL (SQL statements constructed at runtime), a feature previously unavailable in the PL /SQL language. Here is a portion of the specification of that package: CREATE OR REPLACE PACKAGE DBMS_SQL IS −− CONSTANTS −− v6 constant integer := 0; [Appendix A] What's on the Companion Disk? 1.3.3 Referencing Built−in Package Elements 17 native constant integer := 1; v7 constant integer := 2; −− −− PROCEDURES AND FUNCTIONS −− FUNCTION open_cursor RETURN INTEGER; PROCEDURE parse (c IN INTEGER, statement IN VARCHAR2, language_flag IN INTEGER); What this tells you is that there are three different constants, one procedure, and one function defined in the package. (There is actually much, much more, of course, but this is all we need to get the point across.) To reference any of the elements, you will use the same "dot notation" used to specify columns in tables. So if I want to open a dynamic cursor, I use this: DECLARE dyncur PLS_INTEGER; BEGIN dyncur := DBMS_SQL.OPEN_CURSOR; And if I want to parse a string using the "native" database method, I would write the following code: PROCEDURE showemps (where_in IN VARCHAR2) IS dyncur PLS_INTEGER := DBMS_SQL.OPEN_CURSOR; BEGIN DBMS_SQL.PARSE (dyncur, 'SELECT ename FROM emp WHERE ' || NVL (where_in, '1=1'), DBMS_SQL.NATIVE); END; In this case, I have qualified my references to the OPEN_CURSOR, PARSE, and NATIVE elements of the DBMS_SQL package. The first two instances are programs (a function and a procedure). The third instance is a constant, passed as the third argument in my call to DBMS_SQL.PARSE. 1.3.4 Exception Handling and Built−in Packages Programs in built−in packages can raise exceptions. You will often want to write code to check for and handle these exceptions. You should know about the different ways that exceptions can be defined and raised by programs in the built−in packages. This will affect the way you write your exception handlers. At the beginning of each package's coverage, you will find a description of the exceptions defined within that package. Within the documentation of many of the programs within a package, you will also find an explanation of the specific exceptions that may be raised by those individual programs. When references are made to named exceptions in these explanations, they will appear in one of two forms: PACKAGE.exception_name or: exception_name If the exception name is unqualified (i.e., no package name appears before the exception name), then this exception is defined either: • [Appendix A] What's on the Companion Disk? 1.3.4 Exception Handling and Built−in Packages 18 In the package currently under discussion, or • In the STANDARD package; examples are VALUE_ERROR and NO_DATA_FOUND. In this section, I will review the four types of exceptions you may encounter when working with built−in packages. I will then show you the kind of code you will need to write to handle exceptions properly when they propagate out from built−in packages. The following sections demonstrate how to write code to work with these different types of exceptions. Table 1.2 summarizes these types. Table 1.2: Types of Exceptions Type How Exception Is Defined How Exception Is Raised SQLCODE Behavior SQLERRM Behavior How to Handle Exception Package−named system exception The package gives a name to a specific Oracle error number using the PRAGMA EXCEPTION_INIT statement. The packaged program issues a RAISE statement. Returns the Oracle error number. Returns the standard Oracle error message text. You can handle it by number within a WHEN OTHERS clause, or by name with its own exception handler; the latter improves the readability of your code. Package−defined exception The package declares one or more exceptions; these exceptions have names, but no message text and no unique number. The packaged program RAISEs that exception by name. Returns 1. Returns "Unhandled user−defined exception" message. You can only handle it by name or with a WHEN OTHERS clause, in which case it is impossible to tell which exception was raised. Standard system exception It is previously given a name in the The packaged program issues a RAISE statement. Returns the Oracle error number. Returns the standard Oracle error message text. You can handle it [Appendix A] What's on the Companion Disk? 1.3.4 Exception Handling and Built−in Packages 19 STANDARD package or it is simply an error number. by name in its own exception handler, if a name has been associated with that error number. Otherwise, you handle the exception by number in a WHEN OTHERS clause. Package−specific exception In this case, Oracle has rudely appropriated for itself one or more of the application−specific error numbers between −20,999 and −20,000 set aside for customers. The packaged program calls RAISE_APPLICATION_ERROR. Returns the number in the −20NNN range. Returns the message text provided in the call to RAISE_APPLICATION_ERROR. You can handle these exceptions by number within a WHEN OTHERS clause. 1.3.4.1 Package−named system exception In this scenario, the package gives a name to a specific Oracle error number using the PRAGMA EXCEPTION_INIT statement. You can then handle the exception by name with its own exception handler or by number within a WHEN OTHERS clause. Let's look at an example. The DBMS_DEFER package associates names with a number of Oracle errors. Here is an example of one such association: updateconflict EXCEPTION; PRAGMA EXCEPTION_INIT (updateconflict, −23303); If a program in DBMS_DEFER raises this exception, you can handle it in either of the following ways: EXCEPTION WHEN DBMS_DEFER.UPDATECONFLICT THEN /* SQLCODE returns −23303 and SQLERRM returns the standard Oracle error message */ or: EXCEPTION WHEN OTHERS THEN IF SQLCODE = −23303 THEN [Appendix A] What's on the Companion Disk? 1.3.4 Exception Handling and Built−in Packages 20 . calling built−in packaged functionality. 1.3.3 Referencing Built−in Package Elements As noted earlier, a package can have up to two parts: the specification and the body. When it comes to built−in. to a specific Oracle error number using the PRAGMA EXCEPTION_INIT statement. The packaged program issues a RAISE statement. Returns the Oracle error number. Returns the standard Oracle error message. statement. Returns the Oracle error number. Returns the standard Oracle error message text. You can handle it [Appendix A] What's on the Companion Disk? 1.3.4 Exception Handling and Built−in Packages

Ngày đăng: 07/07/2014, 00:20

TỪ KHÓA LIÊN QUAN