Search code examples
ibm-midrangerpgle

How to create RPG IV to insert and retrieve data from DB2


I am a newbie to AS/400 and familiar with Data Description Specification (DDS) statements. I have two tables created using a Data Description Specification.

I need to write two RPGLE programs to insert and retrieve from these tables.

EMPPERS FILE

                                         REF(HRFREF)      
         R EMPR                                       
           EMPID     R               REFFLD(IDNO)     
           NAME      R                                
           DOB       R                                
           STS       R                                
         K EMPID                                      

DEPTMNT FILE

                                         REF(HRFREF)  
         R DEPTR                                  
           DEPTID    R               REFFLD(DEPID)
           DEPNM     R                            
           STS       R                            
         K DEPTID                                 

Solution

  • Without actually doing the work for you, here is something to get you started. I agree with the other answers, though. You really need to get into the programmer's guide and reference manual. However, I also understand that if you're coming from this with no RPG IV knowledge whatsoever, it can be intimidating.

    The example below uses direct file access in RPG and is only for the one file. Both files can be treated the same when using this technique.

    Looking at the first line, this is an F-spec. It's used for direct file access to declare your file and to declare how you're going to use it. In the example I declared it as externally described, updateable, full procedural, keyed, and on the disk. Everything else is free-form.

    Femppers   uf a e           k disk
    
     /free
      //Read from EMPPERS file with a given employee id.  Change
      //the status and update it.
      EMPID = 12345;  //assuming EMPID is numeric 5,0.
      Chain (EMPID) Emppers;
      If %Found();
        STS = 'A';    //Assuming STS is a 1-byte alpha
        Update Empr;  //Update the record format, not the file.
      EndIf;
    
      //Add a new record to EMPPERS:
      EMPID = 45678;
      NAME = 'John Doe';
      DOB = Date('1980-01-01':*ISO);
      STS = 'A';
      Write Empr;   //Write to the record format, not the file.
    
      *INLR = *On;  //Tell the program that it's okay to end.
                    //Note: *INLR doesn't actually end the program,
                    //      it just says that it's OKAY to end.
     /end-free