Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
3
Добавлен:
01.07.2025
Размер:
5.06 Mб
Скачать

3.3.1.1 Language keywords

The PL/SQL compiler recognizes certain words as having certain semantics no matter what. For example, one very important reserved word is END, which terminates programs, IF statements, and loops. If you try to declare a variable named "end", as I do below:

DECLARE

end VARCHAR2(10) := 'blip'; /* Will not work! Can't use END as name. */

BEGIN

DBMS_OUTPUT.PUT_LINE (end);

END;

/

then you will get the subsequent compile error:

PLS-00103: Encountered the symbol "END" when expecting one of the following:

The appearance of the word "end" in the declaration section signals to PL/SQL the premature termination of that anonymous block.

3.3.1.2 Identifiers from standard package

In addition to avoiding identifiers that duplicate keywords, you should also avoid using identifiers that duplicate names Oracle has defined in a special package named STANDARD. Oracle defines quite a few identifiers in this package, including built-inexceptions like DUP_VAL_ON_INDEX,functions like UPPER, REPLACE, and TO_DATE, andsubtypes such as STRING. You can view a list of functions and procedures defined in STANDARD with this command:

SQL> DESC SYS.STANDARD

You may occasionally find that you want to use the name of a built-in from STANDARD. You can still reference the built-in form by prefixing it with STANDARD, as follows:

DECLARE

dup_val_on_index EXCEPTION; -- local re-declaration

BEGIN

...

INSERT INTO ... /* may raise the built-in exception */

...

RAISE dup_val_on_index; -- resolves to the locally declared exception

EXCEPTION

WHEN dup_val_on_index

THEN

/* handle the locally declared exception */

...

WHEN STANDARD.DUP_VAL_ON_INDEX

THEN

/* handle the usual exception */

...

END;

3.3.1.3 Approaches to avoiding reserved words

Finding a valid name for your identifier should be the least of your problems, as there are thousands and thousands of permutations of the legal characters. The question is, how will you know if you inadvertently use a reserved word in your own program? By some counts, there are hundreds of words known to PL/SQL!

You could just ignore this issue and deal with the compiler errors as they come up. This is what a lot of people do, but it can be a real drain on a workday because sometimes the error messages are completely off the wall. Some people use a high-priced development environment with a snazzy syntax-directed editor that alerts you to possible problems as you type. Personally, my favorite solution is to use a cheap (as in free) editor that is aware of PL/SQL's keywords and just highlights them automatically.[2]

[2] As a matter of fact, I helped create a syntax highlighting file and an automatic indentation file for an open source editor known as vim (derived from vi). Very cool. Visit the book's web site for details.

I have compiled a small list of the Oracle9i PL/SQL reserved words, shown in Table 3-4 (and available in the reserved.txt file on the O'Reilly site). Starting from the list of keywords Oracle publishes in the V$RESERVED_WORDS data dictionary view, I tried to declare a variable, and then a procedure, using the word as its identifier. If the compiler prevented me doing from one or both of those operations, I put the keyword on the list.

Table 3-4. Absolute minimal list of words to avoid using as PL/SQL identifiers

ACCESS

ADD

ALL

ALTER

AND

ANY

AS

ASC

AT

AUDIT

BEGIN

BETWEEN

BY

CASE

CHAR

CHECK

CLOSE

CLUSTER

COLUMN

COLUMNS

COMMENT

COMMIT

COMPRESS

CONNECT

CREATE

CURRENT

DEFAULT

CURSOR

DATE

DECIMAL

DECLARE

DEFAULT

DELETE

DESC

DISTINCT

DROP

ELSE

END

EXCLUSIVE

EXISTS

FILE

FLOAT

FOR

FROM

FUNCTION

GRANT

GROUP

HAVING

IDENTIFIED

IF

IMMEDIATE

IN

INCREMENT

INDEX

INDEXES

INITIAL

INSERT

INTEGER

INTERSECT

INTO

IS

LEVEL

LIKE

LOCK

LONG

MAXEXTENTS

MINUS

MLSLABEL

MODE

MODIFY

NOAUDIT

NOCOMPRESS

NOT

NOWAIT

NULL

NUMBER

OF

OFFLINE

ON

ONLINE

OPEN

OPTION

OR

ORDER

OVERLAPS

PACKAGE

PCTFREE

PRIOR

PRIVILEGES

PROCEDURE

PUBLIC

RAW

RENAME

RESOURCE

RETURN

REVOKE

ROLLBACK

ROW

ROWID

ROWNUM

ROWS

SAVEPOINT

SELECT

SESSION

SET

SHARE

SIZE

SMALLINT

START

SUCCESSFUL

SYNONYM

SYSDATE

TABLE

THEN

TO

TRIGGER

TYPE

UID

UNION

UNIQUE

UPDATE

USE

USER

VALIDATE

VALUES

VARCHAR

VARCHAR2

VIEW

WHEN

WHENEVER

WHERE

WITH

If you are running Oracle 8.1.5 or later, you can ask your DBA to grant you privilege to execute this statement:

SQL> SELECT * FROM V$RESERVED_WORDS;

Although the list is overwhelmingly long, it does not even include all the identifiers in STANDARD; the PL/SQL compiler does not care if you carelessly reuse identifiers declared there. All the more reason to use an intelligent editor.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]