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 OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- 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 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Oracle PL/SQL Language Pocket Reference, the cover image, and related trade dress are trademarks of OReilly 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 intellectual 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]
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.
The purpose of this pocket reference is to help PL/SQL users find the syntax of specific language elements. It is not a self-contained user guide; basic knowledge of the PL/SQL programming language is assumed. For more information, see the following OReilly 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, Robert 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.
Acknowledgments
We are grateful to all those who helped in the preparation of this book. In particular, thanks to Patrick Barel, Indu Janardanan, 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 first-edition reviewers Eric J. Givler and Stephen Nelson and to second- and third-edition reviewer Jonathan Gennick. In addition, we appreciate all the good work by the OReilly 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 identifiers.
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, use of comments and pragmas, and construction of statements and blocks.
PL/SQL Character Set
The PL/SQL language is constructed from letters, digits, symbols, 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: identifiers, 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/SQLs 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.
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 complaints 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 declaration 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 complex datatype literals because their values are internal representations; complex types receive values through direct assignment or via constructors. Unlike the rest of PL/SQL, literals are case-sensitive. 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. Terminate the literal string with the programmer-defined delimiter followed by a trailing single quotefor example,