Wednesday, April 21, 2010

Lexical Units in Oracle PL/SQL

PL/SQL is not case-sensitive, so lower-case letters are equivalent to corresponding upper-case letters except within string and character literals. A line of PL/SQL text contains groups of characters known as lexical units, which can be classified as follows:


delimiters (simple and compound symbols)

identifiers, which include reserved words

literals

comments

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.

You use identifiers to name PL/SQL program objects and units, which include constants, variables, exceptions, cursors, subprograms, and packages. Some identifiers called RESERVED WORDS, have a special syntactic meaning to PL/SQL and so cannot be redefined. For flexibility, PL/SQL lets you enclose identifiers within double quotes. Quoted identifiers are seldom needed, but occasionally they can be useful.

A literal is an explicit numeric, character, string, or Boolean value not represented by an identifier.

Two kinds of numeric literals can be used in arithmetic expressions: integers and reals.

String literal is a sequence of zero or more characters enclosed by single quotes. All string literals except the null string (`') belong to type CHAR. PL/SQL is case-sensitive within string literals.

Boolean literals are the predefined values TRUE and FALSE and the non-value NULL (which stands for a missing, unknown, or inapplicable value). Keep in mind that Boolean literals are not strings.

The PL/SQL compiler ignores comments but you should not. Adding comments to your program promotes readability and aids understanding. PL/SQL supports two comment styles: single-line and multiline. Single-line comments begin with a double hyphen (--) anywhere on a line and extend to the end of the line. Multiline comments begin with a slash-asterisk (/*), end with an asterisk-slash (*/), and can span multiple lines. You cannot nest comments.

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

However, you cannot embed spaces in 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

To show structure, you can divide lines using carriage returns and indent lines using spaces or tabs. Compare these IF statements for readability:

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

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