Read PF randomly by a record number in CL program

Read PF randomly by a record number in CL program
Read PF randomly by a record number in CL program, OVRDBF , RRN, DCL , DCLF , CHGVAR , override ,  ibmi, as400 and sql tricks, as400 tutorial, ibmi tutorial, *RRN, POSITION, RCVF, PGM, ENDPGM
Read PF randomly by a record number in CL program

Overriding the database file

We can use OVRDBF command to override the database file in a CL program and read the PF randomly by a record number i.e. RRN(record relative number). The POSITION keyword/option is used to specify the starting position for reading records from the database file. We can specify *START or *END to read first or last record in a file or we can specify nth record using *RRN.

Below we set position at rrn 2 in the file CLPF1 using OVRDBF command.

                      Override with Data Base File (OVRDBF)                   
                                                                              
 Type choices, press Enter.                                                   
                                                                              
 File being overridden  . . . . . FILE         > CLPF1                        
 Overriding to data base file . . TOFILE         *FILE                        
   Library  . . . . . . . . . . .                                             
 Overriding member  . . . . . . . MBR                                         
 Starting position in file:       POSITION                                    
   Retrieve order . . . . . . . .                *RRN                         
   *RRN-rcd nbr *KEY-nbr key flds                2                            
   *KEY-rec format having key . .                                             
   *KEY-key value . . . . . . . .                                             
  • Here we want to read the second record only, so we will write *RRN In the retrieve order and 2 in the RRN-rcd nbr.
  • CL program

                  PGM                                                      
                  DCL        VAR(&NO) TYPE(*DEC) LEN(5 0)                  
                  DCL        VAR(&amo;NM) TYPE(*CHAR) LEN(20)                  
                  DCLF       FILE(CLPF1)                                   
                                                                           
                                                                           
                  OVRDBF     FILE(CLPF1) POSITION(*RRN 2)                  
                  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(&amo;NM) TYPE(*CHAR) LEN(20)  

    The DCL command is used in CLP programs to declare the definition for a program variable. *DEC a variable with a packed decimal value which is LEN(5). *CHAR a variable with a character string value which is LEN(20).

                  DCLF       FILE(CLPF1)                      

    This DCLF command is used to declare file named CLPF1 in cl program.

                  OVRDBF     FILE(CLPF1) POSITION(*RRN 2)

    This OVRDBF command will override the file CLPF1 and it's position the cursor at relative record number 2 of the file.

    RCVF

    This RCVF comamnd is used to recieve file records i.e. to read the file records in CL program specifically. The file being read will be CLPF1 which is declared using DCLF command in the above program.

                  CHGVAR     VAR(&NO) VALUE(&ROLLNO)                       
                  CHGVAR     VAR(&NM) VALUE(&NAME)   
    

    This CHGVAR command will change the variable value or evaluate the NO and NM program variables with file fields ROLLNO and NAME of file CLPF1 being read using RCVF command in the CL program.

    ENDPGM

    This ENDPGM command will end the program at this point.

    Post a Comment

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