Problem solve Get help with specific problems with your technologies, process and projects.

Be careful with SETGT + REDPE combination

Be careful with the SETGT and REDPE combination while trying to Read the last record with equal key.

If you have a file with a key that is not unique, and you are trying to reach the last record of a particular key value, the traditional technique used in RPG is SETGT followed by REDPE.

You should be careful with the SETGT & REDPE combination.

For example, you have a file with key values of 10,20,20,30 and 40. If you wanted to read the last record for the key value 20, you would give:

20 SETGT
20 REDPE 50

Here is the scenario that will make the above combination fail:

If you have multiple jobs accessing the same file, and if another job writes a record into the file with a key of 25 after SETGT was executed in your original program, but before REDPE, your REDPE operation will fail--meaning that there is no record with an equal key, though it really exists.

It is because SETGT actually sets the file pointer to the record next to the key value specified. REDPE will basically read the previous record from that position and try to check whether the key is equal to the one specified to set the record found indicator.

In our example, file pointer will be set to 30 and then will try to read the previous record. Since another application has written 25, REDPE will read 25, and since 25 is not the expected key, the record not found indicator will be set.


The following program can be used to illustrate this scenario:

1. Create the following file

R INPUT
FIELD1 2A
FIELD2 10A
K FIELD1

2. Create the following program.

FTESTPF IF E K DISK

*****************************************************************

I 'POINTER IS SET' C #SET
I 'RECORD FOUND ' C #FND
I 'RECORD NOT FOUND' C #NFND
*****************************************************************
C *ENTRY PLIST
C PARM FIELD1
* C FIELD1 SETGTTESTPF 4041
C #SET DSPLY C FIELD1 REDPETESTPF 50
*
C *IN50 IFEQ '0'
C #FND DSPLY
C ELSE
C #NFND DSPLY
C ENDIF
*
C SETON LR
C RETRN

3. Populate the file TESTPF with key values 10,20,20,30,40

4. Get into the RPG program in ISDB with '20' as the input parameter.

5. After executing SETGT, add record '25'.

6. Then step into REDPE operation in the program in ISDB.

7. Now see what happens.

REDPE will fail.

To understand better run the following program like above.

FTESTPF IF E K DISK
*****************************************************************
I 'POINTER IS SET' C #SET
I 'RECORD FOUND ' C #FND
I 'RECORD NOT FOUND' C #NFND
*****************************************************************
C *ENTRY PLIST
C PARM FIELD1
*
*
C FIELD1 SETGTTESTPF 4041
C #SET DSPLY
C READPTESTPF 50
*
C *IN50 IFEQ '0'
C #FND DSPLY
C FIELD1 DSPLY
C FIELD2 DSPLY
C ELSE
C #NFND DSPLY
C ENDIF
*
C SETON LR
C RETRN
****************** End of data
****************************************

The only difference is that instead of REDPE, it is READP. See what happens in READP. It would have read '25'. So be careful to take these into consideration while coding SETGT + REDPE combination.

Dig Deeper on RPG iSeries programming

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataCenter

Close