确定静态滚动光标结果表中的行数

您可以确定INSENSITIVE或SENSITIVE STATIC滚动光标结果表中的行数。

过程

要确定静态滚动光标结果表中的行数,请按以下步骤操作:

  1. 执行FETCH语句,例如FETCH AFTER,将光标定位在最后一行之后。
  2. 请执行下列其中一项操作:
    • 在SQLCA中检索字段SQLERRD(1)和SQLERRD(2)的值(C和C++中的字段为sqlerrd[0]和sqlerrd[1])。SQLERRD(1)和SQLERRD(2)共同构成一个双字值,其中包含结果表中的行数。
    • 发出获取诊断信息请求,以获取 DB2_NUMBER_ROWS 项目的数值。

示例

以下C语言代码演示了如何获取敏感静态游标结果表中的行数。

EXEC SQL INCLUDE SQLCA;
long int rowcount; 
EXEC SQL                                            
 DECLARE SENSTAT SENSITIVE STATIC SCROLL CURSOR FOR  
 SELECT * FROM EMP;
EXEC SQL OPEN SENSTAT;
if (SQLCODE==0) {
 EXEC SQL FETCH AFTER SENSTAT; /* Position the cursor after the end */
                               /* of the result table               */ 
 if (SQLCODE==0) {
  /************************************/ 
  /* Get the row count from the SQLCA */
  /************************************/
  printf("%s \n","Row count from SQLCA: ");
  printf("%s %d\n","SQLERRD1: High-order word: ",sqlca.sqlerrd[0]);
                              /* Get the high-order word of the     */
                              /* result table size                  */
  printf("%s %d\n","SQLERRD2: Low-order word: ",sqlca.sqlerrd[1]);
                              /* Get the low-order word of the      */
                              /* result table size                  */
  /******************************************/ 
  /* Get the row count from GET DIAGNOSTICS */
  /******************************************/
  EXEC SQL GET DIAGNOSTICS :rowcount = DB2_NUMBER_ROWS;
  if (SQLCODE==0) {
   printf("%s %d\n","Row count from GET DIAGNOSTICS: ",rowcount);
  }
 }
}