![]() |
|
Use the Collection Derived Table segment to:
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.
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.
When you uses the collection-expression format, certain restrictions apply:
In addition to the restrictions outlined in the previous section, the following restrictions apply when you use the collection-expression format with ESQL/C.
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.
The following example uses a collection expression to create a collection derived table:
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:
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:
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/CSuppose 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 CollectionSuppose 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.
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 CollectionSuppose 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.
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.
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 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.