Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
5.97 Mб


Fundamentals of the PL/SQL Language

There are six essentials in painting. The first is called spirit; the second, rhythm; the third, thought; the fourth, scenery; the fifth, the brush; and the last is the ink. —Ching Hao

The previous chapter provided an overview of PL/SQL. This chapter focuses on the detailed aspects of the language. Like other programming languages, PL/SQL has a character set, reserved words, punctuation, datatypes, and fixed syntax rules.

This chapter contains these topics:

Character Set on page 2-1

Lexical Units on page 2-1

Declarations on page 2-8

PL/SQL Naming Conventions on page 2-12

Scope and Visibility of PL/SQL Identifiers on page 2-14

Assigning Values to Variables on page 2-16

PL/SQL Expressions and Comparisons on page 2-17

Summary of PL/SQL Built-In Functions on page 2-28

Character Set

You write a PL/SQL program as lines of text using a specific set of characters:

Upperand lower-case letters A .. Z and a .. z

Numerals 0 .. 9

Symbols ( ) + - * / < > = ! ~ ^ ; : . ' @ % , " # $ & _ | { } ? [ ]

Tabs, spaces, and carriage returns

PL/SQL keywords are not case-sensitive, so lower-case letters are equivalent to corresponding upper-case letters except within string and character literals.

Lexical Units

A line of PL/SQL text contains groups of characters known as lexical units:

delimiters (simple and compound symbols) identifiers, which include reserved words literals


Fundamentals of the PL/SQL Language 2-1

Lexical Units

To improve readability, you can separate lexical units by spaces. In fact, you must separate adjacent identifiers by a space or punctuation. The following line is not allowed because the reserved words END and IF are joined:

IF x > y THEN high := x; ENDIF; -- not allowed, must be END IF

You cannot embed spaces inside lexical units except for string literals and comments. For example, the following line is not allowed because the compound symbol for assignment (:=) is split:

count : = count + 1; -- not allowed, must be :=

To show structure, you can split lines using carriage returns, and indent lines using spaces or tabs. The formatting makes the IF statement on the right more readable:

IF x>y THEN max:=x;ELSE max:=y;END IF;


IF x >




max :=









max :=








A delimiter is a simple or compound symbol that has a special meaning to PL/SQL. For example, you use delimiters to represent arithmetic operations such as addition and subtraction.

Symbol Meaning

+addition operator

%attribute indicator

'character string delimiter

. component selector

/division operator


expression or list delimiter


expression or list delimiter

:host variable indicator

,item separator

*multiplication operator

"quoted identifier delimiter

=relational operator

<relational operator

>relational operator

@remote access indicator

;statement terminator

-subtraction/negation operator

2-2 PL/SQL User's Guide and Reference

Lexical Units






assignment operator


association operator

||concatenation operator

**exponentiation operator

<<label delimiter (begin)

>>label delimiter (end)


multi-line comment delimiter (begin)


multi-line comment delimiter (end)


range operator


relational operator


relational operator


relational operator


relational operator


relational operator


relational operator

--single-line comment indicator


You use identifiers to name PL/SQL program items and units, which include constants, variables, exceptions, cursors, cursor variables, subprograms, and packages. Some examples of identifiers follow:

X t2

phone# credit_limit LastName oracle$number

An identifier consists of a letter optionally followed by more letters, numerals, dollar signs, underscores, and number signs. Other characters such as hyphens, slashes, and spaces are not allowed, as the following examples show:


-- not allowed because of ampersand


-- not allowed because of hyphen



not allowed


of slash

user id


not allowed


of space

Adjoining and trailing dollar signs, underscores, and number signs are allowed:

money$$$tree SN## try_again_

You can use upper, lower, or mixed case to write identifiers. PL/SQL is not case sensitive except within string and character literals. If the only difference between identifiers is the case of corresponding letters, PL/SQL considers them the same:


Fundamentals of the PL/SQL Language 2-3

Lexical Units

LastName -- same as lastname

LASTNAME -- same as lastname and LastName

The size of an identifier cannot exceed 30 characters. Every character, including dollar signs, underscores, and number signs, is significant. For example, PL/SQL considers the following identifiers to be different:

lastname last_name

Identifiers should be descriptive. Avoid obscure names such as cpm. Instead, use meaningful names such as cost_per_thousand.

Reserved Words

Some identifiers, called reserved words, have a special syntactic meaning to PL/SQL. For example, the words BEGIN and END are reserved. Trying to redefine a reserved word causes a compilation error. Instead, you can embed reserved words as part of a longer identifier:



-- end BOOLEAN;

-- not allowed; causes compilation error

end_of_game BOOLEAN;

-- allowed









Often, reserved words are written in upper case for readability. For a list of reserved words, see Appendix F.

Predefined Identifiers

