Oracle Pl/SQL Language Pocket Reference: A Guide to Oracle's Pl/SQL Language Fundamentals 9781491920008, 1491920009

Be more productive with the Oracle PL/SQL language. The fifth edition of this popular pocket reference puts the syntax o

454 67 1MB

English Pages 190 [189] Year 2015

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Copyright......Page 4
Table of Contents......Page 5
Introduction......Page 7
PL/SQL Language Fundamentals......Page 8
Identifiers......Page 9
Boolean, Numeric, and String Literals......Page 10
Numeric Literals......Page 11
Datetime Interval Literals......Page 12
Delimiters......Page 13
Comments......Page 14
Pragmas......Page 15
Block Structure......Page 16
Variables and Program Data......Page 18
Scalar Datatypes......Page 19
LOB Datatypes......Page 25
NULLs in PL/SQL......Page 26
Declaring Variables......Page 27
Anchored Declarations......Page 28
Programmer-Defined Subtypes......Page 29
Conditional Control Statements......Page 30
Sequential Control Statements......Page 34
Numeric FOR Loop......Page 36
Cursor FOR Loop......Page 37
WHILE Loop......Page 38
CONTINUE Statement......Page 39
Database Interaction......Page 41
Transaction Management......Page 42
Autonomous Transactions......Page 45
Explicit Cursors......Page 46
Implicit Cursors......Page 50
Native Dynamic SQL......Page 54
DBMS_SQL......Page 56
Cursor Variables......Page 57
Cursor Expressions......Page 59
Exception Handling......Page 60
Declaring Exceptions......Page 61
Raising Exceptions......Page 63
Propagation......Page 64
Declaring Records......Page 68
Referencing a Record’s Fields......Page 69
Assigning Records......Page 70
Records and DML......Page 71
Collections in PL/SQL......Page 72
Declaring a Collection......Page 75
Initializing a Collection......Page 76
Adding and Removing Elements......Page 77
Nested Table Functions......Page 78
Collection Methods......Page 81
Bulk Binds......Page 85
Built-in Functions......Page 90
Built-in Regular Expression Functions......Page 105
Stored Procedures and Functions......Page 111
Procedures......Page 112
Functions......Page 113
Parameters......Page 114
Local Programs......Page 118
Program Overloading......Page 119
Forward Declarations......Page 120
Table Functions......Page 121
Function Result Cache......Page 122
Privileges and Stored PL/SQL......Page 124
Triggers......Page 126
Creating Triggers......Page 127
DML Events......Page 131
Compound DML Triggers......Page 132
Database Events......Page 134
Package Structure......Page 135
Package Data......Page 137
SERIALLY_REUSABLE Pragma......Page 138
Package Initialization......Page 139
Calling a Function......Page 140
Column Name Versus Function Name Precedence......Page 142
Object Types......Page 143
Methods......Page 146
Methods in Subtypes......Page 150
Manipulating Objects in PL/SQL and SQL......Page 152
Upcasting and Downcasting......Page 153
Changing Object Types......Page 156
Compiling Stored PL/SQL Programs......Page 158
Conditional Compilation......Page 161
Compiler Warnings......Page 164
Optimizing Compiler......Page 167
Performing Native Compilation of PL/SQL......Page 169
Java Language Integration......Page 170
Example......Page 171
Publishing Java to PL/SQL......Page 172
Data Dictionary......Page 174
Index......Page 175
Recommend Papers

Oracle Pl/SQL Language Pocket Reference: A Guide to Oracle's Pl/SQL Language Fundamentals
 9781491920008, 1491920009

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Co

h

rs

5t

ve

le

iti

ac

Ed

Or

ta

on

Da

ba

Oracle PL/SQL Language Pocket Reference

se 12 c

A GUIDE TO ORACLE'S PL/SQL LANGUAGE FUNDAMENTALS

Steven Feuerstein, Bill Pribyl & Chip Dawes

Oracle PL/SQL Language Pocket Reference Be more productive with the Oracle PL/SQL language. The fifth edition of this popular pocket reference puts the syntax of specific PL/SQL language elements right at your fingertips, including features added in Oracle Database 12c. Whether you’re a developer or database administrator, when you need answers quickly, the Oracle PL/SQL Language Pocket Reference will save you hours of frustration with concise summaries of: ■■ Fundamental language elements, such as block structure,

datatypes, and declarations ■■ Statements for program control, cursor management, and

exception handling ■■ Records, procedures, functions, triggers, and packages ■■ Execution of PL/SQL functions in SQL ■■ Compilation options, object-oriented features, collections,

and Java integration Steven Feuerstein one of the world’s leading experts on the Oracle PL/SQL language, is also the author of Oracle PL/SQL Best Practices (O’Reilly). Bill Pribyl, coauthor of the bestselling Oracle PL/SQL Programming, is also a teacher and software consultant. Chip Dawes, a consultant with PwC’s Health Industry Advisory services, has worked with Oracle Database versions 5 through 12. OR ACLE/DATABA SES

US $15.99

oreilly.com, Twitter: @oreillymedia

CAN $18.99

ISBN: 978-1-491-92000-8

FIFTH EDITION

Oracle PL/SQL Language Pocket Reference

Steven Feuerstein, Bill Pribyl, and Chip Dawes

