1

我想将游标和数据结构传递给一个过程,然后该过程用 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

我如何传递光标,我将如何定义数据结构参数?

4

2 回答 2

1

我不知道您是否在其他网站上收到了答案,但其他人可能需要它。

游标“全局”存在但仅在声明它们的模块中,您不需要将其传递给此模块中的过程,您声明的游标始终可用,直到它关闭或作业结束。

您可以执行以下操作(仅当您在同一模块中时):

 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 是基于的,NULLindicator 数组也是如此。

所有 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 

希望这可以帮助某人。

于 2011-12-22T11:35:21.967 回答
0

我不确定您是否可以动态定义光标。问这个问题的好地方是RPG400-L。该列表中有 RPG 编译器团队的成员经常回答此类问题。

于 2011-06-15T13:25:48.653 回答