Identifiers globally declared in package STANDARD, such as the exception INVALID_NUMBER, can be redeclared. However, redeclaring predefined identifiers is error prone because your local declaration overrides the global declaration.

Quoted Identifiers

For flexibility, PL/SQL lets you enclose identifiers within double quotes. Quoted identifiers are seldom needed, but occasionally they can be useful. They can contain any sequence of printable characters including spaces but excluding double quotes. Thus, the following identifiers are valid:

"X+Y" "last name"

"on/off switch" "employee(s)"

"*** header info ***"

The maximum size of a quoted identifier is 30 characters not counting the double quotes. Though allowed, using PL/SQL reserved words as quoted identifiers is a poor programming practice.


A literal is an explicit numeric, character, string, or Boolean value not represented by an identifier. The numeric literal 147 and the Boolean literal FALSE are examples.

2-4 PL/SQL User's Guide and Reference

Lexical Units

Numeric Literals

Two kinds of numeric literals can be used in arithmetic expressions: integers and reals. An integer literal is an optionally signed whole number without a decimal point. Some examples follow:

030 6 -14 0 +32767

A real literal is an optionally signed whole or fractional number with a decimal point. Several examples follow:

6.6667 0.0 -12.0 3.14159 +8300.00 .5 25.

PL/SQL considers numbers such as 12.0 and 25. to be reals even though they have integral values.

Numeric literals cannot contain dollar signs or commas, but can be written using scientific notation. Simply suffix the number with an E (or e) followed by an optionally signed integer. A few examples follow:

2E5 1.0E-7 3.14159e0 -1E38 -9.5e-3

E stands for "times ten to the power of." As the next example shows, the number after E is the power of ten by which the number before E is multiplied (the double asterisk (**) is the exponentiation operator):

5E3 = 5 * 10**3 = 5 * 1000 = 5000

The number after E also corresponds to the number of places the decimal point shifts. In the last example, the implicit decimal point shifted three places to the right. In this example, it shifts three places to the left:

5E-3 = 5 * 10**-3 = 5 * 0.001 = 0.005

As the following example shows, if the value of a numeric literal falls outside the range 1E-130 .. 10E125, you get a compilation error:



n := 10E127; -- causes a 'numeric overflow or underflow' error END;


Real literals can also use the trailing letters f and d to specify the types



x BINARY_FLOAT := sqrt(2.0f); -- Single-precision floating-point number y BINARY_DOUBLE := sqrt(2.0d); -- Double-precision floating-point number




Character Literals

A character literal is an individual character enclosed by single quotes (apostrophes). Character literals include all the printable characters in the PL/SQL character set: letters, numerals, spaces, and special symbols. Some examples follow:

'Z' '%' '7' ' ' 'z' '('

Fundamentals of the PL/SQL Language 2-5

Lexical Units

PL/SQL is case sensitive within character literals. For example, PL/SQL considers the literals 'Z' and 'z' to be different. Also, the character literals '0'..'9' are not equivalent to integer literals but can be used in arithmetic expressions because they are implicitly convertible to integers.

String Literals

A character value can be represented by an identifier or explicitly written as a string literal, which is a sequence of zero or more characters enclosed by single quotes. Several examples follow:

'Hello, world!' 'XYZ Corporation' '10-NOV-91'

'He said "Life is like licking honey from a thorn."' '$1,000,000'

All string literals except the null string ('') have datatype CHAR.

To represent an apostrophe within a string, you can write two single quotes, which is not the same as writing a double quote:

'I''m a string, you''re a string.'

Doubling the quotation marks within a complicated literal, particularly one that represents a SQL statement, can be tricky. You can also use the following notation to define your own delimiter characters for the literal. You choose a character that is not present in the string, and then do not need to escape other single quotation marks inside the literal:

--q'!...!' notation lets us use single quotes inside the literal. string_var := q'!I'm a string, you're a string.!';

--To use delimiters [, {, <, and (, pair them with ], }, >, and ).

--Here we pass a string literal representing a SQL statement

--to a subprogram, without doubling the quotation marks around


func_call(q'[select index_name from user_indexes where status = 'INVALID']');

-- For NCHAR and NVARCHAR2 literals, use the prefix nq instead of q. where_clause := nq'#where col_value like '%é'#';

PL/SQL is case sensitive within string literals. For example, PL/SQL considers the following literals to be different:



Boolean Literals

Boolean literals are the predefined values TRUE, FALSE, and NULL (which stands for a missing, unknown, or inapplicable value). Remember, Boolean literals are values, not strings. For example, TRUE is no less a value than the number 25.

Datetime Literals

Datetime literals have various formats depending on the datatype. For example:


d1 DATE := DATE ’1998-12-25’;

t1 TIMESTAMP := TIMESTAMP ’1997-10-22 13:01:01’;

2-6 PL/SQL User's Guide and Reference

Соседние файлы в папке Oracle 10g