FOURTH EDITION Oracle PL/SQL Language Pocket Reference Steven Feuerstein, Bill Pribyl, and Chip Dawes Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo Oracle PL/SQL Language Pocket Reference, Fourth Edition by Steven Feuerstein, Bill Pribyl, and Chip Dawes Copyright © 2008 Chip Dawes, Steven Feuerstein, and Bill Pribyl All rights reserved Printed in Canada Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (safari.oreilly.com) For more information, contact our corporate/ institutional sales department: (800) 998-9938 or corporate@oreilly.com Editors: Deborah Russell and Mary Treseler Production Editor: Mary Brady Proofreader: Mary Brady Indexer: Johnna VanHoose Dinse Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Printing History: April 1999: February 2003: April 2004: October 2007: First Edition Second Edition Third Edition Fourth Edition Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc The Pocket Reference series designations, Oracle PL/SQL Language Pocket Reference, the image of ants, and related trade dress are trademarks of O’Reilly Media, Inc 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 O’Reilly Media, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps Oracle® and all Oracle-based trademarks and logos are trademarks or registered trademarks of Oracle Corporation, Inc in the United States and other countries O’Reilly Media, Inc is independent of Oracle Corporation Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc in the United States and other countries O’Reilly Media, Inc is independent of Sun Microsystems, Inc While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein ISBN-10: 0-596-51404-2 ISBN-13: 978-0-596-51404-4 [TM] Contents Introduction Acknowledgments Conventions 1 PL/SQL Language Fundamentals PL/SQL Character Set Identifiers Boolean, Numeric, and String Literals Numeric Literals Datetime Interval Literals Delimiters Comments Pragmas Statements Block Structure 2 6 8 9 Variables and Program Data Scalar Datatypes LOB Datatypes Implicit Datatype Conversions NULLs in PL/SQL Declaring Variables Anchored Declarations Programmer-Defined Subtypes 11 12 18 19 19 19 22 23 iii Conditional and Sequential Control Conditional Control Statements Sequential Control Statements 23 23 27 Loops Simple Loop Numeric FOR Loop Cursor FOR Loop WHILE Loop REPEAT UNTIL Loop Emulation EXIT Statement CONTINUE Statement (Oracle Database 11g) Loop Labels 28 29 29 30 30 31 31 31 33 Database Interaction Sequences in PLSQL Transaction Management Autonomous Transactions 34 34 34 37 Cursors in PL/SQL Explicit Cursors Implicit Cursors Dynamic Cursors DBMS_SQL SQL Injection and Bind Variables Cursor Variables Cursor Expressions 38 38 42 45 46 47 48 49 Exception Handling Declaring Exceptions Raising Exceptions Scope Propagation 50 51 53 54 54 iv | Contents Records in PL/SQL Declaring Records Referencing Fields of Records Assigning Records Records and DML Nested Records 56 57 58 58 59 60 Collections in PL/SQL Declaring a Collection Initializing a Collection Adding and Removing Elements Nested Table Functions Collection Methods Collections and Privileges Nested Collections Bulk Binds 61 63 64 65 65 68 71 71 71 Built-in Functions and Packages Built-in Functions Built-in Regular Expression Functions Built-in Packages 75 75 87 93 Stored Procedures and Functions Procedures Functions Parameters Local Programs Program Overloading Forward Declarations Table Functions Function Result Cache Privileges and Stored PL/SQL 97 98 99 100 103 104 106 106 107 109 Contents | v Triggers Creating Triggers Trigger Predicates DML Events Compound DML Triggers DDL Events Database Events 109 110 114 114 115 117 117 Packages Package Structure Referencing Package Elements Package Data SERIALLY_REUSABLE Pragma Package Initialization 117 118 120 120 120 121 Calling PL/SQL Functions in SQL Calling a Function Calling Packaged Functions in SQL Column/Function Name Precedence 122 123 124 125 Object-Oriented Features Object Types Type Inheritance Methods Methods in Subtypes Manipulating Objects in PL/SQL and SQL Upcasting and Downcasting Changing Object Types 125 126 128 128 132 133 135 138 Compilation Compiling Stored PL/SQL Programs Conditional Compilation Compiler Warnings Optimizing Compiler Performing Native Compilation of PL/SQL 139 140 142 145 147 149 vi | Contents Java Language Integration Example Publishing Java to PL/SQL Data Dictionary 152 153 154 155 Index 157 Contents | vii Chapter Oracle PL/SQL Language Pocket Reference Introduction The Oracle PL/SQL Language Pocket Reference is a quick reference guide to the PL/SQL programming language, which provides procedural extensions to the SQL relational database language and a range of Oracle development tools Where a package, program, or function is supported only for a particular version of the Oracle database (e.g., Oracle Database 11g), we indicate this in the text The purpose of this pocket reference is to help PL/SQL users find the syntax of specific language elements It is not a selfcontained user guide; basic knowledge of the PL/SQL programming language is assumed For more information, see the following O’Reilly books: Oracle PL/SQL Programming, Fourth Edition, by Steven Feuerstein with Bill Pribyl Learning Oracle PL/SQL, by Bill Pribyl with Steven Feuerstein Oracle PL/SQL Best Practices, Second Edition, by Steven Feuerstein Oracle in a Nutshell, by Rick Greenwald and David C Kreines Acknowledgments We are grateful to all those who helped in the preparation of this book In particular, thanks to Bryn Llewellyn for his ... 27 Loops Simple Loop Numeric FOR Loop Cursor FOR Loop WHILE Loop REPEAT UNTIL Loop Emulation EXIT Statement CONTINUE Statement (Oracle Database 11g) Loop Labels 28 29 29 30 30 31 31 31 33 Database. .. relational database language and a range of Oracle development tools Where a package, program, or function is supported only for a particular version of the Oracle database (e.g., Oracle Database 11g) ,... Addition operator | Oracle PL/SQL Language Pocket Reference Delimiter Description - Subtraction operator * Multiplication operator / Division operator ** Exponentiation operator || Concatenation operator