ForEach statement

The forEach statement defines a loop that runs once for each element in a list or result set.

In the general use of the forEach statement, a loop iteration retrieves the next element in a list and operates on that element.

In the usage that is specific to SQL, an iteration retrieves the next row in an SQL result set and operates on that row. After processing all rows, the forEach statement closes the result set. If an exception occurs, the result set stays open.

Syntax



Syntax diagram for the forEach statement with SQL

entity
Name of the SQL entity, which is a variable based on an External type, Handler type, or Record type.
simpleVar
A field of an EGL simple type that is compatible with the corresponding column in the result set.
SQLResultSet
An SQL result set that was established by an open statement.
varDeclaration
A variable declaration, including the name and type. The type must be compatible with the type of each list element.
list
A list.
statement
An EGL statement.
Assume that the following Record type is available:
Record myRecordType 
  myString String;
  myInt Int;
end

The following code illustrates the general use of the forEach statement:

myList string[];
myList.appendElement("one");
myList.appendElement("two");
myList.appendElement("three");

myList02 int[];
myList02.appendElement(1);
myList02.appendElement(2);
myList02.appendElement(3);

myList03 MyRecordType[];
myList03.appendElement(new MyRecordType{myString = "01", myInt = 1});
myList03.appendElement(new MyRecordType{myString = "02", myInt = 2});
myList03.appendElement(new MyRecordType{myString = "03", myInt = 3});        

forEach(myElement string from myList)
   SysLib.writeStdOut(myElement);
end

forEach(myElement02 int from myList02)
   SysLib.writeStdOut(myElement02);
end
        
forEach (myElement03 MyRecordType from myList03)
   SysLib.writeStdOut(myElement03.myString + " " + myElement03.myInt);
end
The output is as follows:
one
two
three
1
2
3
01 1
02 2
03 3

Compatibility

Table 1. Compatibility
Target Issue
Java No issues.
JavaScript Database access is not supported in JavaScript. However, the forEach statement is available for general use.