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

11.2.4 Making Sense of Collections

We have noticed over the years that relatively few developers know about and use collections. This always comes as a surprise, because we have found them to be so handy. One challenge is that collections are relatively complicated. Three different types of collections, multiple steps involved in defining and using them, usage in both PL/SQL programs and database objects, more complex syntax than simply working with individual variables: all of these factors conspire to limit usage of collections.

We have organized this chapter so that we can be comprehensive in our treatment of collections, avoid redundancy in treatment of similar topics across different collection types, and offer guidance in your usage of collections. The resulting chapter is rather long. Here is a quick guide to the remainder of its contents:

Declaring collection types and collections

First, we start by showing you how to declare different types (or templates) of collections, along with the syntax to instantiate specific collections from those types.

Collection built-ins

Next, we explore the many built-in functions or methods that Oracle provides to help you examine and manipulate the contents of a collection.

Working with collections

Now it is time to build on all those "preliminaries" to explore some of the nuances of working with collections, including the initialization process necessary for nested tables and VARRAYs, different ways to populate and access collection data, and so on.

We then finish up the chapter with (a) a look at collection "pseudo-functions" designed to let us manipulate collections as relational tables, and vice versa; and (b) some details on how to maintain collections in the database and choose the most appropriate collection type.

11.3 Declaring Collection Types and Collections

Before you can work with a collection, you must declare it. When you declare a collection, it must be based on a collection type. So the first thing you must learn to do is define a collection type.

There are two different ways of creating user-defined collection types:

  • You can define a nested table type or VARRAY type "in the database" using the CREATE TYPE command. This makes the datatype available to use for a variety of purposes: columns in database tables, variables in PL/SQL programs, and attributes of object types.

  • You can declare the collection type within a PL/SQL program usingTYPE . . . IS . . . syntax. This collection type will then be available only for use within the block in which the TYPE is defined.

11.3.1 Declaring an Associative Array

As with a record, an associative array is declared in two stages:

  1. Define a particular associative array structure (made up of strings, dates, etc.) using the TYPE statement. The result of this statement is a datatype you can use in declaration statements.

  2. Declare the actual collection based on that table type. The declaration of an associative array is a specific instance of a generic datatype.

11.3.1.1 Defining the table type

The TYPE statement for an associative array has the following format:

TYPE table_type_name IS TABLE OF datatype [ NOT NULL ]

INDEX BY [ BINARY_INTEGER | subtype of BINARY_INTEGER | VARCHAR2 (size_limit)];

where table_type_name is the name of the table structure you are creating, and datatype is the datatype of the single column in the table. You can optionally specify that the table be NOT NULL, meaning that every row in the table must have a value.

Prior to Oracle9i Release 2, the only way you could specify an index for an associative array (a.k.a., index-by table) was:

INDEX BY BINARY_INTEGER

With Oracle9i Release 2, you can now also specify:

INDEX BY VARCHAR2 (size_limit)

The rules for the table type name are the same as for any identifier in PL/SQL: the name can be up to 30 characters in length, it must start with a letter, and it can include some special characters such as underscore (_) and dollar sign ($). (This feature is explored later in Section 11.6.8.)

The datatype of the table type's column can be any of the following:

Scalar datatype

Any PL/SQL-supported scalar datatype, such as VARCHAR2, CLOB, POSITIVE, DATE, or BOOLEAN.

Anchored datatype

A datatype inferred from a column, previously defined variable, or cursor expression using the %TYPE attribute.

Here are some examples of associative array type declarations:

TYPE company_keys_tabtype IS TABLE OF company.company_id%TYPE NOT NULL

INDEX BY BINARY_INTEGER;

TYPE booklist_tabtype IS TABLE OF books%ROWTYPE

INDEX BY BINARY_INTEGER;

TYPE books_by_author_tabtype IS TABLE OF books%ROWTYPE

INDEX BY VARCHAR2(100);

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