Reusing a Single File Description on Multiple Files

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

 

Dynamically specify files in RPG without the use of embedded SQL.

 

Do you have physical files with the same file format that are separated into multiple physical files--whether it be for legal reasons, query speed, or storage archiving--which requires you to keep modifying your programs when the new files are created? This article will show you how to continue creating new physical files, without the need for additional program compiling and maintenance.

 

In my shop, we had a physical file that needed to be physically separated into different files for legal reasons. These files reused the same physical file layout for each version, and at the end of each year a new file would be created to isolate the data. There was at least one program that would perform historical reporting by accessing several of the files, and this program would need to be maintained each year to include the additional physical file that was created.

 

This type of programming maintenance is highly undesirable, especially at year-end, when you're at your busiest! My initial solution to this problem was to write dynamic embedded SQL in the RPG program. This would have allowed me to specify the file name within the SQL statement to retrieve the data from the file. But this solution would have required a notable amount of program logic to be rewritten. Instead, I chose to use the EXTFILE and EXTMBR keywords on my file specifications to accommodate this capability.

 

Using EXTFILE instead of embedded SQL allowed me to make minimal programming changes to the existing program. All I needed to do was change the file specifications and the existing logic that opened the files.

The File Specification Keywords: EXTFILE and EXTMBR

The file referred to in the file specification must exist in order for your program to compile. You could create an empty generic dummy file to represent the file you will be using throughout the years, or you may chose to use an arbitrary file from a specific year. The only problem with this would come into play when you decide sometime down the line that you want to start archiving older files off of the system. Then the file that your program is referring to would no longer exist, and your program would not be able to compile.

 

     FEMPLOYEE  IF   E           K DISK    USROPN

     F                                     EXTFILE(fileName)

     F                                     EXTMBR(fileMember)

 

 

In the file specification above, you can see the EXTFILE and EXTMBR keywords being specified for the file. Each of these keywords will accept a parameter to specify the external file and member, respectively, to use within the program. The fileName and fileMember parameters are variables that will be defined in your program to determine the file and member to use.

 

The EXTFILE keyword allows you to specify the file to use during run time, instead of at compile time. The compiler will still validate the file existence and its use during the compile, but the actual file that will be used will be determined during run time. When you are using the EXTFILE keyword, you must also specify the USROPN keyword. This is required to specify the file that will be used prior to opening it.

 

You can specify the name of the file with or without the library, and you can also use *LIBL as the library reference. The library is separated from the file using the forward slash. For our example, we will be using the EMPLOYEE file in the MYLIB library, so the fileName will be set to MYLIB/EMPLOYEE. The file name parameter of the EXTFILE keyword is case-sensitive.

 

The EXTMBR keyword can be used to specify the member of the file to open. The EXTMBR keyword is independent of the EXTFILE keyword, so you can use EXTFILE and EXTMBR together or separately. You may decide to use the EXTMBR keyword as a replacement to the OVRDBF command to override your physical file members even if you aren't using EXTFILE. The member parameter of the EXTMBR keyword is case-sensitive.

The DDS

For our programming example, we will be working with a common file layout that will be named EMPLOYEE. The EMPLOYEE DDS will be reused for each new file that is created with the naming convention of "EMP" + YYYY, where YYYY indicates a four-digit year.

 

 

     A          R MCFMT

     A            MCACCT         6S 0       COLHDG('ACCOUNT NUMBER')

     A            MCFNAME       32A         COLHDG('FIRST NAME')

     A            MCLNAME       32A         COLHDG('LAST NAME')

     A          K MCACCT

 

The Example

Suppose we have an employee named Eibeamma that seems to remarry every year and keeps changing her name. The data in our EMPLOYEE file will have a record of this, and we will be able to see any name changes when we read through the EMP2007, EMP2008, and EMP2009 files with our program using EXTFILE and EXTMBR keywords on our F specification.

 

Eibeamma's name may change every year, but her account number of 400 remains the same. So, we will open and close the EMPLOYEE files for the past three years and use her account number to see the changes.

