Here’s a clear explanation and working examples of the CHAIN, UPDATE, and DELETE operations in free-form RPGLE, including how to handle record locks and status codes safely.
๐ 1. CHAIN (Read a Record by Key)
The CHAIN
operation retrieves a record from a keyed file using its key.
โ Syntax:
Chain (CustomerID) Customers;
๐ Example:
**FREE
Dcl-F Customers Usage(*Update) Keyed;
Dcl-S CustomerID Char(10) Inz('C123');
Chain (CustomerID) Customers;
If %Found(Customers);
Dsply ('Customer: ' + %Trim(Name));
Else;
Dsply ('Customer not found');
EndIf;
-
%Found()
checks if a record was found. -
%Error()
+%Status
can check for locks or other issues.
๐ง 2. UPDATE (Write Back to File)
The UPDATE
operation updates the record that was previously read via CHAIN
, READ
, etc.
โ Example:
If %Found(Customers);
Name = 'Updated Name';
Update Customers;
EndIf;
๐ With Lock Detection:
Update Customers;
If %Error and %Status = 1218;
Dsply ('Record is locked, cannot update.');
EndIf;
๐งน 3. DELETE (Remove a Record)
Deletes the current record (previously read with CHAIN
or READ
).
โ Example:
Chain (CustomerID) Customers;
If %Found(Customers);
Delete Customers;
EndIf;
๐งพ Full Example: CHAIN → UPDATE → DELETE
**FREE
Ctl-Opt DftActGrp(*No) ActGrp(*Caller);
Dcl-F Customers Usage(*Update) Keyed;
Dcl-S CustomerID Char(10) Inz('C123');
Chain (CustomerID) Customers;
If %Error and %Status = 1218;
Dsply 'Record is locked. Try later.';
ElseIf %Found(Customers);
Dsply ('Customer: ' + %Trim(Name));
// Update
Name = 'New Name';
Update Customers;
// Delete
Delete Customers;
Dsply 'Customer updated and deleted.';
Else;
Dsply 'Customer not found.';
EndIf;
*INLR = *On;
Return;
๐ง Tips:
Operation | Purpose | Needs *Update ? |
Lock-safe? |
---|---|---|---|
CHAIN |
Read by key | Only if updating | Yes |
UPDATE |
Save modified record | โ Yes | Check lock |
DELETE |
Delete current record | โ Yes | Check lock |