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

11.6.1 Initializing Collection Variables

With an associative array datatype, initialization is a nonissue. Simply declaring an associative array variable initializes it in an "empty" state. Then you can just assign values to subscripted table elements as you desire. Index values (subscripts) can be almost any positive or negative integer. A program can even assign subscripts to associative arrays arbitrarily, skipping huge ranges of subscripts without paying a memory or performance penalty.[2] So if you are working with associative arrays, you can skip to the next section on assigning elements to a collection.

[2] This sparseness makes it possible to use an associative array as an in-memory representation of almost any database table that uses an integer primary key.

The allocation scheme for nested tables and VARRAYs is different from that of associative arrays. First of all, if you don't initialize a collection of those types, it will be atomically null, and any attempt to read or write an element of an atomically null collection will generate a runtime error. For example:

DECLARE

/* The variable cool_colors is not initialized in its

|| declaration; it is "atomically null."

*/

cool_colors Color_tab_t;

BEGIN

IF cool_colors IS NULL THEN -- valid; will be TRUE

...

IF cool_colors(1) IS NULL THEN -- exception raised

...

cool_colors(1) := 'BLUE'; -- exception raised

You must initialize the collection before using it. There are three ways you can initialize a collection:

  • Explicitly, using a constructor

  • Implicitly, via a direct assignment of another collection variable

  • Implicitly, via a fetch from the database

There is no requirement that you initialize any particular number of elements in a collection. Zero, one, or more are fine, and you can always add more values later. In particular, don't be confused byVARRAYs. Just because you specify a limit on the number of elements it can hold does not imply that you have to put that many elements in when you initialize it.

11.6.1.1 Initializing explicitly with a constructor

Earlier, we saw declarations that looked like this:

my_favorite_colors Color_tab_t := Color_tab_t('PURPLE', 'GREEN');

my_favorite_numbers Number_t := Number_t(42, 65536);

Color_tab_t( ) is the constructor function supplied by Oracle when we created the Color_tab_t collection type. This function accepts an arbitrary number of arguments, as long as each argument is of the "proper" datatype—which in this case is VARCHAR2(30), because our original type definition statement was the following:

CREATE TYPE Color_tab_t AS TABLE OF VARCHAR2(30);

At initialization, Oracle allocates to the variable an amount of memory necessary to hold the values you supply as arguments. Initialization both creates and populates the "slots" for the elements.

So, if I want to fix the earlier invalid example, I can simply initialize the variable:

DECLARE

cool_colors Color_tab_t := Color_tab_t('VIOLET'); -- initialize

BEGIN

IF cool_colors(1) IS NULL THEN -- This is OK now!

What do you suppose Oracle does with the following initialization?

working_colors Color_tab_t := Color_tab_t( );

This is a way of creating an empty collection. "Empty" is a kind of enigmatic state in which the collection is not atomically null but still has no data. Whenever you create such an empty collection, you'll need to "extend" the collection variable later when you want to put elements into it. (See the discussion of the EXTEND method earlier in this chapter.)

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