RPG file operation codebook
Plain-English IBM i learning notes with examples, comparisons, and official references.
| Opcode | Definition | RPG example | SQL idea |
|---|---|---|---|
READ | Read the next sequential record from the current file position. | read CUSTOMER; if not %eof(CUSTOMER); // use the record | SELECT ... ORDER BY key |
CHAIN | Retrieve one record by key or relative record number. | chain customerId CUSTOMER; if %found(CUSTOMER); // use the record | SELECT ... FROM CUSTOMER WHERE CUSTOMER_ID = :customerId |
SETLL | Position 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 |
SETGT | Position 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 |
READE | Read 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 |
READP | Read 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 |
READPE | Read 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 |
WRITE | Create a new record in an output-capable file. | eval orderId = nextId; write ORDER; | INSERT INTO MYLIB.ORDER (ORDER_ID, ...) VALUES (:orderId, ...) |
UPDATE | Modify 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 |
DELETE | Delete 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 |
EXFMT | Write a display format and wait for input in one operation. | exfmt OrderScreen; | No direct SQL equivalent; use a UI boundary around the database operation |
READC | Read 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 / CLOSE | Explicitly 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 / ROLBK | Commit or roll back the current transaction when commitment control is active. | commit; // or rollback on error | COMMIT; / ROLLBACK; |