Chain(NE) hang low…. 

 November 17, 2015

By  NickLitten

The wonderful IBM RPGLE programming language:

What is Chain(N) and Chain(NE)


For file I/O requests in RPG (ie: Chain, Read, Reade, Setll, etc) we can add some options using

(N) – No Lock

(E) – Error Logging

(NE) – No Lock and Error Logging

Writing code in RPG the CHAIN operation code is used to go and get a specific ROW (or record) of data from a file. It returns the first entry that matches the KEY that is being used. In this blog let’s look at what happens when the file(CustomerMasterFile) is defined in our program as an UPDATE file. So, if we wanted to retrieve the Customer information from a Customer Master file (assuming it is keyed by the company name and the customer id number) we might code it simply like this:

CHAIN (Company : CustomerNumber) CustomerMasterFile;

which is basically the same as:

SETLL (Company : CustomerNumber) CustomerMasterFile;
READE (Company : CustomerNumber) CustomerMasterFile;

This will get the data that matches the key criteria (Company and Customer) and it will place a record lock to prevent any other programs reading that record while we are exclusively using it. Now, we can add these extender things to add extra functions. So, you might also see this code like this

CHAIN(N) (Company : CustomerNumber) CustomerMasterFile;
 If %Found(CustomerMasterFile);
   Exsr DoCustomerStuff;
   Exsr OhDearIcouldntFindIt;

And this means – read the data but do not lock the record (obviously this only applies to files defined as update capable). For files defined as UPDATE then records are *locked ready to be updated. Using the (N) extender says – read this update file, treat it like an INPUT file and do not lock the record you are reading.

The trouble is that reading a record with (N) *NOLOCK might still fail if somebody else has already grabbed that record and is exclusively locking it.

So what if we see this:

CHAIN(NE) (Company : CustomerNumber) CustomerMasterFile;

This extends the function to say N=NOLOCK and E=ERRORLOGGING so we are given more information about the CHAIN attempt. Code to use the NE example might look something like this:

 If %Found(CustomerMasterFile);
   Exsr DoCustomerStuff;
 ElseIf %ERROR;
   Exsr HandTheFileReadError;
   Exsr OhDearIcouldntFindIt;


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

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

Get In Touch

I’m always looking for awesome input, feedback and critique!


Snug CBD

 20% Discount

I have partnered with SNUG CBD givING you Organic CBD
20% discount code "NL20"