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

PL/SQL Naming Conventions

FOR item IN

(

SELECT first_name || ' ' || last_name complete_name FROM employees WHERE ROWNUM < 11

)

LOOP

-- Now we can refer to the field in the record using this alias. dbms_output.put_line('Employee name: ' || item.complete_name);

END LOOP; END;

/

Restrictions on Declarations

PL/SQL does not allow forward references. You must declare a variable or constant before referencing it in other statements, including other declarative statements.

PL/SQL does allow the forward declaration of subprograms. For more information, see "Declaring Nested PL/SQL Subprograms" on page 8-5.

Some languages allow you to declare a list of variables that have the same datatype. PL/SQL does not allow this. You must declare each variable separately:

DECLARE

--Multiple declarations not allowed.

--i, j, k, l SMALLINT;

--Instead, declare each separately.

iSMALLINT;

jSMALLINT;

--To save space, you can declare more than one on a line.

kSMALLINT; l SMALLINT;

BEGIN NULL;

END;

/

PL/SQL Naming Conventions

The same naming conventions apply to all PL/SQL program items and units including constants, variables, cursors, cursor variables, exceptions, procedures, functions, and packages. Names can be simple, qualified, remote, or both qualified and remote. For example, you might use the procedure name raise_salary in any of the following ways:

raise_salary(...);

 

-- simple

emp_actions.raise_salary(...);

-- qualified

raise_salary@newyork(...

);

-- remote

emp_actions.raise_salary@newyork(...

); -- qualified and remote

In the first case, you simply use the procedure name. In the second case, you must qualify the name using dot notation because the procedure is stored in a package called emp_actions. In the third case, using the remote access indicator (@), you reference the database link newyork because the procedure is stored in a remote database. In the fourth case, you qualify the procedure name and reference a database link.

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

PL/SQL Naming Conventions

Synonyms

You can create synonyms to provide location transparency for remote schema objects such as tables, sequences, views, standalone subprograms, packages, and object types. However, you cannot create synonyms for items declared within subprograms or packages. That includes constants, variables, cursors, cursor variables, exceptions, and packaged subprograms.

Scoping

Within the same scope, all declared identifiers must be unique; even if their datatypes differ, variables and parameters cannot share the same name. In the following example, the second declaration is not allowed:

DECLARE

valid_id BOOLEAN;

valid_id VARCHAR2(5); -- not allowed, duplicate identifier BEGIN

--The error occurs when the identifier is referenced, not

--in the declaration part. valid_id := FALSE;

END;

/

For the scoping rules that apply to identifiers, see "Scope and Visibility of PL/SQL Identifiers" on page 2-14.

Case Sensitivity

Like all identifiers, the names of constants, variables, and parameters are not case sensitive. For instance, PL/SQL considers the following names to be the same:

DECLARE

zip_code INTEGER;

Zip_Code INTEGER; -- duplicate identifier, despite Z/z case difference BEGIN

zip_code := 90120; -- causes error because of duplicate identifiers END;

/

Name Resolution

In potentially ambiguous SQL statements, the names of database columns take precedence over the names of local variables and formal parameters. For example, if a variable and a column with the same name are both used in a WHERE clause, SQL considers that both cases refer to the column.

To avoid ambiguity, add a prefix to the names of local variables and formal parameters, or use a block label to qualify references.

CREATE TABLE employees2 AS SELECT last_name FROM employees;

<<MAIN>> DECLARE

last_name VARCHAR2(10) := 'King'; my_last_name VARCHAR2(10) := 'King';

BEGIN

--Deletes everyone, because both LAST_NAMEs refer to the column DELETE FROM employees2 WHERE last_name = last_name; dbms_output.put_line('Deleted ' || SQL%ROWCOUNT || ' rows.'); ROLLBACK;

Fundamentals of the PL/SQL Language 2-13

Scope and Visibility of PL/SQL Identifiers

--OK, column and variable have different names

DELETE FROM employees2 WHERE last_name = my_last_name; dbms_output.put_line('Deleted ' || SQL%ROWCOUNT || ' rows.'); ROLLBACK;

