SQL + FILE OPERATIONS

RPG file operation codebook

Plain-English IBM i learning notes with examples, comparisons, and official references.

OpcodeDefinitionRPG exampleSQL idea
READRead the next sequential record from the current file position.
read CUSTOMER;
if not %eof(CUSTOMER); // use the record
SELECT ... ORDER BY key
CHAINRetrieve one record by key or relative record number.
chain customerId CUSTOMER;
if %found(CUSTOMER); // use the record
SELECT ... FROM CUSTOMER WHERE CUSTOMER_ID = :customerId
SETLLPosition at the first record greater than or equal to a key; %EQUAL tells you whether the key exists.
setll customerId CUSTOMER;
if %equal(CUSTOMER); // exact key exists
SELECT ... WHERE CUSTOMER_ID >= :customerId ORDER BY CUSTOMER_ID FETCH FIRST 1 ROW ONLY
SETGTPosition after a key so a following READP can retrieve the prior record.
setgt customerId CUSTOMER;
readp CUSTOMER;
SELECT ... WHERE CUSTOMER_ID < :customerId ORDER BY CUSTOMER_ID DESC FETCH FIRST 1 ROW ONLY
READERead the next record while its key remains equal to the search key.
setll customerId ORDER;
reade customerId ORDER;
SELECT ... WHERE CUSTOMER_ID = :customerId ORDER BY key
READPRead the previous record relative to the current file position.
readp CUSTOMER;
if not %eof(CUSTOMER);
SELECT ... WHERE key < :currentKey ORDER BY key DESC FETCH FIRST 1 ROW ONLY
READPERead the previous record whose key equals the search key.
readpe customerId ORDER;
SELECT ... WHERE CUSTOMER_ID = :customerId AND key < :currentKey ORDER BY key DESC FETCH FIRST 1 ROW ONLY
WRITECreate a new record in an output-capable file.
eval orderId = nextId;
write ORDER;
INSERT INTO MYLIB.ORDER (ORDER_ID, ...) VALUES (:orderId, ...)
UPDATEModify the most recently read record that is eligible for update.
chain orderId ORDER;
if %found(ORDER); update ORDER;
UPDATE MYLIB.ORDER SET ... WHERE ORDER_ID = :orderId
DELETEDelete the current eligible record or a record found by key.
chain orderId ORDER;
if %found(ORDER); delete ORDER;
DELETE FROM MYLIB.ORDER WHERE ORDER_ID = :orderId
EXFMTWrite a display format and wait for input in one operation.
exfmt OrderScreen;
No direct SQL equivalent; use a UI boundary around the database operation
READCRead the next changed subfile record.
readc SFLREC;
if not %eof(); // validate changed row
No direct SQL equivalent; changed-row state belongs to the display program
OPEN / CLOSEExplicitly open or close a file when USROPN or resource ownership requires it.
open CONFIG;
// use it
close CONFIG;
Connection and cursor lifecycle are the closest SQL analogue
COMMIT / ROLBKCommit or roll back the current transaction when commitment control is active.
commit;
// or rollback on error
COMMIT; / ROLLBACK;

IBM documentation