Oracle PL/SQL Language Pocket Reference by Steven Feuerstein, Bill Pribyl, and Chip Dawes Copyright © 2015 Steven Feuerstein, Bill Pribyl, Chip Dawes. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebasto‐ pol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promo‐ tional use. Online editions are also available for most titles (http://safaribook‐ sonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected].

Editor: Tim McGovern Production Editor: Shiny Kalapurakkel Copyeditor: Amanda Kersey Proofreader: Kim Cofer Indexer: Lucie Haskins Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest September 2015:

Fifth Edition

Revision History for the Fifth Edition 2015-09-04:

First Release

See http://oreilly.com/catalog/errata.csp?isbn=9781491920008 details.

for

release

The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Oracle PL/SQL Language Pocket Reference, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellec‐ tual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.

978-1-491-92000-8 [LSI]

Table of Contents

Introduction PL/SQL Language Fundamentals Variables and Program Data Conditional and Sequential Control Loops Database Interaction Cursors in PL/SQL Exception Handling Records in PL/SQL Collections in PL/SQL Built-in Functions and Packages Stored Procedures and Functions Triggers Packages Calling PL/SQL Functions in SQL Object-Oriented Features Compilation Java Language Integration Index

1 2 12 24 30 35 40 54 62 66 84 105 120 129 134 137 152 164 169

iii

Oracle PL/SQL Language Pocket Reference

Introduction The Oracle PL/SQL Language Pocket Reference is a quick refer‐ ence guide to the PL/SQL programming language, which pro‐ vides procedural extensions to the SQL relational database lan‐ guage. 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 program‐ ming language is assumed. For more information, see the fol‐ lowing O’Reilly books: • Oracle PL/SQL Programming, Sixth Edition, by Steven Feuerstein with Bill Pribyl • Oracle PL/SQL Best Practices, Second Edition, by Steven Feuerstein • Oracle Essentials, Fifth Edition, by Rick Greenwald, Rob‐ ert Stackowiak, and Jonathan Stern Wherever a package, program, or function is supported only for a particular version of the Oracle database (e.g., Oracle Database 12c), we indicate this in the text.

1

Acknowledgments We are grateful to all those who helped in the preparation of this book. In particular, thanks to Patrick Barel, Indu Janar‐ danan, and Prachi Sharma for their technical reviews (though all mistakes and omissions remain the responsibility of the authors). Bryn Llewellyn gave us thorough and thoroughly helpful feedback on previous editions. Thanks as well to firstedition reviewers Eric J. Givler and Stephen Nelson and to second- and third-edition reviewer Jonathan Gennick. In addi‐ tion, we appreciate all the good work by the O’Reilly crew, especially editor Tim McGovern, in editing and producing this book.

Conventions UPPERCASE indicates PL/SQL keywords, as well as certain identifiers used by Oracle Corporation as built-in function and package names. Italic indicates filenames and directories, as well as the first use of a term. Constant width is used for code examples, literals, and identifi‐

ers.

Constant width bold indicates user input in examples showing

an interaction.

[] enclose optional items in syntax descriptions. {} enclose a list of items in syntax descriptions; you must choose one item from the list. | separates bracketed list items in syntax descriptions.

PL/SQL Language Fundamentals This section summarizes the fundamental components of the PL/SQL language: characters, identifiers, literals, delimiters,

2

|

Oracle PL/SQL Language Pocket Reference

use of comments and pragmas, and construction of statements and blocks.

PL/SQL Character Set The PL/SQL language is constructed from letters, digits, sym‐ bols, and whitespace, as defined in the following table: Type

Characters

Letters

A–Z, a–z

Digits

0—9

Symbols

~!@#$%*( )_−+=|:;"'< >,^.?/

Whitespace Space, tab, newline, carriage return

Characters are grouped together into four lexical units: identifi‐ ers, literals, delimiters, and comments.

Identifiers Identifiers are names for PL/SQL objects, such as constants, variables, exceptions, procedures, cursors, and reserved words. Identifiers have the following characteristics: • Can be up to 30 characters in length • Cannot include whitespace (space, tab, carriage return) • Must start with a letter • Can include a dollar sign ($), an underscore (_), and a pound sign (#) • Are not case-sensitive Using PL/SQL’s reserved words as identifiers in your programs is not a good idea and can result in compilation or runtime errors that are difficult to troubleshoot.

PL/SQL Language Fundamentals

| 3

TIP Earlier editions of this book included a list of reserved words. However, Oracle Database 12c has more than 2,080 reserved words as listed in the V$RESERVED_WORDS data dictionary view. Although you can in fact use many of these words as your own identifiers without any com‐ plaints from the compiler, doing so can be confusing. We suggest you write PL/SQL using an editor that highlights reserved words automatically and avoid using them to name your own variables or subprograms.

If you enclose an identifier within double quotes, all but the first of these rules are ignored. For example, the following dec‐ laration is valid (although not exactly sensible): DECLARE "1 ^abc" VARCHAR2(100); BEGIN IF "1 ^abc" IS NULL THEN ... END;

Boolean, Numeric, and String Literals Literals are specific values not represented by identifiers. For example, TRUE, 3.14159, 6.63E-34, 'Moby Dick', and NULL are all literals of type Boolean, number, or string. There are no com‐ plex datatype literals because their values are internal represen‐ tations; complex types receive values through direct assignment or via constructors. Unlike the rest of PL/SQL, literals are casesensitive. To embed single quotes within a string literal, place two single quotes next to each other. You can define your own quoting mechanism for string literals in both your SQL and PL/SQL statements. Use the characters q' (q followed by a straight single quote) to designate the programmer-defined delimiter for your string literal. Termi‐ nate the literal string with the programmer-defined delimiter followed by a trailing single quote—for example, q'!my 4

|

Oracle PL/SQL Language Pocket Reference

string!'. NCHAR and NVARCHAR delimiters are preceded by the letters nq, as in nq'^nchar string^'. This technique can simplify your code when consecutive single quotes appear within a string, such as the literals in a SQL statement. If you define your delimiter with one of the four bracketing charac‐ ters ( [ {