The RPG Code (Free-Format)

     FEMPLOYEE  IF   E           K DISK    USROPN

     F                                     EXTFILE(fileName)

     F                                     EXTMBR(fileMember)

     D*

     D currentKey      S              6S 0

     D currentDate     S               D

     D currentYear     S              4S 0

     D displayBytes    S             52A

     D fileName        S             21A

     D fileMember      S             10A

     C*

      /free

       currentKey = 400;

       // Current Year

       currentDate = %date();

       exsr getAcctName;

       // Last Year

       currentDate = currentDate - %years(1);

       exsr getAcctName;

       // Two Years Ago

       currentDate = currentDate - %years(1);

       exsr getAcctName;

       *inlr = *ON;

       //------------------------------------------------------

       //--- Subroutine: getAcctName                        ---

       //------------------------------------------------------

       begsr getAcctName;

       currentYear = %subDt(currentDate: *YEARS);

       fileName = 'MYLIB/EMP' + %editc(currentYear: '3');

       fileMember = 'MCPRESS';

       displayBytes = 'fileName: ' + %trim(fileName)

                    + ' MBR: ' + %trim(fileMember);

       DSPLY displayBytes;

       open EMPLOYEE;

       chain currentKey EMPLOYEE;

       displayBytes = 'Account '

                    + %trim(%editc(currentKey: '3'));

       if %found();

         displayBytes = %trim(displayBytes) + ': '

                      + %trim(MCLNAME) + ', '

                      + %trim(MCFNAME);

       else;

         displayBytes = %trim(displayBytes) + ': '

                      + ' NOT FOUNDÜ';

       endif;

       DSPLY displayBytes;

       close EMPLOYEE;

       endsr;

      /end-free

The RPG Code (Fixed-Format)

     FEMPLOYEE  IF   E           K DISK    USROPN

     F                                     EXTFILE(fileName)

     F                                     EXTMBR(fileMember)

     D*

     D currentKey      S              6S 0

     D currentDate     S               D

     D currentYear     S              4S 0

     D displayBytes    S             52A

     D fileName        S             21A

     D fileMember      S             10A

     C*

     C                   EVAL      currentKey = 400

     C* Current Year

     C                   EVAL      currentDate = %date()

     C                   EXSR      getAcctName

     C* Last Year

     C                   EVAL      currentDate = currentDate - %years(1)

     C                   EXSR      getAcctName

     C* Two Years Ago

     C                   EVAL      currentDate = currentDate - %years(1)

     C                   EXSR      getAcctName

     C                   EVAL      *inlr = *ON

     C***********************************************************************

     C*  --- Subroutine: getAcctName                        ---

     C***********************************************************************

     C     getAcctName   BEGSR

     C                   EVAL      currentYear = %subDt(currentDate: *YEARS)

     C                   EVAL      fileName = 'MYLIB/EMP'

     C                                      + %editc(currentYear: '3')

     C                   EVAL      fileMember = 'MCPRESS'

     C                   EVAL      displayBytes = 'fileName: '

     C                                          + %trim(fileName)

     C                                          + ' MBR: '

     C                                          + %trim(fileMember)

     C     displayBytes  DSPLY

     C                   OPEN      EMPLOYEE

     C     currentKey    CHAIN     EMPLOYEE                           68

     C                   EVAL      displayBytes = 'Account '

     C                                          + %trim(%editc(currentKey: '3'));

     C                   IF        *IN68 = *OFF

     C                   EVAL      displayBytes = %trim(displayBytes) + ': '

     C                                          + %trim(MCLNAME) + ', '

     C                                          + %trim(MCFNAME)

     C                   ELSE

     C                   EVAL      displayBytes = %trim(displayBytes) + ': '

     C                                          + ' NOT FOUNDÜ'

     C                   ENDIF

     C     displayBytes  DSPLY

     C                   CLOSE     EMPLOYEE

     C                   ENDSR

The Output

There are DSPLY statements throughout the program, so running the program will generate the following results:

 

DSPLY  fileName: MYLIB/EMP2009 MBR: MCPRESS

DSPLY  Account 400: Systumeye, Eibeamma

DSPLY  fileName: MYLIB/EMP2008 MBR: MCPRESS

DSPLY  Account 400: Isereez, Eibeamma

DSPLY  fileName: MYLIB/EMP2007 MBR: MCPRESS

DSPLY  Account 400: Ausfurhundrid, Eibeamma 

 

As you can see, Eibeamma has been busy. But she'll always be known as Ausfurhundrid to me.

Download the Code

You can download this article's code (in both free-format and fixed-format) as well as the DDS for the EMPLOYEE file by clicking here.

 

 

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$