OpnQryF and Random Access

Article ID: 15944
Q: I have a CL program that executes an OpnQryF statement to select specific records. Additionally, the OpnQryF statement uses parameter KeyFld to specify key fields for the resulting open data path (the file itself is not keyed). With RPG I can read the records sequentially, but how can the program perform random access (e.g., Chain, SetLL/Read)?

A: In order for an RPG program to access the data randomly, you need a format file that defines the structure (e.g., data fields from your database file, key fields) the program uses. You'll define this file in the RPG program (rather than the actual file) and instruct the OpnQryF command to use this format to present your file's data. A sample should clear things up a bit.

First, the format file can be a physical file or a logical file. Because the format file's purpose is to define a structure rather than to contain data, I suggest that you create it devoid of members. You use parameter Mbr(*None) on the compile command to accomplish this.

Sample original physical file NameFile:

      *    =================================================================
      *    = File.......... NameFile                                       =
      *    = Description... Sample file                                    =
      *    =================================================================

     A          R NAMEFILER

     A            FIRSTNAME     15
     A            MIDDLENAME    15
     A            LASTNAME      20
     A            STATUS         1
     A            SOMEINFO     256
 

Let's now create a format file that defines key fields. I chose to use a logical file.

Format file NameFileF:

      *    =================================================================
      *    = File.......... NameFileF                                      =
      *    = Description... Sample format file                             =
      *    =================================================================

     A          R NAMEFILER               PFILE(NAMEFILE)

     A          K LASTNAME
     A          K FIRSTNAME
     A          K MIDDLENAME
 

Now that we have the format file, let's look at a sample RPG program that uses random access:

       //  =================================================================
       //  = Sample RPG program                                            =
       //  =================================================================

     FNameFileF IF   E           K Disk

     C     NameKey       KList
     C                   KFld      LastName
     C                   KFld      FirstName
     C                   KFld      MiddleName

      /Free

           LastName = 'Davidson' ;
           FirstName = 'John' ;
           MiddleName = 'Wayne' ;

           Chain NameKey NameFileF ;
           Select ;
             When %Found ;
               .
               .
               .
             Other ;
               .
               .
               .
           EndSl ;

          *InLR = *On ;

      /End-Free
 

You can see that there's nothing special about the RPG program.

The magic occurs in the CL program, as the following sample demonstrates:

/*  ====================================================================  */
/*    = Sample CL program                                              =  */
/*  ====================================================================  */

Pgm

  OvrDbF     File( NameFileF )                                             +
             ToFile( NameFile )                                            +
             Share( *Yes )
  OpnQryF    File( NameFile)                                               +
             Format( NameFileF )                                           +
             QrySlt( 'Status = "A"' )                                      +
             KeyFld(                                                       +
                     ( LastName *Ascend )                                  +
                     ( FirstName *Ascend )                                 +
                     ( MiddleName *Ascend )                                +
                   )
  Call       SomeRPG
  CloF       NameFile
  DltOvr     NameFileF

EndPgm 

First, notice that the OvrDbF command overrides the format file to the original database file (NameFile). Next, the OpnQryF opens and queries NameFile. Note that in addition to defining key fields, the OpnQryF command specifies that the commands result format should be that of file NameFileF rather than NameFile. The program then continues by calling the RPG program, closing the open data path, and deleting the file override.

ProVIP Sponsors

ProVIP Sponsors