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

11.3.2.1 Examples of declaring nested tables and varraYs

Here are some examples of declaring nested tables in PL/SQL. First, I create a nested table type in the database:

CREATE OR REPLACE TYPE Color_tab_t AS TABLE OF VARCHAR2(30);

Next, I declare some PL/SQL variables. There is no reason you must use only types that you have created in the database. You can also declare them locally, or mix and match from both sources:

DECLARE

-- A variable that will hold a list of available font colors

font_colors Color_tab_t;

/* The next variable will later hold a temporary copy of

|| font_colors. Note that we can use %TYPE to refer to the

|| datatype of font_colors. This illustrates two different

|| ways of declaring variables of the Color_tab_t type.

*/

font_colors_save font_colors%TYPE;

-- Variable to hold a list of paint colors

paint_mixture Color_array_t;

/* As with Oracle7 index-by tables, you can define

|| a table datatype here within a declaration section...

*/

TYPE Number_t IS TABLE OF NUMBER;

/* ...and then you can use your new type in the declaration

|| of a local variable. The next line declares and initializes

|| in a single statement. Notice the use of the constructor,

|| Number_t(value, value, ...), to the right of the ":="

*/

my_favorite_numbers Number_t := Number_t(42, 65536);

/* Or you can just refer to the Color_tab_t datatype in the

|| data dictionary. This next line declares a local variable

|| my_favorite_colors to be a "nested" table and initializes it

|| with two initial elements using the default constructor.

*/

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

END;

This code also illustrates default constructors (special functions Oracle provides whenever you create a type) that serve to initialize and/or populate their respective types. A constructor has the same name as the type, and accepts as arguments a comma-separated list of elements. See Section 11.6.1 for more information.

11.4 Where Collections Can Be Used

The following sections describe the different places in your code where a collection can be declared and used. Because a collection type can be defined in the database itself (nested tables and VARRAYs only), you can find collections not only in PL/SQL programs, but also inside tables and object types.

11.4.1 Collections as Components of a Record

Using a collection type in a record is similar to using any other type. You can use VARRAYs, nested tables, associative arrays, or any combination thereof in RECORD datatypes. For example:

DECLARE

TYPE toy_rec_t IS RECORD (

manufacturer INTEGER,

shipping_weight_kg NUMBER,

domestic_colors Color_array_t,

international_colors Color_tab_t

);

RECORD types cannot live in the database; they are available only within PL/SQL programs. Logically, however, you can achieve a similar result by using object types in place of RECORD types. Briefly, object types can have a variety of attributes; you can include the two new collection types as attributes within objects; you can also define a collection whose elements are themselves objects. For a complete discussion of object types, see Chapter 21.

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