CHAIN/UPDATE/DELETE

22 Views


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

 

Post Comments