Reading PF in CL program

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

CL program to read a database file and show its field’s value on the console and help out every time before reading a new record from the file

Before reading any PF in CL program, Let's first create a PF in IBM i.

DDS source for physical file used in the program

      A          R RCLPF1                                                            
      A            ROLLNO         5P 0                                               
      A            NAME          20A                                                 
      A            AGE            3P 0                                               
      A            CLASS          2P 0                                               
  • Here, we created PF named CLPF1 having a record format RCLPF1 with some fields.
  • Now, we will compile the program using the CRTPF command and will insert some records using SQL INSERT statements that you can perform at your end.
  • CL Program for Reading Physical File

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

    Explanation of the above code

                  DCL        VAR(&NO) TYPE(*DEC) LEN(5 0)                                
                  DCL        VAR(&NM) TYPE(*CHAR) LEN(20)                                
    
  • Here, we have taken two variables &NO and &NM also remember these variables’ names must start with the ‘&’ symbol and here the variables are of two types one is decimal and the other is character and we have declared the CLPF1 file
  •               DCLF       FILE(CLPF1)                        
  • This DCLF command is used to declare file named CLPF1.
  •               RCVF                                                                   
                  CHGVAR     VAR(&NO) VALUE(&ROLLNO)                                     
                  CHGVAR     VAR(&NM) VALUE(&NAME)                                       
                  ENDPGM                                                                 
    
  • RCVF command is used to read files in a CL program and it will read only first record and evaluating value from the file fields named &ROLLNO and &NAME to the program variables &NO and &NM.
  • Now compile the program and call the program “call PROGRNAM_NAME” from the IBM i command line. So, it only reads the first record.
  • To read all records there are 2 approaches

    CL program for the first approach

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

    Explanation of the above code

    We are taking the same program, the only difference here is the MONMSG MSGID(CPF0864) and GOTO commands with label and the rest are the same but it will read all the records in a PF. As here the label is provided and then we provided the MONMSG and then GOTO command. So, after 1st read it will read the file CLPF1 and then if any record exists in that file that record will be fetched and again it will come to GOTO Read label when end of file is not reached and the process keeps moving in loop till the time it won't reaches to the end of file condition. The same procedure will be followed for the 2nd program as well.

    CL program for the second approach

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

    Explanation of the above code

    We are using the same program here as well but we are not using the GOTO approach or label approach , Here, infinite DOWHILE LOOP is running as per the condition 1=1 which always true. We have to check the condition at which the loop will be stopped. As here we have defined DOWHILE ( COND 1=1) at first the file will move to RCVF (Receive File) command and then it will check for MONMSG MSGID(CPF0864) and in case if the end of file reaches then the LEAVE command will leave the loop and it will come out of the loop.

    Post a Comment

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