forEach

EGL 关键字 forEach 标记在循环中运行的一组语句的开始。仅当指定的结果集可用时才会发生第一次迭代。(如果结果集不可用,语句将会因为产生硬错误而失败。)循环读取结果集中的每一行并持续至发生下列其中一个事件:

forEach 语法图
sqlRecord
在先前运行的 open 语句中使用的 SQL 记录的名称。必须指定 SQL 记录或结果集标识,建议您指定结果集标识。
from resultSetID
在先前运行的 open 语句中使用的结果集标识。有关详细信息,请参阅 resultSetID
into ... item
INTO 子句,它标识从游标或存储过程中接收值的 EGL 主变量。在与此子句类似的子句中(该子句位于 #sql{ } 块外部),不要在主变量名称之前包括分号。

此上下文中的 INTO 子句的规范将覆盖相关 open 语句中标识的任何 INTO 子句。

statement
以 EGL 语言表示的语句

在大多数情况下,EGL 运行时会发出隐式的 close 语句,它会在 forEach 语句的最后一次迭代后发生。这一隐式语句会修改 SQL 系统变量,因此您可能想要将与 SQL 有关的系统变量的值保存在 forEach 语句的主体中。

如果 forEach 语句因为 noRecordFound 之外的错误而终止,所以 EGL 运行时不会发出隐式 close 语句。

下面是一个示例,更详细的描述可在 SQL 示例中找到:
  VGVar.handleHardIOErrors  = 1;

          try
    open selectEmp
      with #sql{
        select empnum, empname
        from employee
        where empnum >= :empnum
        for update of empname
      }
      into empnum, empname;
  onException
    myErrorHandler(6);    // exits program
  end
    
          try
    forEach (from selectEmp)
      empname = empname + " " + "III";
    
              try
        execute
          #sql{
            update employee
            set empname = :empname
          where current of selectEmp
          };
      onException
        myErrorHandler(10); // exits program
      end
        end  // end forEach; cursor is closed automatically
         // when the last row in the result set is read

  onException
    // the exception block related to forEach is not run if the condition 
    // is "sqlcode = 100", so avoid the test "if (sqlcode != 100)"
    myErrorHandler(8);  // exits program
  end
    
  sysLib.commit();

相关概念
resultSetID
SQL 支持

相关任务
EGL 语句和命令的语法图

相关参考
EGL 语句
exit
open
SQL 示例

使用条款 | 反馈
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.