Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Oracle ® PL/SQL ™ by Example FOURTH EDITION Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. This page intentionally left blank Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Oracle ® PL/SQL ™ by Example FOURTH EDITION BENJAMIN ROSENZWEIG ELENA SILVESTROVA RAKHIMOV Upper Saddle River, NJ • Boston • Indianapolis • San Francisco • New York • Toronto • Montreal • London • Munich • Paris • Madrid • Cape Town • Sydney • Tokyo • Singapore • Mexico City Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales 800-382-3419 corpsales@pearsontechgroup.com For sales outside the United States, please contact: International Sales international@pearson.com Visit us on the Web: www.informit.com/ph Library of Congress Cataloging-in-Publication Data Rosenzweig, Benjamin. Oracle PL/SQL by example / Benjamin Rosenzweig, Elena Silvestrova Rakhimov. p. cm. ISBN 0-13-714422-9 (pbk. : alk. paper) 1. PL/SQL (Computer program language) 2. Oracle (Computer file) 3. Relational databases. I. Rakhimov, Elena Silvestrova. II. Title. QA76.73.P258R68 2008 005.75’6—dc22 2008022398 Copyright © 2009 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc. Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax: (617) 671 3447 ISBN-13: 978-0-137-14422-8 ISBN-10: 0-137-14422-9 Text printed in the United States on recycled paper at Edwards Brothers in Ann Arbor, Michigan First printing August 2008 Editor-in-Chief: Mark Taub Acquisitions Editor: Trina MacDonald Development Editor: Songlin Qiu Managing Editor: Kristy Hart Project Editor: Todd Taber Copy Editor: Gayle Johnson Indexer: Erika Millen Proofreader: Debbie Williams Technical Reviewers: Oleg Voskoboynikov, Shahdad Moradi Publishing Coordinator: Olivia Basegio Cover Designer: Chuti Prasertsith Composition: Nonie Ratcliff Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. To my parents, Rosie and Sandy Rosenzweig, for their love and support. —Benjamin Rosenzweig To Sean. —Elena Silvestrova Rakhimov Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. This page intentionally left blank Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Acknowledgments xiv About the Authors xv Introduction xvii CHAPTER 1 PL/SQL Concepts 1 LAB 1.1 PL/SQL in Client/Server Architecture 2 1.1.1 Use PL/SQL Anonymous Blocks 8 1.1.2 Understand How PL/SQL Gets Executed 10 LAB 1.2 PL/SQL in SQL*Plus 12 1.2.1 Use Substitution Variables 16 1.2.2 Use the DBMS_OUTPUT.PUT_LINE Statement 17 Chapter 1 Try It Yourself 19 CHAPTER 2 General Programming Language Fundamentals 21 LAB 2.1 PL/SQL Programming Fundamentals 22 2.1.1 Make Use of PL/SQL Language Components 23 2.1.2 Make Use of PL/SQL Variables 24 2.1.3 Handle PL/SQL Reserved Words 26 2.1.4 Make Use of Identifiers in PL/SQL 27 2.1.5 Make Use of Anchored Datatypes 28 2.1.6 Declare and Initialize Variables 31 2.1.7 Understand the Scope of a Block, Nested Blocks, and Labels 34 Chapter 2 Try It Yourself 37 CHAPTER 3 SQL in PL/SQL 39 LAB 3.1 Making Use of DML in PL/SQL 40 3.1.1 Use the Select INTO Syntax for Variable Initialization 41 3.1.2 Use DML in a PL/SQL Block 42 3.1.3 Make Use of a Sequence in a PL/SQL Block 44 LAB 3.2 Making Use of SAVEPOINT 45 3.2.1 Make Use of COMMIT, ROLLBACK, and SAVEPOINT in a PL/SQL Block 48 Chapter 3 Try It Yourself 51 Contents Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. CHAPTER 4 Conditional Control: IF Statements 53 LAB 4.1 IF Statements 54 4.1.1 Use the IF-THEN Statement 58 4.1.2 Use the IF-THEN-ELSE Statement 62 LAB 4.2 ELSIF Statements 65 4.2.1 Use the ELSIF Statement 69 LAB 4.3 Nested IF Statements 74 4.3.1 Use Nested IF Statements 76 Chapter 4 Try It Yourself 80 CHAPTER 5 Conditional Control: CASE Statements 81 LAB 5.1 CASE Statements 82 5.1.1 Use the CASE Statement 89 5.1.2 Use the Searched CASE Statement 91 LAB 5.2 CASE Expressions 96 5.2.1 Use the CASE Expression 100 LAB 5.3 NULLIF and COALESCE Functions 103 5.3.1 The NULLIF Function 107 5.3.2 Use the COALESCE Function 109 Chapter 5 Try It Yourself 112 CHAPTER 6 Iterative Control: Part I 113 LAB 6.1 Simple Loops 114 6.1.1 Use Simple Loops with EXIT Conditions 118 6.1.2 Use Simple Loops with EXIT WHEN Conditions 120 LAB 6.2 WHILE Loops 124 6.2.1 Use WHILE Loops 128 LAB 6.3 Numeric FOR Loops 132 6.3.1 Use Numeric FOR Loops with the IN Option 137 6.3.2 Use Numeric FOR Loops with the REVERSE Option 139 Chapter 6 Try It Yourself 142 CHAPTER 7 Iterative Control: Part II 143 LAB 7.1 The CONTINUE Statement 144 7.1.1 Use the CONTINUE Statement 146 7.1.2 Use the CONTINUE WHEN Condition 152 viii Contents Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. LAB 7.2 Nested Loops 154 7.2.1 Use Nested Loops 157 Chapter 7 Try It Yourself 161 CHAPTER 8 Error Handling and Built-in Exceptions 163 LAB 8.1 Handling Errors 164 8.1.1 Understand the Importance of Error Handling 167 LAB 8.2 Built-in Exceptions 169 8.2.1 Use Built-in Exceptions 174 Chapter 8 Try It Yourself 178 CHAPTER 9 Exceptions 179 LAB 9.1 Exception Scope 180 9.1.1 Understand the Scope of an Exception 183 LAB 9.2 User-Defined Exceptions 188 9.2.1 Use User-Defined Exceptions 193 LAB 9.3 Exception Propagation 197 9.3.1 Understand How Exceptions Propagate 203 9.3.2 Reraise Exceptions 206 Chapter 9 Try It Yourself 209 CHAPTER 10 Exceptions: Advanced Concepts 211 LAB 10.1 RAISE_APPLICATION_ERROR 212 10.1.1 Use RAISE_APPLICATION_ERROR 215 LAB 10.2 EXCEPTION_INIT Pragma 217 10.2.1 USE the EXCEPTION_INIT Pragma 219 LAB 10.3 SQLCODE and SQLERRM 222 10.3.1 Use SQLCODE and SQLERRM 225 Chapter 10 Try It Yourself 227 CHAPTER 11 Introduction to Cursors 229 LAB 11.1 Cursor Manipulation 230 11.1.1 Make Use of Record Types 234 11.1.2 Process an Explicit Cursor 235 11.1.3 Make Use of Cursor Attributes 240 11.1.4 Put It All Together 242 Contents ix Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... are Oracle server, Oracle Forms, and Oracle Reports The PL/SQL engine processes and executes any PL/SQL statements and sends any SQL statements to the SQL statement processor The SQL statement processor is always located on the Oracle server Figure 1.1 illustrates the PL/SQL engine residing on the Oracle server When the PL/SQL engine is located on the server, the whole PL/SQL block is passed to the PL/SQL. .. in the following example FOR EXAMPLE SELECT REGEXP_INSTR ( 'Oracle PL/SQL By Example Updated for Oracle 11g', '((ora)(cle))', 1, 2, 0, 'i') FROM dual; REGEXP_INSTR('ORACLEPL/SQLBYEXAMPLEUPDATEDFORORACLE11G', ) -38 The REGEXP_INSTR function returns the position of the first character in the source string 'Oracle PL/SQL ' corresponding to the second occurrence of the first... xviii Introduction FOR EXAMPLE SELECT REGEXP_COUNT ( 'Oracle PL/SQL By Example Updated for Oracle 11g', 'ora', 1, 'i') FROM dual; REGEXP_COUNT('ORACLEPL/SQLBYEXAMPLEUPDATEDFORORACLE11G','ORA',1,'I') -2 The REGEXP_COUNT function returns how many times the search pattern 'ora' appears in the source string 'Oracle PL/SQL ' 1 indicates the position of the source string... displayed on your client machine PL/SQL Block PL/SQL and SQL Statements PL/SQL Block PL/SQL Statements PL/SQL Statement Processor PL/SQL Engine SQL Statements SQL Statement Processor Oracle Server FIGURE 1.1 The PL/SQL engine and Oracle server Now, assume that you need to issue multiple SELECT statements Each SELECT statement is a request against the database and is sent to the Oracle server The results of... Application Using PL/SQL SQL Statement Processor PL/SQL Engine FIGURE 1.2 PL/SQL in client/server architecture In addition, applications written in PL/SQL are portable They can run in any environment that Oracle can run in Because PL/SQL does not change from one environment to the next, different tools can use a PL/SQL script PL/SQL BLOCK STRUCTURE A block is the most basic unit in PL/SQL All PL/SQL programs... passed to the PL/SQL engine on the Oracle server The PL/SQL engine processes the block according to Figure 1.1 When the PL/SQL engine is located on the client, as it is in Oracle Developer Tools, the PL/SQL processing is done on the client side All SQL statements that are embedded within the PL/SQL block are sent to the Oracle server for further processing When the PL/SQL block contains no SQL statements,... realize that PL/SQL is not a stand-alone programming language PL/SQL is a part of the Oracle RDBMS, and it can reside in two environments, the client and the server As a result, it is very easy to move PL/SQL modules between server-side and client-side applications In both environments, any PL/SQL block or subroutine is processed by the PL/SQL engine, which is a special component of many Oracle products... performance optimizer enables the PL/SQL compiler to rearrange PL/SQL code to enhance performance The optimization level used by the PL/SQL compiler is controlled by the PLSQL_OPTIMIZE_ LEVEL parameter Its values range from 0 to 2, where 2 is the default value This means that the PL/SQL compiler performs optimization by default Starting with Oracle 11g, the PL/SQL compiler can perform subprogram inlining Subprogram... CHAPTER 1 PL/SQL Concepts CHAPTER OBJECTIVES In this chapter, you will learn about PL/SQL in client/server architecture PL/SQL in SQL*Plus PL/SQL stands for Procedural Language Extension to SQL PL/SQL extends SQL by adding programming structures and subroutines available in any high-level language In this chapter, you will see examples that illustrate the syntax and rules of the language PL/SQL is... shared by various middle-tier processes The new package, DBMS_CONNECTION_POOL, enables database administrators to start and stop DRCP and configure its parameters Automatic Subprogram Inlining The PL/SQL compiler translates PL/SQL code into machine code Starting with Oracle 10g, the PL/SQL compiler can use the performance optimizer when compiling PL/SQL code The performance optimizer enables the PL/SQL . REGEXP_COUNT (&apos ;Oracle PL/SQL By Example Updated for Oracle 11g', 'ora', 1, 'i') FROM dual; REGEXP_COUNT('ORACLEPL/SQLBYEXAMPLEUPDATEDFORORACLE11G','ORA',1,'I'). (&apos ;Oracle PL/SQL By Example Updated for Oracle 11g', '((ora)(cle))', 1, 2, 0, 'i') FROM dual; REGEXP_INSTR('ORACLEPL/SQLBYEXAMPLEUPDATEDFORORACLE11G', .)