FOURTH EDITION Oracle PL/SQL Programming Steven Feuerstein with Bill Pribvl O'REILLY' Beijing Cambridge Farnham Köln Paris Sebastopol Taipei Tokyo
Table of Contents Preface xiii Part 1. Programming in PL/SQL 1. Introduction to PL/SQL 3 What Is PL/SQL? 3 The Origins of PL/SQL 4 So This Is PL/SQL 7 About PL/SQL Versions 11 Resources for PL/SQL Developers 18 Some Words of Advice 21. Creating and Running PL/SQL Code 25 SQL*Plus 25 Performing Essential PL/SQL Tasks 41 Calling PL/SQL from Other Languages 48 3. Language Fundamentals 55 PL/SQL Block Structure 55 The PL/SQL Character Set 64 Identifiers 66 Literals 71 The Semicolon Delimiter 75 Comments 75 The PRAGMA Keyword 77 Labels 78
Part PL/SQL Program Structure 4. Conditional and Sequential Control 83 IF Statements 83 CASE Statements and Expressions 92 The GOTO Statement 100 The NULL Statement 101 5. Iterative Processing with Loops 105 Loop Basics 105 The Simple Loop 108 The WHILE Loop 112 The Numeric FOR Loop 114 The Cursor FOR Loop 116 Loop Labels 119 Tips for Iterative Processing 120 6. Exception Handlers 125 Exception-Handling Concepts and Terminology 125 Defining Exceptions 128 Raising Exceptions 135 Handling Exceptions 138 Building an Effective Error Management Architecture 151 Making the Most of PL/SQL Error Management 160 Part PL/SQL Program Data 7. Working with Program Data 165 Naming Your Program Data 165 Overview of PL/SQL Datatypes 167 Declaring Program Data 172 Programmer-Defined Subtypes 179 Conversion Between Datatypes 180 8. Strings 189 String Datatypes 189 Working with Strings 193 String Function Quick Reference 217 in Table of Contents
9. Numbers 226 Numeric Datatypes 226 Number Conversions 238 Numeric Functions 251 10. Dates and Timestamps 257 Datetime Datatypes 257 Getting the Date and Time 262 Interval Datatypes 264 Datetime Conversions 269 Date and Timestamp Literals 283 Interval Conversions 284 Interval Literals 287 CAST and EXTRACT 288 Datetime Arithmetic 290 Date/Time Functions 299 11. Records 302 Records in PL/SQL 302 12. Collections 318 Collections Overview 319 Collection Methods (Built-Ins) 333 Working with Collections 341 Nested Table Multiset Operations 373 Maintaining Schema-Level Collections 380 13. Miscellaneous Datatypes 382 The BOOLEAN Datatype 382 The RAW Datatype 383 The UROWID and ROWID Datatypes 384 The LOB Datatypes 388 Working with LOBs 389 Predefined Object Types 412 Table of Contents I vii
Part IV. SQL in PL/SQL 14. DML and Transaction Management 425 DML in PL/SQL 426 Bulk DML with the FORALL Statement 435 Transaction Management 448 Autonomous Transactions 452 15. Data Retrieval 460 Cursor Basics 461 Wortring with Implicit Cursors 470 Working with Explicit Cursors 476 BULK COLLECT 491 SELECT...FOR UPDATE 497 Cursor Variables and REF CURSORs 501 Cursor Expressions 514 16. Dynamic SQL and Dynamic PL/SQL 518 NDS Statements 519 Binding Variables 530 Wortring with Objects and Collections 534 Dynamic PL/SQL 536 Recommendations for NDS 542 When to Use DBMS_SQL 548 NDS Utility Package 560 Part V. PL/SQL Application Construction 17. Procedures, Functions, and Parameters 565 Modular Code 566 Procedures 567 Functions 571 Parameters 579 Local Modules 591 Module Overloading 596 Forward Declarations 602 Advanced Topics 603 Go Forth and Modularize! 621 viii Table of Contents
18. Packages 622 Why Packages? 622 Rules for Building Packages 629 Rules for Calling Packaged Elements 637 Working with Package Data 638 When to Use Packages 647 Packages and Object Types 654 19. Triggers 655 DML Triggers 656 DDL Triggers 673 Database Event Triggers 683 INSTEAD OF Triggers 690 AFTER SUSPEND Triggers 697 Maintaining Triggers 705 20. Managing PL/SQL Code 708 Managing Code in the Database 709 Using Native Compilation 719 Using the Optimizing Compiler and Compile-Time Warnings 726 Conditional Compilation 739 Testing PL/SQL Programs 750 Debugging PL/SQL Programs 756 Tuning PL/SQL Programs 767 Protecting Stored Code 773 21. 1/0 and PL/SQL 777 Displaying Information 777 Reading and Writing Files 781 Sending Email 796 Working with Web-Based Data (HTTP) 807 Other Types of VO Available in PL/SQL 818 Part Vl. Advanced PL/SQL Topics 22. Application Security and PL/SQL 823 Security Overview 823 Encryption 825 fable of Contents ix
Row-Level Security 847 Application Contexts 866 Fine-Grained Auditing 873 23. Inside PL/SQL 883 Looking Under the Hood 884 PL/SQL's Optimizing Compiler 894 Dependency Management 898 Execution Authority Models 910 PL/SQL and Oracle Memory 918 Server-Side PL/SQL Processing: Reprise 933 What You Need to Know 937 24. Globalization and Localization in PL/SQL 939 Overview and Terminology 941 Unicode Primer 942 Character Semantics 953 String Sort Order 957 Multilingual Information Retrieval 962 Date/Time 967 Currency Conversion 972 Globalization Development Kit for PL/SQL 974 25. Object-Oriented Aspects of PL/SQL 981 Introduction to Oracle's Object Features 981 An Extended Example 984 Object Views 1023 Maintaining Object Types and Object Views 1035 Pontifications 1038 26. Calling Java from PL/SQL 1041 Oracle and Java 1041 Getting Ready to Use Java in Oracle 1043 A Simple Demonstration 1047 Using loadjava 1054 Using dropjava 1056 Managing Java in the Database 1056 Using DBMS_JAVA 1058 Publishing and Using Java in PL/SQL 1063 x I Table of Contents
27. External Procedures 1078 Introduction to External Procedures 1079 The Oracle Net Configuration 1083 Setting Up Multithreaded Mode 1086 Creating an Oracle Library 1089 Writing the Call Specification 1091 Raising an Exception from the Called C Program 1101 Nondefault Agents 1103 Maintaining External Procedures 1105 A. Regular Expression Metacharacters and Function Parameters 1109 B. Number Format Models 1114 C. Date Format Models 1117 Index 1123 Table of Contents 1 xi