![]() |
|
Use a FOREACH loop to select and manipulate more than one row.
A FOREACH loop is the procedural equivalent of using a cursor. When a FOREACH statement executes, the database server takes the following actions:
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:
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.
The WITH HOLD keyword specifies that the cursor should remain open when a transaction closes (is committed or rolled back).
Use the WHERE CURRENT OF cursor clause to update or delete the current row of cursor.
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.
RestrictionsWhen 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:
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 VariableTo 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:
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.