informix
Informix Guide to SQL: Syntax
Segments

Collection Derived Table

Use the Collection Derived Table segment to:

Syntax

Element Purpose Restrictions Syntax
alias Temporary name for a collection derived table within the scope of a SELECT statement If you do not assign an alias, the database server assigns an implementation-dependent alias name. If you use a potentially ambiguous word as an alias, you must precede the alias with the keyword AS. For further information on this restriction, see AS Keyword with Aliases. Identifier, p. 4-205
collection_expr Any expression that evaluates to a single collection For example, column references, scalar subquery, dotted expression, functions, operators (through overloading), collection subqueries, literal collections, collection constructors, cast functions, and so on. See Restrictions with the Collection-Expression Format. Expression, p. 4-73
collection_var Name of a typed or untyped collection variable that holds the collection derived table The variable must have been declared in an ESQL/C program or defined in an SPL routine. For more information, see the Informix ESQL/C Programmer's Manual or DEFINE, respectively. Name must conform to language-specific rules for variable names.
derived_column Temporary name for a derived column in a table If you do not assign a derived-column name, the behavior of the database server differs based on the data type of the elements in the underlying collection. For more information, see Row Type of the Resulting Collection Derived Table. If the underlying collection is not a row type, you can specify only one derived-column name. Identifier, p. 4-205
row_var Name of an ESQL/C row variable that holds the collection derived table The variable must be declared as row variable in an ESQL/C program. For more information, see the Informix ESQL/C Programmer's Manual. Name must conform to language-specific rules for variable names.

Usage

A collection derived table is a virtual table in which the values in the rows of the table are equivalent to the elements of a collection. In other words, a collection derived table appears as a table in an SQL statement.

The TABLE keyword table converts a collection into a virtual table. You can use the collection expression format to query a collection column or you can use the collection variable or row variable format to manipulate the data in a collection column.

Accessing a Collection Through a Virtual Table

When you use the collection expression format of the Collection Derived Table segment to access the elements of a collection, you can select elements of the collection directly through a virtual table. You can use this format only in the FROM clause of a SELECT statement. The FROM clause can be in either a query or a subquery.

With this format you can use joins, aggregates, WHERE clause, expressions, the ORDER BY clause and other operations not available to you when you use the collection-variable format. This format reduces the need for multiple cursors and temporary tables.

Restrictions with the Collection-Expression Format

When you uses the collection-expression format, certain restrictions apply:

Additional Restrictions that Apply to ESQL/C

In addition to the restrictions outlined in the previous section, the following restrictions apply when you use the collection-expression format with ESQL/C.

Row Type of the Resulting Collection Derived Table

Although a collection derived table appears to contain columns of individual data types, the resulting columns are, in fact, fields of a row type. The type of row type as well as the column name depends on several factors.

If the data type of the elements of the underlying collection expression is type, the database server determines the row type of the collection derived table by the following rules:

The extended examples shown in the following table illustrate these rules. The table uses the following schema for its examples.

Example of a Collection Expression

The following example uses a collection expression to create a collection derived table:

Accessing a Collection Through a Collection Variable

When you use the collection-variable format of the Collection Derived Table segment, you use a host or program variable to access and manipulate the elements of a collection.

This format allows you to modify the contents of a variable as you would a table in the database and then update the actual table with the contents of the collection variable.

You can use the collection-variable format (the TABLE keyword preceding a collection variable) in place of the name of a table, synonym, or view name in the following SQL statements:

Using a Collection Variable to Manipulate Collection Elements

When you use data manipulation statements (SELECT, INSERT, UPDATE or DELETE) in conjunction with a collection variable you can modify one or more elements in a collection.

To modify elements in a collection, follow these general steps:

  1. Create a collection variable in your SPL routine or ESQL/C program.
  2. For information on how to declare a collection variable in ESQL/C, see the Informix ESQL/C Programmer's Manual. For information on how to define a collection variable in SPL, see DEFINE.

  3. In ESQL/C, allocate memory for the collection; see ALLOCATE COLLECTION.
  4. Optionally, use a SELECT statement to select a collection column into the collection variable.
  5. If the collection variable is an untyped COLLECTION variable, you must perform a SELECT from the collection column before you use the variable in the Collection Derived Table segment. The SELECT statement allows the database server to obtain the collection type.

  6. Use the appropriate data manipulation statement with the Collection Derived-Table segment to add, delete, or update collection elements in the collection variable.
  7. To insert more than one element or to update or delete a particular element or in the collection, you must use a cursor for the collection variable.

  8. After the collection variable contains the correct elements, use an INSERT or UPDATE statement on the table or view that holds the actual collection column to save the changes that the collection variable holds.

