RPG IV (RPG ILE) and AS400 Data Area Programming

Data Area processing in ILE RPG program

All examples in this section of article use a data area created by the following command

CRTDTAARA DTAARA(EMP_INFO) TYPE(*CHAR) LEN(10) VALUE('01TutoIndi')

To use a data area in an RPG IV program we need to first declare the data area as a variable in the source code of the program. This variable can either be a stand-alone or a data structure. In RPG IV a data area can be declared in the following two ways.

  1. One line declaration ( RPG IV method of declaring a data area; Recommended)
    
    DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++
    DW@DtaAra         S             10A   DTAARA(EMP_INFO)
    
    

    Notice that the data type of the stand alone variable should be same as the data type of the data area. However, if you need to declare a data area which has multiple data formats you should declare it as a data structure. The above dclaration can be made as

    
    DW@DtaAra         DS                  DTAARA(EMP_INFO)
    DW@Emp#                   1      2  0                 
    DW@EmpName                3     10                    
    
    
  2. Declaration Using Like Definition (Old method)
    This section is given for the sake of completion of this article as I've found such declaration at several places in RPG III and even RPG IV, both types of programs!

    This method of declaration is usually a two step method.

    Firstly, an RPG variable is declared. A stand alone variable is declared in the C Spec result section or D-Spec and a data structure is defined in the I Specification or D Spec in RPG IV.

    After that, The data area is declared in the C-Spec using the opcode "Define" as given below. Notice that the first factor of this opcode is *DTAARA which tells the compiler that the factor 2 is a data area and look for it in the library list when compiling.

    
    C     *DTAARA       Define    EMP_INFO      W@DtaAra
    

    Here, W@DtaAra can be a data structure or a stand alone variable. If it's a stand alone variable, it can be defined here itself.

    Note:- The idea is to declare variable and then the Data area. How you achieve this is upto you. Due to this flexibility, I have found several combinations of declaration of data area.


  3. Read a Data Area in ILE RPG (IV)

    Reading a data area in an RPG IV program is a two step process. The first step is to read a data area using the opcode IN as given blow.

    
    C     *Lock         In        W@Dtaara
    

    As soon as this statement is executed, the variable/s is/are populated with the data area value. Here it's important to note that the first factor '*LOCK' is required to maintain data integrity and hence can not be omitted. The exception to this rule is the local data area where no lock is required.

    The second step would to unlock the data area. If you are using a data area for read only purpose, you need to unlock the data area using the opcode Unlock as given blow.

    
    CL0N01Factor1+++++++Opcode&ExtFactor2++
    C                   Unlock    W@DtaAra 
    

    Other way to unlock a data area will be to update it. To update a data area we use the opcode 'OUT'. The opcode OUT updates the date area with the current value of the RPG variable/Data structure. The syntax to update the data area is as given below.

    
    CL0N01Factor1+++++++Opcode&ExtFactor2+
    C                   Out       W@DtaAra
    

    The OUT opcode requires a lock on the data area except in case of local data area where it's not required to take a lock beforehand.

    That's all about data area processing in RPG IV. Now refer the examples given below to become more intimate with data area processing in RPG programs.


    Program using Read only Data Area
    
    DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++
    DW@DtaAra         DS                  DTAARA(EMP_INFO)
    DW@Emp#                   1      2  0                 
    DW@EmpName                3     10                    
     **                                                   
    CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++
    C     *Lock         In        W@Dtaara                
    C     W@Emp#        Dsply                             
    C     W@EmpName     Dsply                             
    C                   Unlock    W@DtaAra                
     **                                                   
    C                   Return                                              
    

    Output:-
    DSPLY 1
    DSPLY TutoIndi


    Program Updating Data Area
    
    DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++
    DW@DtaAra         DS                  DTAARA(EMP_INFO) 
    DW@Emp#                   1      2  0                  
    DW@EmpName                3     10                     
     **                                                    
    CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result+++++
    C     *Lock         In        W@Dtaara                 
    C     W@Emp#        Dsply                              
    C     W@EmpName     Dsply                              
     **                                                    
    C                   Eval      W@Emp# +=1               
    C                   Out       W@DtaAra                 
     **                                                    
    C     W@Emp#        Dsply                              
    C     W@EmpName     Dsply                              
    C                   Return                                                      
    

    Output:-
    DSPLY 1
    DSPLY TutoIndi
    DSPLY 2
    DSPLY TutoIndi

    The above program shows how to update specific portion of a data area. 'OUT' opcode in itself updates all values of the data area. However, with judicious use of data structure we accomplish partial update.


    Summary
    The Data Area article has been quite a long one hence a summary is given below.
    1. Data area is an object. It stores data but it's not a physical file.
    2. CRTDTAARA, DSPDTAARA, CHGDTAARA commands create, display and change a data area respectively. These commands are run from command line.
    3. RTVDTAARA command return the current value of data area in a similar data type CL variable.
    4. We can use specific portion of a data area using substring optional feature of RTVDTAARA and CHGDTAARA commands.
    5. Data area should be defined in the D-Spec in an RPG program.
    6. Reading a data area in an RPG program is a two step process (Except local data area). Read with lock and Unlock.
    7. Data area is read using the opcode IN with *LOCK as factor 1. *LOCK is required if data area is not a local data area.
    8. Data area can be unlocked by UNLOCK or OUT opcodes. Out opcode updates and unlocks the data area.

    Comments and feedbacks are most welcome (Link at the end). If you have any doubts/questions, feel free to ask it in the forum.