INFORMIX
Informix Guide to SQL: Tutorial
Chapter 13: Casting Data Types
Home Contents Index Master Index New Book

Casting Collection Data Types

In some cases, you can use an explicit cast to convert from one collection type to another collection type. To compare or substitute between values of any two collection types, one of the following conditions must be true:

    Two element types are equivalent when all component types are the same. For example, if the element type of one collection is a row type, the other collection type is also a row type with the same number of fields and the same field data types.

    If the corresponding element types are not of the same data type, Universal Server can use either system-defined casts or user-defined casts to handle data conversions on the element types.

When the database server inserts, updates, or compares values of a collection data type, type checking occurs at the level of the element data type. Consequently, in a cast between two collection types, the data conversion occurs at the level of the element type because the actual data stored in a collection is of a particular element type.

Suppose you create the types and tables that Figure 13-7 shows. These types and tables are used in the collection casting examples that follow.

Figure 13-7

Converting Between Collection Types with the Same Element Type

When the element type of two collections is the same but the collection types differ, you can insert or update elements from one collection with elements from the other collection without an explicit cast. The following INSERT statement retrieves elements from the list_tab table and inserts the elements into the m_set_tab table. Although one collection is a MULTISET and the other collection is a LIST, no explicit cast is necessary because the element types of the two collection types are the same (both are of the INT element type).

Because each collection data type (SET, MULTISET, and LIST) has different characteristics, elements retrieved from one collection type and inserted into another collection type are represented differently. For example, elements stored in a LIST collection have a specific order associated with them. This order is lost when these same elements are inserted into a MULTISET collection.

Converting Between Collections with Different Element Types

How you handle conversions between two collections depends on the element type of each collection and the type of cast that the database server uses to convert one element type to another when the element types are different:

When the Conversion Between Element Types Requires an Implicit Cast

When an implicit cast exists in the database to convert between different element types of two collections, you do not need to use an explicit cast to insert or update elements from one collection type into the other collection type. The following INSERT statement retrieves elements from the set_tab2 table and inserts the elements into the set_tab3 table. Although the collection column from set_tab2 has an INT element type and the collection column from set_tab3 has a FLOAT element type, a system-defined cast implicitly handles the conversion between INT and FLOAT values. An explicit cast is unnecessary in this case.

When the Conversion Between Element Types Requires an Explicit Cast

When a conversion between different element types of two collections is performed with an explicit cast, you must explicitly cast one collection to the other collection type. In the following example, the conversion between the element types (INT and my_int) requires an explicit cast. (A cast between a distinct type and its source type is always explicit).

The following INSERT statement retrieves elements from the set_tab2 table and inserts the elements into the set_tab1 table. The collection column from set_tab2 has an INT element type and the collection column from set_tab1 has a my_int element type. Because the conversion between the element types (INT and my_int) requires an explicit cast, you must explicitly cast the collection type.

To perform an explicit cast on a collection type, you must include the constructor (SET, MULTISET, or LIST), the element type, and the NOT NULL keyword.

The following INSERT statement retrieves elements from the m_set_tab table and inserts the elements into the set_tab1. The explicit cast is necessary because a conversion between the INT and my_int element types requires an explicit cast. This example differs from the previous example in that here a MULTISET collection is explicitly cast as a SET collection.




Informix Guide to SQL: Tutorial, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.