The collection variable stores the elements of the collection. However, it has no intrinsic connection with a database column. Once the collection variable contains the correct elements, you must then save the variable into the actual collection column of the table with either an INSERT or an UPDATE statement.

Example of Deleting from a Collection in ESQL/C

Suppose that the set_col column of a row in the table1 table is defined as a SET and for one row contains the values {1,8,4,5,2}. The following ESQL/C code fragment uses an update cursor and a DELETE statement with a WHERE CURRENT OF clause to delete the element whose value is 4:

After the DELETE statement executes, this collection variable contains the elements {1,8,5,2}. The UPDATE statement at the end of this code fragment saves the modified collection into the set_col column of the database. Without this UPDATE statement, the collection column never has element 4 deleted.

For information on how to use collection-host variables in an ESQL/C program, see the discussion of complex data types in the Informix ESQL/C Programmer's Manual.

Example of Deleting from a Collection

Suppose that the set_col column of a row in the table1 table is defined as a SET and one row contains the values {1,8,4,5,2}. The following SPL code fragment uses a FOREACH loop and a DELETE statement with a WHERE CURRENT OF clause to delete the element whose value is 4:

This SPL routine defines two variables, a and b, each to hold a set of SMALLINT values. The first SELECT statement selects a SET column from one row of table1 into b. Then, the routine declares a cursor that selects one element at a time from b into a. When the cursor is positioned on the element with the value 4, the DELETE statement deletes that element from b. Last, the UPDATE statement updates the row of table1 with the new collection that is stored in b.

For information on how to use collection variables in an SPL routine, see the Informix Guide to SQL: Tutorial.

Example of Updating a Collection

Suppose that the set_col column of a table called table1 is defined as a SET and that it contains the values {1,8,4,5,2}. The following ESQL/C program changes the element whose value is 4 to a value of 10.

After you execute this ESQL/C program, the set_col column in table1 contains the values {1,8,10,5,2}.

This ESQL/C program defines two collection variables, a and b, and selects a SET from table1 into b. The WHERE clause ensures that only one row is returned. Then, the program defines a collection cursor, which selects elements one at a time from b into a. When the program locates the element with the value 4, the first UPDATE statement changes that element value to 10 and exits the loop.

In the first UPDATE statement, x is a derived column name used to update the current element in the collection derived table. The second UPDATE statement updates the base table table1 with the new collection. For information on how to use collection host variables in an ESQL/C program, see the discussion of complex data types in the Informix ESQL/C Programmer's Manual.

Example of Inserting a Value into a Multiset Collection

Suppose the ESQL/C host variable a_multiset has the following declaration:

The following INSERT statement adds a new MULTISET element of 142,323 to a_multiset:

When you insert elements into a client-collection variable, you cannot specify a SELECT statement or an EXECUTE FUNCTION statement in the VALUES clause of the INSERT. However, when you insert elements into a server-collection variable, the SELECT and EXECUTE FUNCTION statements are valid in the VALUES clause. For more information on client- and server-collection variables, see the Informix ESQL/C Programmer's Manual.

Accessing a Nested Collection

If the element of the collection is itself a complex type (collection or row type), the collection is a nested collection. For example, suppose the ESQL/C collection variable, a_set, is a nested collection that is defined as follows:

To access the elements (or fields) of a nested collection, use a collection or row variable that matches the element type (a_list and an_int in the preceding code fragment) and a select cursor.

Accessing a Row Variable

The TABLE keyword can make an ESQL/C row variable a collection derived table, that is, a row appears as a table in an SQL statement. For a row variable, you can think of the collection derived table as a table of one row, with each field of the row type being a column of the table row.

Use the TABLE keyword in place of the name of a table, synonym, or view name in the following SQL statements:

The DELETE and INSERT statements do not support a row variable in the Collection Derived Table segment.

For example, suppose an ESQL/C host variable a_row has the following declaration:

The following ESQL/C code fragment adds the fields in the a_row variable to the row_col column of the tab_row table:

Related Information

Related statements: DECLARE, DELETE, DESCRIBE, FETCH, INSERT, PUT, SELECT, UPDATE, DEFINE, and FOREACH

For information on how to use collection variables in an SPL routine, see the Informix Guide to SQL: Tutorial.

For information on how to use collection or row variables in an ESQL/C program, see the chapter on complex data types in the Informix ESQL/C Programmer's Manual.


Informix Guide to SQL: Syntax, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved