Read, Chain Records From Database File
Process Database (Physical/Logical) Files in ILE RPG
In ILE RPG if you want to read some data then you have to first get the cursor (Assume it similar to the cursor when you use SEU on AS400 machine) to specific record of your interest. To do this, use the Opcode SETLL. SETLL puts the cursor before the record which key fileds' values match with the values specified in the Parameter list's field. The parameter list is specified as the first factor of SETLL. The name of the file on which cursor positioning is to be done is specified as factor 2.
Once the cursor has been set, you perform actual read on the file. For this, use the opcode READ. The name of the file which is to be read is specified as the factor 2.
As soon as you read a file all the file fields in the program are populated with file fields' values.
One successful read operation, advances the cursor to the next record. So if you perform a READ again, the next record is read.
From the above theroy, if you want to read all the records of a file then you need to do the followings.
- Set the cursor at the beginning of the file
- Perform READ operation on the file to be read in a loop.
Now that we know what needs to be done to read all records of a AS400 database file, let us find out how we can do it.
- To set the cursor at the beginning of the file, we specify the *LOVAL as factor 1. When we perform SETLL with *LOVAL on a file, the cursor is positioned before the first record. It means that, when you perform READ operation on the file, the first record will be processed.
How do we specify SETLL on a file which does not have any key fields?
RPG provides the option to set the cursor at any position by using the record number. So to set the cursor before the first record of a database file, just use the record number 1! - The second to-do is very easy. isn't it? Just read in loop! Well, the problem here would be how to exit the loop. (Did you say by using the count of records in file?)
The characteristic of the opcode READ is that whenever a successful read occurs, it sets off the indicator at EQUAL position. In case the End Of File (EOF) is reached, this indicator is set on. So, we generally base looping condition on EQUAL indicator.
Ok, Now its time we saw the concept in action. In the next example, the program processes a database file and displays all the records one by one on pressing enter. If you remember, we used the same files in one of our previous examples! We populated this file there, now we will read the populated records.
Example to display all records of Physical file one by one.
FFilename++IPEASF.....L.....A.Device+.Keywords+++++++++++++++++++++++++ FLOGFILE IF E Disk FLOGDSPF CF E WorkStn ** CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq C 1 SetLl LOGFILE C Read LOGFILE 90 ** C DoW Not (*INKA Or *In90) ** C Eval $Num1 = num1 C Eval $Num2 = num2 C Eval $Sum = $Num1 + $Num2 ** C ExFmt LOG02 ** C Read LOGFILE 90 ** C EndDo ** C Eval *InLr = *On C Return
In the above example see how the looping is handled. First of all we do a SetLl on the file with record number, this is because we are not going to access it with key field. Next we perform a READ on the file. We include the EQUAL indicator in the looping condition. This ensures that the program does not continue if no data is there in the log file. We then calculate the sum and display the screen. When enter is pressed, the file is read for the next record and the next iteration takes place. Here, the program may end if
- User presses F1 to exit
- All records have been displayed. In case there's no record in the log file, the screen is not displayed at all!
The file descriptions remain the same as in the previous example. However, they are given here again for your reference in case you landed directly on this page. Please do not be confused with the label. You might want to modify them.
** The physical file A R LOGPF01 A USER 10A A NUM1 15P 0 A NUM2 15P 0 A K USER // Display File A DSPSIZ(24 80 *DS3) A R LOG02 ** FUNCTION KEY DEFINED A CF01 ** SCREEN FIELDS AND LABELS A R LOG01 A CF01 A 11 19'Please Enter Your Name :' A $USER 10 B 11 45 A 23 3'F1=Exit' A COLOR(BLU) ** A 5 2'Enter two Numbers and Press Ent A to get their sum.' A 7 2'Enter First Number :' A $NUM1 10 0B 7 23 A 8 2'Enter Second Number:' A $NUM2 10 0B 8 23 A 10 23'-----------' A 11 17'Sum =' A $SUM 11 0O 11 23COLOR(WHT) A 23 2'F1=Exit' A COLOR(BLU) A 2 2'Sum Of Two Numbers' A COLOR(WHT) A DSPATR(UL)
Note:-The combined effect of the first two statements is same as using the Opcode CHAIN with factor 1 as 1. The CHAIN is actually equivalent to SETLL + READ.
- 5059 reads
