Select values from a nested collection

To select values from a nested collection, you must declare a collection variable and a select cursor for each level of collection.

The following code fragment uses the nested collection variable, nested_coll and the collection variable list_coll to select the lowest-level elements in the nested-collection column, setlist_col.
EXEC SQL BEGIN DECLARE SECTION;
   client collection set(list(integer not null) not null) nested_coll;
   client collection list(integer not null) list_coll;
   int an_element;
EXEC SQL END DECLARE SECTION;
int num_elements = 1;
int an_int;
int keep_fetching = 1;
⋮

EXEC SQL allocate collection :nested_coll;
EXEC SQL allocate collection :list_coll;

/* Step 1: declare the select cursor on the SET collection variable */
EXEC SQL declare set_curs2 cursor for 
   select * from table(:nested_coll);

/* Step 2: declare the select cursor on the LIST collection variable */
EXEC SQL declare list_curs2 cursor for
   select * from table(:list_coll);

/* Step 3: open the SET cursor */
EXEC SQL open set_curs2;

while (keep_fetching)
   {

/* Step 4: fetch the SET elements into the SET insert cursor */
   EXEC SQL fetch set_curs2 into :list_coll;

/* Open the LIST cursor */
   EXEC SQL open list_curs2;

/* Step 5: put the LIST elements into the LIST insert cursor */
   for (an_int=0; an_int<10; an_int++)
      {
      EXEC SQL fetch list_curs2 into :an_element;
      
⋮

      };
   EXEC SQL close list_curs2;
   num_elements++;

   if (done_fetching(num_elements))
      {
      EXEC SQL close set_curs2;
      keep_fetching = 0;
      }
   };
EXEC SQL free set_curs2;
EXEC SQL free list_curs2;

EXEC SQL deallocate collection :nested_coll;
EXEC SQL deallocate collection :list_coll;: