how does CHAIN statment work? rpgle as400

Posted on 2011-10-25
Last Modified: 2012-06-21
How does CHAIN work?
in particular, if I have 10 records: 1,2,3,4,..,10
if I have just read the 10th record (10).
if I execute this statment:2 chain file
Does the program auto-return to the top of the file in order
to find my 2th record,
or instead it returns NOT FOUND value??

Question by:bobdylan75

    Author Comment

    the key is obviously the only field I have, the value 1,2,3..10
    LVL 14

    Expert Comment

    chain gets the a rcord basewd on the index on the file along with other criteria.
    It return a single record.

    if you have the 10 recors in an indexed file

    chain 2 will reset the pointer, if you then did a read you would ger record 3.


    Author Comment

    I don't mean 2, but the key=2 like the value of the 2th record.
    eval key= 2
    key chain filename

    the key of the file if the field VALUE. (1,2,3,..10)
    LVL 34

    Accepted Solution

    CHAIN can operate on file opened in Keyed sequence, or in Arrival sequence.

    If the file is opened in Keyed mode, then CHAIN causes a single-record lookup by key.  The position of the file pointer before the CHAIN operation does not matter.  CHAIN causes the system to search the index of the file and locate the first record with a matching key.  If a matching record is found, the record is retrieved into the program's input buffer, and the file pointer is positioned at the next record in keyed sequence, so a subsequent READ operation would retrieve the next record in keyed sequence.

    Let's use a different example, since the numeric keys can be confusing when discussing relative record numbers.

    Given this file (on arrival sequence)

    RRN   Key
    1       Jerrold
    2       Ladonne
    3       Gary
    4       John
    5       Mark

    File1 opened in arrival sequence:

    REC CHAIN FILE1 <- RRN2 is retrieved
    READ FILE1 <- RRN3 (next in arrival sequence) is retrieved

    Keyed order would be:

    Key        RRN
    Gary       3
    Jerrold    1
    John       4
    Ladonne 2
    Mark       5

    File1 opened "K"eyed":

    KEY CHAIN FILE1 <- RRN4 is FOUND, and retrieved.
    READ FILE1 <- RRN 2 (next in keyed sequence) is retrieved

    - Gary Patterson

    Author Closing Comment

    thank you very clear explanation.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now