Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / PL_SQL / b14261.pdf
Скачиваний:
27
Добавлен:
12.05.2015
Размер:
4.36 Mб
Скачать

Initializing and Referencing Collections

Initializing and Referencing Collections

Until you initialize it, a nested table or varray is atomically null; the collection itself is null, not its elements. To initialize a nested table or varray, you use a constructor, a system-defined function with the same name as the collection type. This function constructs collections from the elements passed to it.

You must explicitly call a constructor for each varray and nested table variable. Associative arrays, the third kind of collection, do not use constructors. Constructor calls are allowed wherever function calls are allowed.

Example 5–10 initializes a nested table using a constructor, which looks like a function with the same name as the collection type:

Example 5–10 Constructor for a Nested Table

DECLARE

TYPE dnames_tab IS TABLE OF VARCHAR2(30); dept_names dnames_tab;

BEGIN

dept_names := dnames_tab('Shipping','Sales','Finance','Payroll'); END;

/

Because a nested table does not have a declared size, you can put as many elements in the constructor as necessary.

Example 5–11 initializes a varray using a constructor, which looks like a function with the same name as the collection type:

Example 5–11 Constructor for a Varray

DECLARE

--In the varray, we put an upper limit on the number of elements TYPE dnames_var IS VARRAY(20) OF VARCHAR2(30);

dept_names dnames_var; BEGIN

--Because dnames is declared as VARRAY(20), we can put up to 10

--elements in the constructor

dept_names := dnames_var('Shipping','Sales','Finance','Payroll');

END;

/

Unless you impose the NOT NULL constraint in the type declaration, you can pass null elements to a constructor as in Example 5–12.

Example 5–12 Collection Constructor Including Null Elements

DECLARE

TYPE dnames_tab IS TABLE OF VARCHAR2(30); dept_names dnames_tab;

TYPE dnamesNoNulls_type IS TABLE OF VARCHAR2(30) NOT NULL; BEGIN

dept_names := dnames_tab('Shipping', NULL,'Finance', NULL);

--If dept_names was of type dnamesNoNulls_type, we could not include

--null values in the constructor

END;

/

5-10 Oracle Database PL/SQL User’s Guide and Reference

Initializing and Referencing Collections

You can initialize a collection in its declaration, which is a good programming practice, as shown in Example 5–13. In this case, you can call the collection's EXTEND method to add elements later.

Example 5–13 Combining Collection Declaration and Constructor

DECLARE

TYPE dnames_tab IS TABLE OF VARCHAR2(30);

dept_names dnames_tab := dnames_tab('Shipping','Sales','Finance','Payroll'); BEGIN

NULL;

END;

/

If you call a constructor without arguments, you get an empty but non-null collection as shown in Example 5–14.

Example 5–14 Empty Varray Constructor

DECLARE

TYPE dnames_var IS VARRAY(20) OF VARCHAR2(30); dept_names dnames_var;

BEGIN

IF dept_names IS NULL THEN

DBMS_OUTPUT.PUT_LINE('Before initialization, the varray is null.');

--While the varray is null, we cannot check its COUNT attribute.

--DBMS_OUTPUT.PUT_LINE('It has ' || dept_names.COUNT || ' elements.'); ELSE

DBMS_OUTPUT.PUT_LINE('Before initialization, the varray is not null.'); END IF;

dept_names := dnames_var(); -- initialize empty varray IF dept_names IS NULL THEN

DBMS_OUTPUT.PUT_LINE('After initialization, the varray is null.'); ELSE

DBMS_OUTPUT.PUT_LINE('After initialization, the varray is not null.'); DBMS_OUTPUT.PUT_LINE('It has ' || dept_names.COUNT || ' elements.');

END IF; END;

/

Referencing Collection Elements

Every reference to an element includes a collection name and a subscript enclosed in parentheses. The subscript determines which element is processed. To reference an element, you specify its subscript using the syntax

collection_name(subscript)

where subscript is an expression that yields an integer in most cases, or a VARCHAR2 for associative arrays declared with strings as keys.

The allowed subscript ranges are:

For nested tables, 1 .. 2147483647 (the upper limit of PLS_INTEGER).

For varrays, 1 .. size_limit, where you specify the limit in the declaration (not to exceed 2147483647).

For associative arrays with a numeric key, -2147483648 to 2147483647.

Using PL/SQL Collections and Records 5-11

Соседние файлы в папке PL_SQL