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

Oracle PL/SQL Language Pocket Reference- P11

50 321 0
Tài liệu đã được kiểm tra trùng lặp

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Oracle PL/SQL Programming
Trường học O'Reilly & Associates
Chuyên ngành Computer Science
Thể loại Book index
Năm xuất bản 2000
Thành phố Sebastopol
Định dạng
Số trang 50
Dung lượng 251,64 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Previous: About the Disk Oracle PL/SQL Programming, 2nd Edition Next: Acknowledgments The Oracle Library Navigation Copyright c 2000 O'Reilly & Associates.. Oracle PL/SQL Programming, 2n

Trang 1

Previous: About the Disk Preface Next: Acknowledgments

Comments and Questions

Please address comments and questions concerning this book to the publisher:

O'Reilly & Associates

You can also send us messages electronically See the insert in the book for information about

O'Reilly & Associates' online services

For corrections and amplifications for the book, check out http://www.oreilly.com/catalog/oraclep2/

If you have any questions about the disk supplied with this book, contact RevealNet Inc at http://www.revealnet.com

Previous: About the Disk Oracle PL/SQL

Programming, 2nd Edition

Next: Acknowledgments

The Oracle Library

Navigation

Copyright (c) 2000 O'Reilly & Associates All rights reserved

Trang 2

Previous: Which Platform

or Version?

Preface Next: Comments and

Questions

About the Disk

The content of the companion Windows disk for this book has been included on this CD, in the /

prog2/disk/ directory This disk contains the Companion Utilities Guide for Oracle PL/SQL

Programming, an online tool developed by RevealNet, Inc that gives you point-and-click access to

more than 100 files of source code and documentation that we developed Many of the code examples are also printed in the book We've included these to give you a jump start on writing your own PL/SQL code and to keep you from having to type many pages of PL/SQL statements from printed text

Appendix A, describes how to install the Windows-based interface You can run the software in any Microsoft Windows environment If you are working in a non-Windows environment, you can obtain

a compressed file containing the utilities on the desk from the RevealNet PL/SQL Pipeline Archives

at http://www.revealnet.com/plsql-pipeline

Previous: Which Platform

or Version?

Oracle PL/SQL Programming, 2nd Edition

Next: Comments and Questions

Which Platform or Version? Book Index Comments and Questions

The Oracle Library

Navigation

Copyright (c) 2000 O'Reilly & Associates All rights reserved

Trang 3

Previous: Conventions

Used in This Book

Preface Next: About the Disk

Which Platform or Version?

In general, all of the discussions and examples in this book apply regardless of the machine and/or operating system you are using In those few cases where a feature is in any way system-dependent, I note that in the text

There are many versions of PL/SQL, and you may even find that you need to use multiple versions in your development work Chapter 1, Introduction to PL/SQL describes in detail the many versions of PL/SQL and what you need to know about them; see Section 1.4, "PL/SQL Versions" in Chapter 1 in

Chapter 1

Previous: Conventions

Used in This Book

Oracle PL/SQL Programming, 2nd Edition

Next: About the Disk

Conventions Used in This

Book

The Oracle Library

Navigation

Copyright (c) 2000 O'Reilly & Associates All rights reserved

Trang 4

Previous: Audience Preface Next: Which Platform or

Version?

Conventions Used in This Book

The following conventions are used in this book:

Italic

is used for file and directory names

Constant width

is used for code examples

Constant width bold

In some code examples, highlights the statements being discussed

Constant width italic

In some code examples, indicates an element (e.g., a filename) that you supply

Trang 5

In code examples, these characters delimit a multiline comment, which can extend from one line to another

In code examples and related discussions, a dot qualifies a reference by separating an object name from a component name For example, dot notation is used to select fields in a record and to specify declarations within a package

The Oracle Library

Navigation

Copyright (c) 2000 O'Reilly & Associates All rights reserved

Trang 6

Previous: Structure of This

Information systems manager The application development or database

