使用RPG变量作为游标名称来执行SQL语句

问题描述

|| 我想将游标和数据结构传递给过程,然后该过程用sql游标的下一行填充该结构。这可能吗?下面是我要达到的目标的模板。
  *****************************************************
  *
  * Fetch the next row from a cursor
  *
  * @param cursor - the name of the cursor
  * @param structure - the data structure to hold the fields
  *****************************************************
 pfetchNextRow     B
 DfetchNextRow     PI              N
 d cursor                     32767A   varying const
 d structure       DS                  ???????               
  /free 
     exec sql
       fetch next from :cursor  into :structure 
     ;

     if (sqlstate = sql_SUCCESS);
       return *on;
     else;
       exec sql
         close :cursor;
       return *off;
     endif;

  /end-free
 pfetchNextRow     E
我如何传递游标,以及如何定义数据结构参数?     

解决方法

        我不知道您是否在其他站点上收到了答案,但是其他站点可能需要它。 游标仅在声明它们的模块中存在“全局”,而您无需将其传递给该模块中的过程,所声明的游标在关闭或关闭之前始终可用。工作结束。 您可以执行以下操作(仅当您位于“同一个模块”中时):
 P SQLprep_mC      B                   EXPORT
 D SQLprep_mc      PI                  Like(g_retCode)
 D strInSQL                            Like(string_MAX_V)

  /Free
   exec SQL
     SET OPTION
         CLOSQLCSR = *ENDACTGRP;
   exec sql prepare p1 from :strINsql ;
   //.... declare and open
  /End-Free

 P SQLprep_mC      E  



 P SQLfetch_mC_st  B                   EXPORT
 D SQLfetch_mC_st  PI                  Like(g_retCode)
 D    Row                              LikeDs(dsSql_0)
 D    NullI                            Like(sqlNI_0) Dim(DSSql0_nFields)

 D Rowmc_b         DS                  LikeDs(dsSql_0) Based(pNull1)
 D Rowmc           DS                  LikeDs(dsSql_0)
 D pNUll1          s               *   inz(%ADDR(Rowmc))
 D SQLind          s                   Like(sqlNI_0) Based(pNull2)
 D NullImc         S                   like(SQLind) Dim(DSSql_nFields)
 D pNull2          s               *   inz(%ADDR(NullImc))

  /free
     exec SQL fetch next from mC into :Rowmc :NullImc ;
     Row   = Rowmc   ;
     NullI = NullImc ; 
  /end-free

 P SQLfetch_mC_st  E 
这两个过程使用相同的光标\“ mC \”,并且位于同一模块中。 第一个准备,声明并打开游标,第二个获取RowMC中的行。 如您所见,用于提取的DS是BASED,NULLindicator Array也是如此。 在复制文件中,所有Like和LikeDS参数都定义为TEMPLATE,例如:
 D comfraf       e DS                  extname(comfra00f) QUALIFIED TEMPLATE
 D dsSql_0         DS                  Qualified  TEMPLATE
 D  cid                                like(comfraf.cid      )            
 D  ccap                               like(comfraf.ccap     )            

 D sqlNI_0         s              5I 0 TEMPLATE 
希望这可以帮助某人。     ,        我不确定您是否可以动态定义光标。在RPG400-L上问这个问题的好地方。该列表中有RPG编译器团队的成员,他们经常回答此类问题。