How to - update selected fields in a file with RPGLE

If you want to update just one field in a file using RPGLE without rewriting the entire record. In IBM i (AS/400) RPGLE, you can do a partial update using the %FIELDS built-in function.

Example: Update a Single Field in RPGLE (Free-Format)

dcl-f CUSTFILE usage(*update);

dcl-s custId like(CUSTID);
dcl-s newPhone like(PHONE);

custId = '1001';
newPhone = '7025551234';

// Read the record by key
chain custId CUSTFILE;

if %found(CUSTFILE);
 PHONE = newPhone;
 update %fields(PHONE) CUSTFILE; // Only updates PHONE field
endif;

Key Points

  • CHAIN retrieves the record by key and locks it for update.
  • %FIELDS(PHONE) ensures only the PHONE field is updated, leaving other fields untouched.
  • This is more efficient than rewriting the entire record, especially for large files.

Key Points

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>