administration manager in an Oracle shop needs a thorough grasp of the technology used in the development groups Familiarity with the technology will help the manager to better understand the challenges faced by the team members and the ability of that team to solve problems These managers will want to pay particular attention to Part 4, for the big picture of structuring PL/SQL-based applications

One-person information systems shop Oracle licenses are frequently sold into small

companies or departments where the supporting information systems organization consists of little more than a single manager and single developer (or perhaps both of those functions rolled into one) These small organizations do not have the time to search through multiple manuals or sets of training notes to find the solution to their problems This book offers one-stop shopping for these people a consolidated reference and solutions source

Trang 7

Database administrator The DBA in the world of Oracle7 needs to build

database triggers and stored procedures in order to manage business rules at the RDBMS levels and implement distributed databases The DBA will use this book to strengthen his or her understanding of how to write efficient RDBMS-level objects This book will also discuss constructing packages of related objects which will reduce the resources required to maintain these objects

New developer in the Oracle

Developer/2000 environment

Many developers arrive fresh on the Oracle scene through the use of the new Oracle Developer/2000 tools in the Windows environment These

developers will be comfortable manipulating the various widgets in the GUI world, but will find PL/SQL to be a strange, new partner for development This book will quickly bring them up to speed and make them more productive users of Oracle

Developer/2000 software

Experienced Oracle developer Many thousands of programmers have spent years

writing, debugging, and maintaining programs written in SQL*Forms, SQL*Reportwriter, SQL*Plus, and SQL*Menu While their PL/SQL skills have progressed to meet the needs of specific applications, most could expand both their PL/SQL knowledge and their awareness of its subtleties In addition, as developers move into the Oracle Developer/2000 generation, PL/SQL plays a significantly more central role; the developer will have to gain new expertise to meet the demands of this change

Consultant Consultants must offer a high level of service and

quality to their customers This added value is measured in productivity and in the application of skills not currently held by the client Consultants should find this book an invaluable aid in deepening their understanding of PL/SQL technology

Trang 8

Previous: Structure of This

Book

Oracle PL/SQL Programming, 2nd Edition

Next: Conventions Used in This Book

Structure of This Book Book Index Conventions Used in This

Trang 9

Previous: Objectives of

This Book

Preface Next: Audience

Structure of This Book

This second edition has changed from the first edition in a number of significant ways Before listing the individual parts of the book, I want to explain the overall restructuring

About the Second Edition

