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
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)
DCLF FILE(CLPF1)
RCVF CHGVAR VAR(&NO) VALUE(&ROLLNO) CHGVAR VAR(&NM) VALUE(&NAME) ENDPGM
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.