informix
Informix Guide to SQL: Syntax
SPL Statements

FOREACH

Use a FOREACH loop to select and manipulate more than one row.

Syntax

Element Purpose Restrictions Syntax
cursor Identifier that you supply as a name for the FOREACH loop Each cursor name within a routine must be unique. Identifier, p. 4-205
data_var Name of an SPL variable in the calling SPL routine that will receive the value or values the called function returns The data type of data_var must be appropriate for the value that is being returned. Identifier, p. 4-205
function Name of the SPL function you want to execute The function must exist. Database Object Name, p. 4-50
procedure Name of the SPL procedure you want to execute The procedure must exist. Database Object Name, p. 4-50
SPL_var Name of an SPL variable in the calling SPL routine that contains the name of a routine to be executed The data type of SPL_var must be CHAR, VARCHAR, NCHAR, or NVARCHAR. Identifier, p. 4-205

Usage

A FOREACH loop is the procedural equivalent of using a cursor. When a FOREACH statement executes, the database server takes the following actions:

  1. It declares and implicitly opens a cursor.
  2. It obtains the first row from the query that is contained within the FOREACH loop, or it obtains the first set of values from the called routine.
  3. It assigns to each variable in the variable list the value of the corresponding value from the active set that the SELECT statement or the called routine creates.
  4. It executes the statement block.
  5. It fetches the next row from the SELECT statement or called routine on each iteration, and it repeats steps 3 and 4.
  6. It terminates the loop when it finds no more rows that satisfy the SELECT statement or called routine. It closes the implicit cursor when the loop terminates.

Because the statement block can contain additional FOREACH statements, cursors can be nested. No limit exists to the number of cursors that can be nested.

An SPL routine that returns more than one row, collection element, or set of values is called a cursor function. An SPL routine that returns only one row or value is a noncursor function.

The following SPL procedure illustrates FOREACH statements with a SELECT...INTO clause, with an explicitly named cursor, and with a procedure call:

A select cursor is closed when any of the following situations occur:

Using a SELECT...INTO Statement

As indicated in the diagram for FOREACH, not all clauses and options of the SELECT statement are available for you to use in a FOREACH statement.

The SELECT statement in the FOREACH statement must include the INTO clause. It can also include UNION and ORDER BY clauses, but it cannot use the INTO TEMP clause. For a complete description of SELECT syntax and usage, see SELECT.

The type and count of each variable in the variable list must match each value that the SELECT...INTO statement returns.

Using Hold Cursors

The WITH HOLD keyword specifies that the cursor should remain open when a transaction closes (is committed or rolled back).

Updating or Deleting Rows Identified by Cursor Name

Use the WHERE CURRENT OF cursor clause to update or delete the current row of cursor.

Using Collection Variables

The FOREACH statement allows you to declare a cursor for an SPL collection variable. Such a cursor is called a collection cursor. You use a collection variable to access the elements of a collection (SET, MULTISET, LIST) column. Use a cursor when you want to access one or more elements in a collection variable.

Restrictions

When you use a collection cursor to fetch individual elements from a collection variable the FOREACH statement has the following restrictions:

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

Examples

The following excerpt from an SPL routine shows how to fill a collection variable and then how to use a cursor to access individual elements:

In this example, the SELECT statement selects one element at a time from the collection variable b into the element variable a. The select list is an asterisk, because the collection variable b contains a collection of built-in types. The variable b is used with the TABLE keyword as a Collection Derived Table. For more information, see Collection Derived Table.

The next example also shows how to fill a collection variable and then how to use a cursor to access individual elements. This example, however, uses a list of row type fields in its select list.

In this example, the collection variable employees contains a collection of row types. Each row type contains the fields name and salary. The collection query selects one name and salary combination at a time, placing name into n and salary into s. The AS keyword names e as an alias for the collection derived table employees. The alias exists as long as the SELECT statement executes.

Modifying Elements in a Collection Variable

To update an element of a collection, you must first declare a cursor with the FOREACH statement. Then, within the FOREACH loop, select elements one at a time from the collection variable, using the collection variable as a collection derived table in a SELECT query.

When the cursor is positioned on the element to be updated, you can use the WHERE CURRENT OF clause, as follows:

Calling a UDR in the FOREACH Loop

In general, use the following guidelines for calling another UDR from an SPL routine:

In Enterprise Decision Server, you must use EXECUTE PROCEDURE. Enterprise Decision Server does not support the EXECUTE FUNCTION statement.

In Dynamic Server, if you use EXECUTE PROCEDURE, the database server looks first for a user-defined procedure of the name you specify. If it finds the procedure, the server executes it. If it does not find the procedure, it looks for a user-defined function of the same name to execute. If the database server finds neither a function nor a procedure, it issues an error message.

If you use EXECUTE FUNCTION, the database server looks for a user-defined function of the name you specify. If it does not find a function of that name, the server issues an error message.

A called SPL function can return zero (0) or more values or rows.

The type and count of each variable in the variable list must match each value that the function returns.


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