We (the authors and O'Reilly & Associates) are committed to providing comprehensive, useful

coverage of the PL/SQL language over the life of this language The first edition of this book covered most of PL/SQL's features as it existed through PL/SQL Release 2.3 With the release of Oracle8,

however, we faced a challenge: how do we fit all the new technologies of PL/SQL8 into Oracle PL/ SQL Programming and fill out coverage of existing elements of PL/SQL without creating a tome so

unwieldy that reading the book becomes as much a physical as a mental workout?

Furthermore, if we look ahead a few years, we can easily expect that Oracle will continue to roll out expanded functionality in the objects area, as well as providing new and enhanced built-in packages Given this situation, it quickly became clear to us that it was not practical to offer a single text which covered "all things PL/SQL."

Two questions then arose: what do we cut and what do we do with the stuff that we cut? The answers are as follows:

● Move the package examples from the printed text to the disk The second edition's disk offers

a Windows-based interface designed by RevealNet, Inc (http://www.revealnet.com) allowing you rapid access to the many utilities and samples Now, instead of having to look through a directory listing and then using an editor to view those files, you will be able to mouse-click and drill-down your way to the topics in which you are interested We take advantage of this interface to give you an easy way to examine the many examples of packages I built for the book

● Remove the chapter on built-in packages and expand it into a complete book This chapter in the first edition offered lots of good information, but it was not comprehensive and simply couldn't keep growing to absorb all the new technology issuing forth from Oracle

headquarters Moving coverage of built-in packages to its own book will give us room to

Trang 10

Appendix A, What's on the Companion Disk? , of this edition of Oracle PL/SQL Programming

will still provide a quick reference to the more common built-in packages

About the Contents

The second edition of Oracle PL/SQL Programming is divided into seven parts:

Part 1, Programming in PL/SQL

Chapters 1 through 3 explain what it means to program in PL/SQL and in Oracle-based

applications in general This part of the book introduces you to the main features of the PL/SQL language and gives you some general advice on programming habits and effective

coding style

Part 2, PL/SQL Language Elements

Chapters 4 through 10 describe the basic PL/SQL programming components variables, cursors, conditional and sequential control statements, loops, exception handlers, PL/SQL records, and PL/SQL tables These chapters contain numerous examples, along with tips on how, when, and where to apply these programming constructs most effectively When you complete this section of the book you will know how to apply all the constructs of the PL/SQL language You will also be able to examine the complex requirements in your own

applications and use the different parts of PL/SQL to implement those requirements

Part 3, Built-In Functions

Chapters 11 through 14 present the many built-in (predefined) PL/SQL functions and

procedures, which you can put to use immediately in your applications One of the key

barometers of your success with PL/SQL will be the extent to which you know how to

leverage all of the capabilities that Oracle Corporation provides (You don't want to have to reinvent the wheel when so many functions and procedures are provided for you.) Appendix

C, Built-In Packages supplements Part 3 by summarizing the syntax of Oracle's built-in

packages

Part 4, Modular Code

Chapters 15 through 17 take you past the individual components of the PL/SQL language to explore modular construction in PL/SQL You will learn how to build procedures, functions, and packages; more than that, you will learn how to build them correctly More than with any other aspect of code, the way you design your modules has an enormous impact on your

applications' development time, reusability, and maintainability

Part 5, New PL/SQL8 Features

Chapters 18 through 21 contain the main discussion of the new features of Oracle8 in

particular, object types, new types of collections (nested tables and VARRAYs), object views, and external procedures Although we describe Oracle8 enhancements as appropriate in other sections of the book, we localize the major discussion here Doing so allows developers new

Trang 11

to Oracle8 to learn about the features in a coordinated way It also lets developers still

working on Oracle7 systems to skip over much of the discussion until it is more relevant to them

Part 6, Making PL/SQL Programs Work

Chapters 22 through 26 tell you how to manage your PL/SQL code and debug, tune, and trace the execution of your programs This part of the book also contains a summary of particularly helpful tips for effective PL/SQL programming in the real world

Part 7, Appendixes

Appendixes A through C summarize what's on the companion disk, how to call stored

procedures from PL/SQL Version 1.1, and how to call Oracle's built-in packages

If you are new to PL/SQL, reading this book from beginning to end should improve your skills and help your understanding widen in a gradual, natural process If you're already a proficient PL/SQL programmer, you'll probably want to dip into the appropriate sections in order to extract particular techniques for immediate application Whether you use this book as a teaching guide or as a

reference, I hope that it will have a significant impact on your ability to use PL/SQL most effectively

Long as this book is, it doesn't do everything The Oracle environment is a huge and complex one, and in this book I've focused my attention on PL/SQL itself Some topics are outside the logical scope of this book Therefore, this book does not cover:

● Administration of Oracle databases While the DBA can use this book to learn how to write the PL/SQL needed to build and maintain databases, this book does not explore all the

nuances of the Data Definition Language (DDL) of Oracle's SQL

● Application and database tuning I don't cover detailed tuning issues in this book, though the second edition does add a chapter on PL/SQL tuning Oracle Performance Tuning by Peter Corrigan and Mark Gurry (O'Reilly & Associates, Second Edition, 1997) gives you all the information you need about tuning your Oracle applications

Oracle tool-specific technologies independent of PL/SQL This book won't teach you

everything you need to know about such technologies as SQL*Forms and Oracle Forms

triggers Similarly, you won't find detailed discussions of repeating frames of Oracle Reports

in here Nevertheless, many of the techniques offered in this book certainly do apply to the Oracle Developer/2000 environment

Third-party application development software There are many alternatives to using PL/SQL

and the tools supplied by Oracle to build your applications This book does not address these options, nor does it attempt to compare PL/SQL to these third-party products

National Language Support in Oracle This book does not offer comprehensive coverage of

Oracle's National Language Support (NLS) capabilities for developing applications for

multiple languages

● Trusted Oracle Oracle Corporation has developed a special version of its Oracle7 Server for high-security environments This book does not detail the additional datatypes and features available only for Trusted Oracle

Trang 12

Previous: Objectives of

This Book

Oracle PL/SQL Programming, 2nd Edition

Next: Audience

The Oracle Library

Navigation

Copyright (c) 2000 O'Reilly & Associates All rights reserved

Trang 13

Previous: Foreword Preface Next: Structure of This

Book

Preface

Contents:

Objectives of This Book

Structure of This Book

Audience

Conventions Used in This Book

Which Platform or Version?

About the Disk

Comments and Questions

Acknowledgments

Thousands of application developers and database administrators around the world use software provided by Oracle Corporation to build complex systems that manage vast quantities of data At the heart of much of Oracle's software is PL/SQL a programming language that provides procedural extensions to the SQL relational database language and an ever-growing range of Oracle

development tools

PL/SQL figures prominently as an enabling technology in almost every new product released by Oracle Corporation Developers can use PL/SQL to perform many kinds of programming functions, including:

● Implementing crucial business rules in the Oracle Server with PL/SQL-based stored

procedures and database triggers

● Enhancing powerful and easy-to-use GUI interfaces of products like Oracle Developer/2000 with detailed, programmatic control

● Employing object-oriented design principles in Oracle-based applications

● Linking a World Wide Web page to an Oracle database

Perhaps most importantly, PL/SQL plays a crucial role in the design of successful client-server applications because it provides the foundation for the code used to distribute processing and

transactions across the network

Trang 14

PL/SQL was modeled after Ada,[1] a programming language designed for the United States

Department of Defense Ada is a high-level programming language which emphasizes data

abstraction, information hiding, and other key elements of modern design strategies

[1] The language was named "Ada" in honor of Ada Lovelace, a mathematician who is

regarded by many to have been the world's first computer programmer

PL/SQL is a powerful language which incorporates many of the most advanced elements of

procedural languages, including:

● A full range of datatypes

● Explicit block structures

● Conditional and sequential control statements

● Loops of various kinds

● Exception handlers for use in event-based error handling

● Constructs that help in the development of modular code functions, procedures, and

packages (collections of related programs and variables); packages are often referred to as

"poor man's objects"

● User-defined datatypes such as objects and nested tables (with the Oracle objects option)

PL/SQL is integrated tightly into Oracle's SQL language: you can execute SQL statements directly from your procedural program Conversely, you can also call PL/SQL functions from within a SQL statement

Oracle developers who want to be successful in the 1990s and beyond must learn to use PL/SQL to full advantage This is a two-step process: first, learn how to use the language's full set of features; and second, after mastering those individual features, learn how to put these constructs together to build complex applications

How can you best learn PL/SQL? The usual way is to struggle with the software tool and, through trial and error, discover the best implementation techniques With PL/SQL, a slow and uncertain process is made worse by several factors:

● PL/SQL is still a relatively new language; there are few resources outside of Oracle manuals that will help you learn more Classes tend to focus on the flashy side of the new GUI tools, and they ignore the more complicated programming that is so necessary in production

applications Books on Oracle technology try to cover too much territory and as a result

cannot support developers as they move past the most basic requirements

● PL/SQL is just now maturing to the point where it offers and is supported by a comprehensive set of features and programmer utilities For years, developers have complained about a lack

of a debugger, the inability to read from and write to operating system files, no support for arrays, and other issues Oracle Corporation has finally released versions of PL/SQL which address these complaints and are robust enough to support large-scale application

development To complement this robustness, third-party vendors are also beginning to offer useful programming environments

Trang 15

● PL/SQL is an island of procedurality in a sea of declarative or "fill-in-the-form" products; you have to figure out how to blend these two different approaches when you develop your code It

is one thing to click on a button in a designer interface and create a widget It is quite another thing to learn how to manipulate that widget and handle the event-based interactions between widgets with procedural code

For all of these reasons and more, Oracle developers need a solid, comprehensive resource for all things PL/SQL You need to know about the basic building blocks of PL/SQL, but you also need to learn by example, so you can skip as much of the trial-and-error as possible As with any

programming language, there is a right way and many wrong ways (or at least "not as right" ways) in PL/SQL to handle just about every requirement you will meet It's my hope that this book will help you learn how to use the PL/SQL language in the most effective and efficient way possible

Objectives of This Book

What, specifically, will this book help you do?

Take full advantage of PL/SQL The reference manuals may describe all the features of the PL/SQL

language, but they don't tell you how to apply the technology In fact, in some cases, you'll be lucky

to even understand how to use a given feature after you've made your way through the railroad

diagrams Books and training courses tend to cover the same standard topics in the same limited way

In this book, we'll venture beyond to the edges of the language, to the nonstandard ways in which a particular feature can be tweaked to achieve a desired result

Use PL/SQL to solve your problems You don't spend your days and nights writing PL/SQL modules

so that you can rise to a higher plane of existence You use PL/SQL to solve problems for your

company or your customers In this book, I try hard to help you tackle real-world problems, the kinds

of issues developers face on a daily basis (at least those problems that can be solved with mere

software) To do this, I've packed the book with examples not just small code fragments, but

complete application components you can apply immediately to your own situations There is a good deal of code in the book itself, and much more on the disk that accompanies the book In this book I guide you through the analytical process used to come up with a solution In this way I hope you'll see, in the most concrete terms, how to apply PL/SQL features and undocumented applications of those features to a particular situation

Write efficient, maintainable code PL/SQL and the rest of the Oracle products offer the potential for

incredible development productivity If you aren't careful, however, this rapid development capability will simply let you dig yourself into a deeper, darker hole than you've ever found yourself in before I would consider this book a failure if it only ended up helping programmers write more code in less time than ever before I want to help you develop the skills and techniques that give you the time to build modules which readily adapt to change and are easily understood and maintained I want to teach you to use comprehensive strategies and code architectures which allow you to apply PL/SQL

in powerful, general ways to many of the problems you will face

Trang 16

Previous: Foreword Oracle PL/SQL

Programming, 2nd Edition

Next: Structure of This Book

The Oracle Library

Navigation

Copyright (c) 2000 O'Reilly & Associates All rights reserved

Trang 17

Previous: Dedication Foreword Next: Preface

undocumented, nonoptimized, spaghetti code that you have ever seen We could quickly build

unreadable, unmaintainable code with all the application logic wrapped up in the interface elements

Now it's Java, Java, Java, and more Java Our clients want elegant multi-tier applications that run on any client from thin-client network computers to overstuffed NT workstations with hundreds of

megabytes of memory, with that same instantaneous response they expected from client/server

systems Now our applications must support not hundreds of users on a local area network but

thousands of users on the Internet Oracle is even promising that you will be able to execute Java in the database server itself and write stored procedures in Java rather than PL/SQL So why aren't we rushing off to learn Java? Because even with all of the hoopla over Java, PL/SQL is still the best way

to build programs to access data in Oracle7 and Oracle8 databases After Oracle began using PL/SQL

to build its own tools for replication, the open APIs for Designer/2000 and Developer/2000, and the HTML generators, it began to pay a lot more attention to performance characteristics, and PL/SQL8 includes a number of significant enhancements to speed execution of complex PL/SQL logic With these new improvements, PL/SQL8 assumes even more importance as the principal data access

language for Oracle8

Oracle has been methodically enhancing the capabilities of PL/SQL to make it the language of choice for developing distributed computing environments When Oracle finally delivers client-side PL/SQL with the full characteristics of server-side code (effectively turning all clients into mini-servers for certain purposes), we will be able to build truly distributed environments for maximizing the

effectiveness of our limited computing resources We already can use PL/SQL packages and

procedures from most good front-end tools, and that integration should get better over time Almost every Java-based middleware connectivity tool is being released with support for PL/SQL That should help make it easier to build the complex rules in a portable language that we can run in most popular hardware or software environments

Trang 18

PL/SQL is a complex and powerful programming language that you can use to build a new

generation of multi-tier systems systems that can scale up past the department LAN to enable

world-wide enterprise systems on the Internet with the same reliability that we expect from old

mainframe applications The ability to build those systems does not mean that we will be able to manage our development environments effectively You can be sure that the tools to help us cope with the complexity of truly distributed business objects will be slower to emerge, and we will all be

in pretty deep before they are really ready

We can start to get ready now as we begin to take advantage of the new power and sophistication of Oracle8's PL/SQL We can start writing modular code that can be developed in a top-down manner with program names that truly describe their function We can start to write PL/SQL code that we can reuse within and among our applications We can apply the principles of good structured design to our development of PL/SQL packages, procedures, functions, etc And we can use Oracle PL/SQL Programming as our source of the right way to write efficient and effective PL/SQL code

Many of us have already adopted Oracle PL/SQL Programming as our programming "bible."

Steven's no-nonsense approach to minimizing the amount of code he needs to write and maintain; his naming conventions that maximize the documentation content of his code; his innate ability to sniff out ways to make coding PL/SQL less drudgery and more creative all of these characteristics

should be extremely useful and inspiring to good developers The first edition of this book is one of the very few titles in my Oracle collection of more than 70 books that is actually dog-eared from use All of my colleagues already know the book intimately

I have known Steven Feuerstein and his work for a long time through my publishing of Oracle

technical publications: the NY Oracle Users newsletter in 1989; the Oracle User Resource newsletter from 1990 to 1994; the International Oracle Users Journal from 1990 to 1992; the Proceedings of the East Coast Oracle Developers Conference from 1991 to 1996; and since 1994, Pinnacle Publishing's Oracle Developer Steven has been my most prolific and admired writer for more than seven years

Now that Steven (with the able help of Bill Pribyl) has updated the original version of Oracle PL/SQL Programming, I advise every Oracle developer to replace his or her own well-utilized copy with this new edition They'll start out checking the new Oracle8 additions and the additional chapters on performance tuning, debugging, and tracing But I think every one of them should have a copy of the new edition handy so they'll be prepared when someone comes to them with the inevitable questions about PL/SQL Oracle8 is going to be the database behind many of the biggest systems on the

Internet, and PL/SQL is going to be the language that makes it happen The most successful Oracle developers and DBAs will be up to their ears in PL/SQL code and they'll be eternally grateful if that code is written in accordance with Steven and Bill's teachings

As an extra treat, you should also find the book to be readable and entertaining, not easy adjectives to apply to most technical tomes You will enjoy this book! I did

Tony Ziemba

Editor, Oracle Developer

Trang 19

The Oracle Library

Navigation

Copyright (c) 2000 O'Reilly & Associates All rights reserved

Trang 20

Dedication Next: Foreword

Dedication

I dedicate this book to my parents, Sheldon and Joan Feuerstein, and to the children in my life: the babies at the Columbus Maryville Reception Center, Nikolas Silva, Ian and Michaela McCauseland, Danielle and Benjamin DeUrso, Adam and Jay Bartell, Ciera, Brian, and Michael Daniels, Timnah and Masada Sela, and of course my own boys, Eli and Chris

Steven Feuerstein

To my father, who told me "that's no hill for a stepper" enough times that it finally sank in

Bill Pribyl

Oracle PL/SQL Programming, 2nd Edition

Next: Foreword

The Oracle Library

Navigation

Copyright (c) 2000 O'Reilly & Associates All rights reserved

Trang 21

Previous: 8.3 Types of

Exceptions

Chapter 8Exception Handlers

Next: 8.5 Raising an Exception

8.4 Determining Exception-Handling Behavior

A PL/SQL program is an anonymous block, a procedure, or a function This program, or level" block, can call other procedures or functions, or nest an anonymous block within that block So

"highest-at any given point in execution, there might be several layers of PL/SQL blocks nested within other blocks Each PL/SQL block can have its own exception section, or it can be totally void of exception handlers

To determine the appropriate exception-handling behavior, PL/SQL follows rules regarding:

Scope

The PL/SQL block or blocks in which an exception can be raised and handled

Propagation

The way in which an exception is passed back through enclosing blocks until it is handled or

is resolved to be an unhandled exception

Let's look at these two properties of exceptions

8.4.1 Scope of an Exception

The scope of an exception is that portion of the code which is "covered" by that exception An

exception covers a block of code if it can be raised in that block The following table shows the scope for each of the different kinds of exceptions:

Named system exceptions These exceptions are globally available because they are not declared

in or confined to any particular block of code You can raise and handle a named system exception in any block

Trang 22

Named

programmer-defined exceptions

These exceptions can only be raised and handled in the execution and exception sections of the block in which they are declared (and all nested blocks)

Unnamed system

exceptions

These exceptions can be handled in any PL/SQL exception section via the WHEN OTHERS section If they are assigned a name, then the scope of that name is the same as that of the named programmer-defined exception

Unnamed

programmer-defined exceptions

This exception is only defined in the call to RAISE_APPLICATION_ERROR and then is passed back to the calling program

8.4.1.1 Scope of programmer-defined exception

Consider the following example of the exception overdue_balance declared in procedure

check_account The scope of that exception is the check_account procedure, and nothing else:

PROCEDURE check_account (company_id_in IN NUMBER)

I can RAISE the overdue_balance inside the check_account procedure, but I cannot raise that

exception from a program that calls check_account The following anonymous block will generate the associated compile error:

Trang 23

PLS-00201: identifier "OVERDUE_BALANCE" must be declared

The check_account procedure is a "black box" as far as the anonymous block is concerned Any identifiers including exceptions which are declared inside check_account are invisible outside of that program

8.4.1.2 Raising exceptions in nested blocks

When you declare an exception in a block, it is local to that block, but global to all the blocks which are enclosed by that block (nested blocks) In the version of check_account shown in the following example, the procedure contains an anonymous subblock which also raises the overdue_balance Because the subblock is enclosed by the procedure block, PL/SQL can resolve the reference to that exception:

PROCEDURE check_account (company_id_in IN NUMBER)

statements within sub-block

RAISE overdue_balance; Exception raised in

Trang 24

Because overdue_balance was declared for the whole procedure, the name is known throughout all subblocks

8.4.1.3 Overlapping exception names

You never have to declare a named system exception because they have all been declared in the STANDARD package, which is instantiated as soon as you run any PL/SQL code You can, on the other hand, declare your own exceptions with the same name as a previously defined system

exception, as shown below:

8.4.1.4 Qualifying exception names

If I really want to name a local exception the same as a predefined exception and handle the latter in that same PL/SQL block, I need to use the dot notation on the predefined exception This notation distinguishes between the two and allows PL/SQL to find the handler for the predefined exception I can then handle those two different exceptions separately, or I can combine them in a single

Trang 25

WHEN no_data_found OR STANDARD.NO_DATA_FOUND

8.4.1.5 Referencing duplicate exceptions in nested blocks

You cannot declare the same exception more than once in a single block, but you can declare an exception in a nested block with the same name as that of an enclosing block When you do this, the declaration local to that nested block takes precedence over the global exception When you raise that exception, you are raising the local exception, which is a completely different exception from the outer exception, even though they share the same name

In the following version of check_account, my nested block contains its own declaration of the

Ngày đăng: 28/10/2013, 22:15

TỪ KHÓA LIÊN QUAN