Reading Record from multiple PF in CL

Reading Record from multiple PF in CL
Reading Record from multiple PF in CL, Open Identifier(OPNID) ,  DCLF, DCL , Receive File (RCVF), Open File Identifier (OPNID), MONMSG , CPF0864, GOTO ,  ibmi, as400 and sql tricks, as400 tutorial, ibmi tutorial
Reading Record from multiple PF in CL

If we want to declare more than one file in a CL program then we have to use the Open Identifier(OPNID) by specifying the unique value in OPNID parameter for each individual file declaration we can read multiple files in a CL program.

CL program for Reading multiple PF

              PGM                                                                     
              DCL        VAR(&NO) TYPE(*DEC) LEN(5 0)                                 
              DCL        VAR(&NM) TYPE(*CHAR) LEN(20)                                 
              DCL        VAR(&ID) TYPE(*DEC) LEN(5 0)                                 
              DCL        VAR(&NAME) TYPE(*CHAR) LEN(20)                               
                                                                                      
              DCLF       FILE(CLPF1)                                                  
              DCLF       FILE(CLPF2) OPNID(F2)                                        
              RCVF                                                                    
              CHGVAR     VAR(&NO) VALUE(&ROLLNO)                                      
              CHGVAR     VAR(&NM) VALUE(&NAME)                                        
              RCVF       OPNID(F2)                                                    
              CHGVAR     VAR(&ID) VALUE(&F2_STUID)                                    
              CHGVAR     VAR(&NAME) VALUE(&F2_STUNAME)                                
              ENDPGM                                                                  

Explanation of the above code

              DCL        VAR(&NO) TYPE(*DEC) LEN(5 0)                                 
              DCL        VAR(&NM) TYPE(*CHAR) LEN(20)                                 
              DCL        VAR(&ID) TYPE(*DEC) LEN(5 0)                                 
              DCL        VAR(&NAME) TYPE(*CHAR) LEN(20)                               

  • Here, we declare four variables of type decimal, character, decimal, character respectively.
  •               DCLF       FILE(CLPF1)                                                  
                  DCLF       FILE(CLPF2) OPNID(F2)                                        
     
  • Here, we have declared 2 PF’s CLPF1 and CLPF2. With CLPF2 we specified the OPNID parameter value as F2.
  •               RCVF                                                                    
                  CHGVAR     VAR(&NO) VALUE(&ROLLNO)                                      
                  CHGVAR     VAR(&NM) VALUE(&NAME)                                        
                  RCVF       OPNID(F2)                                                    
                  CHGVAR     VAR(&ID) VALUE(&F2_STUID)                                    
                  CHGVAR     VAR(&NAME) VALUE(&F2_STUNAME)                                
                  ENDPGM                                                                  
    
  • Here, When I use the RCVF I have to tell it which file to use. The OPNID is used and must match the value in a file declaration and when we are fetching the data then we have to use the open identifier with the underscore for example:- here we are using F2_STUID and F2_STUNAME as prefix as they are the file fields of CLPF2. While reading CLPF1 no identifier used since no identifier was specified during decalaration for this file.
  • Another CL program for Reading multiple PF

                  PGM                                                                    
                  DCL        VAR(&NO) TYPE(*DEC) LEN(5 0)                                
                  DCL        VAR(&NM) TYPE(*CHAR) LEN(20)                                
                  DCL        VAR(&ID) TYPE(*DEC) LEN(5 0)                                
                  DCL        VAR(&NAME) TYPE(*CHAR) LEN(20)                              
                                                                                         
                  DCLF       FILE(CLPF1)                                                 
                  DCLF       FILE(CLPF2) OPNID(F2)                                       
      READ:       RCVF                                                                   
                  MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(FIL))                       
                  CHGVAR     VAR(&NO) VALUE(&ROLLNO)                                     
                  CHGVAR     VAR(&NM) VALUE(&NAME)                                       
                  GOTO       CMDLBL(READ)                                                
      FIL:                                                                               
                  RCVF       OPNID(F2)                                                   
                  MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(END))                       
                  CHGVAR     VAR(&ID) VALUE(&F2_STUID)                                   
                  CHGVAR     VAR(&NAME) VALUE(&F2_STUNAME)                               
                  GOTO       CMDLBL(FIL)                                                 
      END:                                                                               
                  ENDPGM                                                                 
    
    

    Explanation of the above code

    Now, this program is also having 4 variables and having declared the two files but here we are using the concept of labels and GOTO. When this procedure is receiving the message id CPF0864 given in the program which means end of file has been reached and then the program would be navigated to GOTO label in EXEC section. So, whenever the line is executable the pointer will always come to that line and the program will keep reading the file until it reaches the executable line. The MONMSG command follows the Receive File (RCVF) command and, therefore, is only monitoring for messages sent by the RCVF command. this example specifies MSGID(CPF0864), the MONMSG monitors for this condition. When it receives the message, the GOTO CMDLBL(END) command is run.

    Third CL program for Reading multiple PF

                  PGM                                                                    
                  DCL        VAR(&NO) TYPE(*DEC) LEN(5 0)                                
                  DCL        VAR(&NM) TYPE(*CHAR) LEN(20)                                
                  DCL        VAR(&ID) TYPE(*DEC) LEN(5 0)                                
                  DCL        VAR(&NAME) TYPE(*CHAR) LEN(20)                              
                                                                                         
                  DCLF       FILE(CLPF1)                                                 
                  DCLF       FILE(CLPF2) OPNID(F2)                                       
                                                                                         
                  DOWHILE    COND(1 = 1)                                                 
                  RCVF                                                                   
                  MONMSG     MSGID(CPF0864) EXEC(LEAVE)                                  
                  CHGVAR     VAR(&NO) VALUE(&ROLLNO)                                     
                  CHGVAR     VAR(&NM) VALUE(&NAME)                                       
                  ENDDO                                                                  
                                                                                         
                  DOWHILE    COND(1 = 1)                                                 
                  RCVF       OPNID(F2)                                                   
                  MONMSG     MSGID(CPF0864) EXEC(LEAVE)                                  
                  CHGVAR     VAR(&ID) VALUE(&F2_STUID)                                   
                  CHGVAR     VAR(&NAME) VALUE(&F2_STUNAME)                               
                  ENDDO                                                                  
                                                                                         
                  ENDPGM                                                                 
    

    Explanation of the above code

  • Here, we are using DOWHILE loop and we will apply the condition(1=1) which is always True and hence the loop will always be executed infinitely. inside this loop we used the received file command (RCVF) and then one message and once the end of file is reached it will execute LEAVE command.
  • Post a Comment

    © AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain