Update causing reade to read the same record in RPGLE

20 pts.
Tags:
AS/400
RPGLE
Hi Team, I have a critical issue. In RPGLE program, I have read a file then based on some condition I am updating the file if condition is not satisfied I am not updating that file. I am doing it in a do while loop. The issue I am facing is the record that has been updated is being read again. Please find below snipped of the code.
Key setll file

Key reade file

dow not eq %eof

if condition

Update

endif

key reade file

enddo
In this snippet if the record is not being updated then file read is going to EOF. If a record is updated then the reade is causing to read the same record which is being updated again. Could you please help me in solving this problem? Thanks for the help in advance. Thanks, Soundariya Kumaran
1

Answer Wiki

Thanks. We'll let you know when a new response is added.

That is because you are update one of the Key Fields in the access path. This happens dynamically. Different possible solutions:

Use a different file (with different access path)  as input and chain to do your update.

Before you do the update, save all the fields in a file to a DS. Then on a reade, compare the input data to the data in the DS, if it is the same, go get the next record.

Discuss This Question: 14  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Florenjm
    Do not use readE, just plain read.

    240 pointsBadges:
    report
  • philpl1jb

    dow not eq %eof

    I'm not familiar with the eq in this example.

    I've always seen

    dow not %eof

    or

    dow not %eof( filename)


    54,090 pointsBadges:
    report
  • soundariya
    If I use read it will go till end of file and I want to read based on key to avoid unnecessary reads. And I have used not %eof only.
    20 pointsBadges:
    report
  • CharlieBrowne

    OK, then use the 2nd part of my answer:

    Before you do the update, save all the fields in a file to a DS. Then on a reade, compare the input data to the data in the DS, if it is the same, go get the next record.

    62,385 pointsBadges:
    report
  • philpl1jb

    I would save the key values of each field as you read it.

    Before the secondary READE, use a setgt based on old key values.

    54,090 pointsBadges:
    report
  • CharlieBrowne

    Doing SETGT may not work as there can be multiple child records with the same key and you would not process them all,

    62,385 pointsBadges:
    report
  • lance2221357
    Can you show us FFD, key sequence and RPGLE main source and key list.
    270 pointsBadges:
    report
  • pdraebel
    I think you are doing the READE with a Key that is only a partial key for the file. The update probably updates a field that is part of the File key, but not part of the Key list you use for reading. If so you could be helped by creating a Lf with as key fields exactly the fields you use for reading the file.
    7,545 pointsBadges:
    report
  • philpl1jb

    Whatever you do .. it seems like if you change a key value to a higher value and it's still in the subset (of READE values) you're going to read it again....

    I would read all records in the set

    .. writing them without change to a work file. 

    Then loop through the work file

    .. chain into the PF to get the correct record and update it then.

    If you would rather, you could substitute an array or multi-occurring data structure for the work file.

    54,090 pointsBadges:
    report
  • ToddN2000
    More info is needed as to what field(s) comprise your key. We also need to know what field(s) are being updated.
    134,445 pointsBadges:
    report
  • aceofdelts

    I ran into a similar issue recently. I switched the loop to do the initial SETLL using the full key and READE using a partial key (one less field). After each update, do a SETGT using the full key and READE using a partial key.

    I hate that structure but it got rid of the loop.


    2,550 pointsBadges:
    report
  • GregManzo
    Your best bet is to read & update through an access path that does NOT include the fields you are updating as part of the key. That way you avoid the whole issue of having the record repositioned in the file when you update the key. If you don't have one and can't make one (even if only temporarily) then next best is the 'copy to a work file' as suggested by philpl1jb above.
    ((Last time I fell for this trap was on a S/38 in the 80's - still remember it well because the file was also journalled. I wrote 1000 before & after images of _every_ record in the file into the journal receiver and filled disk - my boss wasn't happy)).
    2,960 pointsBadges:
    report
  • PGMBOB
    I have tried to update by relative record number. read a logical by your keys  You might get the relative record number of the file from the fie information data structure. If the record meets the condition chain to the physical file by relative record and complete the update.  Your next read might give you the next record. Good Luck
    1,235 pointsBadges:
    report
  • Jaideep Khanduja
    use a flag for each record read once and don't read it again. if flag is true skip the record.
    19,430 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: