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

11.6.3 Referencing an Undefined Row

Rows in collections exist only if:

  • You assign a value to a row in an associative array. The mere act of assignment creates that row.

  • You explicitly extend a VARRAY or nested table, creating the row. Once created, you can assign a value to that row.

If you attempt to reference a row (either to set or read its value), PL/SQL will raise one of three exceptions, as illustrated by the following code:

CREATE TYPE strings_nt IS TABLE OF VARCHAR2 (100);

1 DECLARE

2 TYPE strings_ibt IS TABLE OF VARCHAR2 (100)

3 INDEX BY BINARY_INTEGER;

4

5 strings strings_nt := strings_nt ( );

6 ibt_strings strings_ibt;

7 BEGIN

8 BEGIN

9 IF ibt_strings(2) = 'a' THEN NULL; END IF;

10 EXCEPTION

11 WHEN OTHERS

12 THEN DBMS_OUTPUT.put_line ( 'a. ' || SQLERRM);

13 END;

14 BEGIN

15 strings.EXTEND; strings (2) := 'b';

16 EXCEPTION

17 WHEN OTHERS

18 THEN DBMS_OUTPUT.put_line ( 'b. ' || SQLERRM);

19 END;

20

21 BEGIN

22 strings (0) := 'c';

23 EXCEPTION

24 WHEN OTHERS

25 THEN DBMS_OUTPUT.put_line ( 'c. ' || SQLERRM);

26 END;

27 END;

Here is the output when this script is run:

a. ORA-01403: no data found

b. ORA-06533: Subscript beyond count

c. ORA-06532: Subscript outside of limit

The following table provides an explanation of the code.

Line

Description

9

I try to read the second row in an associative array that has no rows defined in it (it was just declared three lines previously). So PL/SQL raises the NO_DATA_FOUND exception.

15

I extend a single row, but then try to assign a value to the second row of a nested table. I have not, however, extended twice, so that subscript is invalid and "beyond count."

22

I try to assign a value to row 0 of the nested table, and that is a definite no-no. It is not a valid row number, either for nested tables or for VARRAYs (though it is fine for associative arrays).

11.6.4 Working with Collections of Composites

When Oracle first made collections available in PL/SQL 2 (corresponding to Oracle7), you were able to create associative arrays of scalar values: dates, numbers, strings, and so on. But that was about it, making collections (back then called "PL/SQL tables") awkward and of limited use.

But that was long ago. Now, depending on the version of Oracle you are using, collections support much more complex data structures:

Collections of records (Oracle 7.3.4 and above)

You can define and manipulate collections of records (as long as those records do not contain as fields other records or collections).

Collections of object type instances, etc. (Oracle8 and above)

You can define collections of object type instances (a.k.a. "objects"), records, LOBs, XML datatypes, and all other noncollection datatypes.

Collections of collections (Oracle9i and above)

You can define multilevel collections, including collections of collections and collections of datatypes that contain, as an attribute or a field, another collection.

Let's take a look at examples of each of these variations.

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