Enterprise Edition Home | Express Edition Home | Previous Page | Next Page   SQL Statements > DECLARE >

Select with a Collection-Derived Table

The diagram for DECLARE refers to this section.

To declare a select cursor for a collection variable, include the Collection- Derived Table segment with the SELECT statement that you associate with the collection cursor. A select cursor allows you to select one or more elements from the collection variable. (For a description of SELECT syntax and usage, see SELECT.)

When you declare a select cursor for a collection variable, the DECLARE statement has the following restrictions:

In addition, the SELECT statement that you associate with the collection cursor has the following restrictions:

Using a SELECT Cursor with a Collection Variable

A collection cursor that includes a SELECT statement with the Collection- Derived Table clause provides access to the elements in a collection variable.

To select more than one element
  1. Create a client collection variable in your ESQL/C program.
  2. Declare the collection cursor for the SELECT statement with the DECLARE statement.

    To modify elements of the collection variable, declare the select cursor as an update cursor with the FOR UPDATE keywords. You can then use the WHERE CURRENT OF clause of the DELETE and UPDATE statements to delete or update elements of the collection.

  3. Open this cursor with the OPEN statement.
  4. Fetch the elements from the collection cursor with the FETCH statement and the INTO clause.
  5. If necessary, perform any updates or deletes on the fetched data and save the modified collection variable in the collection column.

    Once the collection variable contains the correct elements, use the UPDATE or INSERT statement to save the contents of the collection variable in the actual collection column (SET, MULTISET, or LIST).

  6. Close the collection cursor with the CLOSE statement.

This DECLARE statement declares a select cursor for a collection variable:

EXEC SQL BEGIN DECLARE SECTION;
   client collection set(integer not null) a_set;
EXEC SQL END DECLARE SECTION;
...
EXEC SQL declare set_curs cursor for select * from table(:a_set);

For an extended code example that uses a collection cursor for a SELECT statement, see Fetching from a Collection Cursor (IDS).

Enterprise Edition Home | Express Edition Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]