Oracle Database 10g PL/SQL Programming

Transcription

Oracle Database 10gPL/SQL Programming

This page intentionally left blank

Oracle Database 10gPL/SQL ProgrammingScott UrmanRon HardmanMichael McLaughlinMcGraw-Hill/OsborneNew York Chicago San FranciscoLisbon London Madrid Mexico City MilanNew Delhi San Juan Seoul Singapore SydneyToronto

Copyright 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Manufactured in the United Statesof America. Except as permitted under the United States Copyright Act of 1976, no part of this publication maybe reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without theprior written permission of the publisher.0-07-150228-9The material in this eBook also appears in the print version of this title: 0-07-223066-5.All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner,with no intention of infringement of the trademark. Where such designations appear in this book, they have beenprinted with initial caps.McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or foruse incorporate training programs. For more information, please contact George Hoare, Special Sales, atgeorge hoare@mcgraw-hill.com or (212) 904-4069.TERMS OF USEThis is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve allrights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Actof 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverseengineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish orsublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your ownnoncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work maybe terminated if you fail to comply with these terms.THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES ORWARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BEOBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSEDTHROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will beuninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hillhas no responsibility for the content of any information accessed through the work. Under no circumstances shallMcGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similardamages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether suchclaim or cause arises in contract, tort or otherwise.DOI: 10.1036/0072230665

About the AuthorsScott Urman is a Principal Member of Technical Staff in the Diagnostics and DefectResolution (DDR) team in Oracle’s Server Technology division. He currently focuseson the internals of Oracle Text and Oracle Ultrasearch, and has worked with JSP,JDBC, PL/SQL, and OCI. Prior to joining DDR, he was a Senior Analyst in theLanguages division of Oracle Worldwide Technical Support, focusing on all ofOracle’s language tools. He has been with Oracle since 1989. He is also the bestselling author of Oracle8i Advanced PL/SQL Programming, Oracle8 PL/SQLProgramming, and Oracle9i PL/SQL Programming.Ron Hardman, OCP, is a Senior Technical Specialist with Oracle WorldwideTechnical Support. Prior to joining Oracle Corporation, he was an Oracle DatabaseDeveloper and Consultant. He is a frequent presenter on the topics of Oracle Textand Ultrasearch at Oracle User Group conferences, teaches classes on SQL and PL/SQL, and has published articles with Oracle Magazine and other online magazineson subjects related to information retrieval.Michael McLaughlin, D. CS., is the Senior Application Upgrade Manager forOracle Applications Release Engineering. He is currently working on the upgradearchitecture for the next release of Oracle Applications. He has worked with PL/SQLsince its first version in Oracle 6, and has authored customer support notes oncustomizing Oracle Applications with Pro*C and PL/SQL. He has taught computerscience and information technology at Regis University and Colorado TechnicalUniversity, including courses in database development, SQL, PL/SQL, and Java.About the Technical EditorCheryl Riniker is a Senior Technical Specialist with Oracle Worldwide Support inOracle’s Bug Diagnosis and Escalation division. She currently works with OracleApplications’ Financials Suite of products. She has used PL/SQL in developmentprojects since 1997, and received her DBA OCP in 2001. She graduated magnacum laude with an M.A. in English/ESL from Utah State University.Copyright 2004 by The McGraw-Hill Companies, Inc. Click here for terms of use.

This page intentionally left blank

This book is dedicated to ourdaughter Almarah Rose Urman,born May 1st, 2004.—Scott UrmanTo my wife Susan, you inspire me.Thank you for your patience and encouragement.To my daughter Jessica, and son Joshua, thank you foryour hugs and kisses. They never ran out.To my parents, thank you for your example.—Ron HardmanTo my wife Lisa, who is my constant,and our children Sarah, Joseph, Elise,Ian, Ariel, Callie, Nathan, and Spencer.Thank you for your inspiration, patience,and sacrifice that made my effortson this book possible.—Michael McLaughlin

This page intentionally left blank

Contents at a GlancePART IIntroduction1 Introduction to PL/SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Using SQL*Plus and JDeveloper3.233 PL/SQL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .454 Using SQL with PL/SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175 Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1816 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2137 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3038 Creating Procedures, Functions, and Packages . . . . . . . . . . . . . . . . . . . . . . 3359 Using Procedures, Functions, and Packages. . . . . . . . . . . . . . . . . . . . . . . . 38110 Database Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443PART IIAdvanced PL/SQL Features11 Intersession Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49912 External Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54313 Dynamic SQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58314 Introduction to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63515 Objects in the Database16 Large Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71117 Scheduling Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767ix

xOracle Database 10g PL/SQL ProgrammingPART IIIAppendixesA PL/SQL Reserved Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791B Guide to Supplied Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839

For more information about this title, click hereContentsPART IIntroduction1 Introduction to PL/SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Introduction to Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . .Note to Beginning Programmers . . . . . . . . . . . . . . . . . . . . . . . . . . . .PL/What? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Structured Query Language (SQL) . . . . . . . . . . . . . . . . . . . . . . . . . . .Relational Database Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . .PL/SQL vs. SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .PL/SQL vs. Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .PL/SQL History and Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Language Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Anonymous Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Object Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .PL/SQL Statement Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Interpreted . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Native Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Getting the Most from This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .345667911121515161616161717171818181819202021xi

xiiOracle Database 10g PL/SQL Programming2 Using SQL*Plus and JDeveloper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .SQL*Plus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Connecting to the Instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Testing the Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using SQL*Plus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Changing SQL*Plus Session Settings . . . . . . . . . . . . . . . . . . . . . . . . .Running a Script from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Output to the Screen Using SQL*Plus and PL/SQL . . . . . . . . . . . . . .JDeveloper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Installing JDeveloper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Working with PL/SQL in JDeveloper . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2324252628313133343436433 PL/SQL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The PL/SQL Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Basic Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Anonymous Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Named Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Nested Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Object Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Language Rules and Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Lexical Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .PL/SQL Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Scalar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Character/String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .NUMBER Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Date/Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Composite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Reference . .

He has worked with PL/SQL since its first version in Oracle 6, and has authored customer support notes on customizing Oracle Applications with Pro*C and PL/SQL. He has taught computer science and information technology at Regis University and Colorado Technical University, including courses in database development, SQL, PL/SQL, and Java.