Let's go through how SETLL and SETGT interact with special constants like *START, *END, *HIVAL, and *LOVAL in RPGLE for navigating files.
These special constants help you position the file pointer when you don’t know the exact key value, and want to move to either the start or end of the file.
๐ SETLL vs SETGT
| Operation | Meaning |
|---|---|
SETLL |
Position to record equal to or greater than the key |
SETGT |
Position to record strictly greater than the key |
๐ฐ Special Constants
| Constant | Meaning |
|---|---|
*START |
Lowest key — positions before the first record |
*LOVAL |
Lowest possible value for the key (same as *START) |
*END |
Highest key — positions after the last record |
*HIVAL |
Highest possible value for the key (same as *END) |
โ Examples
1๏ธโฃ Read File Forward from the Beginning
Setll *Start MyFile;
Read MyFile;
Dow Not %EOF(MyFile);
Dsply MyFileField;
Read MyFile;
EndDo;
-
Reads all records from beginning to end.
2๏ธโฃ Read File Backward from the End
Setll *End MyFile;
Readp MyFile;
Dow Not %EOF(MyFile);
Dsply MyFileField;
Readp MyFile;
EndDo;
-
Starts at the end and reads in reverse order.
3๏ธโฃ Use *LOVAL and *HIVAL for Keyed Files
For a keyed file where you want to read:
All records ascending:
Setll *LOVAL MyKeyedFile;
Read MyKeyedFile;
All records descending:
Setll *HIVAL MyKeyedFile;
Readp MyKeyedFile;
๐ซ When NOT to Use
-
Don’t use these constants with non-keyed files or files opened with OPNQRYF.
-
These are not valid as actual key values, just file pointer positioners.
Perfect — here's a real-world example showing how to use SETLL *HIVAL with READPE to get the last record for each key value, such as the latest invoice per customer.
๐งพ Scenario
You have a file called INVOICESL1, logically keyed as:
CustomerID (Ascending)
InvoiceDate (Descending)
This way, the most recent invoice appears first for each customer when reading backward.
๐ RPGLE Example: Get Last Invoice Per Customer
Dcl-F InvoicesL1 Usage(*Input) Keyed;
Dcl-S CustomerID Char(10);
Dcl-S InvoiceID Char(10);
Dcl-S InvDate Date;
CustomerID = 'C123';
Setll CustomerID InvoicesL1;
Readpe CustomerID InvoicesL1;
Dow Not %EOF(InvoicesL1) and CustomerID = 'C123';
Dsply ('Invoice: ' + InvoiceID + ' on ' + %Char(InvDate));
Readpe CustomerID InvoicesL1;
EndDo;
๐ Key Points:
-
Logical File Sort Order is crucial: must be
CustomerID ASC, InvoiceDate DESC. -
SETLL *HIVAL+READPEstarts from the end of the file. -
The program keeps track of the previous
CustomerIDto avoid processing multiple invoices per customer.