--OK, block name specifies that 2nd LAST_NAME is a variable

DELETE FROM employees2 WHERE last_name = main.last_name; dbms_output.put_line('Deleted ' || SQL%ROWCOUNT || ' rows.'); ROLLBACK;

END;

/

DROP TABLE employees2;

The next example shows that you can use a subprogram name to qualify references to local variables and formal parameters:

DECLARE

FUNCTION dept_name (department_id IN NUMBER) RETURN departments.department_name%TYPE

IS

department_name departments.department_name%TYPE; BEGIN

--DEPT_NAME.DEPARTMENT_NAME specifies the local variable

--instead of the table column

SELECT department_name INTO dept_name.department_name

FROM departments

WHERE department_id = dept_name.department_id;

RETURN department_name;

END;

BEGIN

FOR item IN (SELECT department_id FROM departments) LOOP

dbms_output.put_line('Department: ' || dept_name(item.department_id)); END LOOP;

END;

/

For a full discussion of name resolution, see Appendix D.

Scope and Visibility of PL/SQL Identifiers

References to an identifier are resolved according to its scope and visibility. The scope of an identifier is that region of a program unit (block, subprogram, or package) from which you can reference the identifier. An identifier is visible only in the regions from which you can reference the identifier using an unqualified name. Figure 2–1 shows the scope and visibility of a variable named x, which is declared in an enclosing block, then redeclared in a sub-block.

Identifiers declared in a PL/SQL block are considered local to that block and global to all its sub-blocks. If a global identifier is redeclared in a sub-block, both identifiers remain in scope. Within the sub-block, however, only the local identifier is visible because you must use a qualified name to reference the global identifier.

Although you cannot declare an identifier twice in the same block, you can declare the same identifier in two different blocks. The two items represented by the identifier are distinct, and any change in one does not affect the other. However, a block cannot reference identifiers declared in other blocks at the same level because those identifiers are neither local nor global to the block.

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

Scope and Visibility of PL/SQL Identifiers

Figure 2–1 Scope and Visibility

 

Scope

Visibility

 

DECLARE

DECLARE

 

 

X REAL;

 

X REAL;

 

 

 

 

 

 

 

 

BEGIN

BEGIN

 

 

...

...

 

 

 

DECLARE

 

DECLARE

 

 

 

 

X REAL;

 

 

 

 

Outer x

 

 

 

 

X REAL;

 

BEGIN

 

BEGIN

 

 

 

 

...

...

 

 

 

END;

 

END;

 

...

 

 

 

 

 

...

 

 

END;

END;

 

 

 

 

 

 

 

 

 

 

DECLARE

DECLARE

 

 

X REAL;

 

X REAL;

 

BEGIN

BEGIN

 

...

...

 

Inner x

 

DECLARE

 

DECLARE

 

 

X REAL;

 

 

X REAL;

 

 

 

 

 

 

 

BEGIN

 

 

 

 

 

 

 

BEGIN

 

 

...

...

 

 

 

END;

 

END;

 

 

 

 

 

 

 

 

 

 

...

...

 

 

END;

END;

The example below illustrates the scope rules. Notice that the identifiers declared in one sub-block cannot be referenced in the other sub-block. That is because a block cannot reference identifiers declared in other blocks nested at the same level.

DECLARE

aCHAR;

bREAL;

BEGIN

-- identifiers available here: a (CHAR), b DECLARE

a INTEGER; c REAL;

BEGIN

--identifiers available here: a (INTEGER), b, c

END; DECLARE

d REAL; BEGIN

--identifiers available here: a (CHAR), b, d

END;

-- identifiers available here: a (CHAR), b

END;

/

Recall that global identifiers can be redeclared in a sub-block, in which case the local declaration prevails and the sub-block cannot reference the global identifier unless you use a qualified name. The qualifier can be the label of an enclosing block:

<<outer>> DECLARE

birthdate DATE; BEGIN

DECLARE

birthdate DATE;

Fundamentals of the PL/SQL Language 2-15

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

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