READNEXT
The CICS FILE READ NEXT statement sequentially retrieves records from a VSAM KSDS or ESDS. It reads records sequentially from the file's current reading position set by STARTBR or RESETBR on a local or remote system. A series of sequential read commands is known as a file browse.
A browse consists of a sequence of READNEXT and READPREV commands in any order. A browse should be initiated with the STARTBR command and terminated with the ENDBR command.
Syntax -
EXEC CICS READNEXT
FILE('file-name')
[UPDATE]
[TOKEN(value)]
INTO(data-area)
[RIDFLD(record-id)]
[KEYLENGTH(key-length)]
[SYSID(system-name)]
[LENGTH(length)]
[REQID(value)]
[RBA|RRN|XRBA]
[RESP(response-field)]
[RESP2(response-field2)]
END-EXEC.
- FILE('file-name') - Specifies the VSAM file name from which the record will be read.
- UPDATE - Specifies that the record is to be retrieved for updating (REWRITE) or deletion (DELETE). If this option is omitted, a read-only operation is assumed. UPDATE guarantees read integrity.
- TOKEN(value) - Specifies a unique identifier for this READ with UPDATE request.
- INTO(data-area) - Defines the data area (variable) where the fetched record is stored.
- RIDFLD(record-id) - Holds the record ID (key) of the retrieved record (only for KSDS files).
- KEYLENGTH(key-length) - Defines the length of the key field (only needed for KSDS). KEYLENGTH is not applicable when RRN is coded. KEYLENGTH must be coded if using SYSID.
- LENGTH(length) - Specifies the length of the record being read (used for variable-length records).
- SYSID(system-name) - Specifies the system name to which the request is directed. If SYSID coded and omit RBA, XRBA and RRN, LENGTH and KEYLENGTH must be coded.
- REQID(value) - Specifies unique request identifier from the browse.
- RBA|RRN|XRBA - Specifies the type of the file and data in the RIDFLD.
- RESP(response-variable) - Optional. It captures the response code of the READ operation and used to check if the command executed successfully or encountered an error.
- RESP2(response2-variable) - Optional. It captures the response2 code of the READ operation when the error occured.
How it works?
- A STARTBR (START BROWSE) command is issued to open a sequential browse.
- The READNEXT command retrieves the first record after the starting point.
- Subsequent READNEXT commands fetch the next record in sequence.
- The browse ends when the last record is reached (ENDFILE condition).
- The browse session is closed using ENDBR (END BROWSE) after reading all records.
Short Examples -
Scenario1 - Sequential forward Read of a KSDS File.
...
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CUSTOMER-RECORD.
05 WS-CUST-ID PIC X(10).
05 WS-CUST-NAME PIC X(30).
05 WS-CUST-ADDRESS PIC X(50).
05 WS-CUST-PHONE PIC X(15).
...
PROCEDURE DIVISION.
MOVE "12345" TO WS-CUST-ID.
EXEC CICS STARTBR
FILE('CUSTFILE')
RIDFLD(WS-CUST-ID)
RESP(WS-RESP)
END-EXEC.
IF WS-RESP = DFHRESP(NORMAL) THEN
PERFORM READ-NEXT-REC
THRU READ-NEXT-EXIT
UNTIL EOF-CUST-FILE
END-IF.
EXEC CICS ENDBR
FILE('CUSTFILE')
END-EXEC.
...
READ-NEXT-REC.
EXEC CICS READNEXT
FILE('CUSTFILE')
INTO(WS-CUST-RECORD)
RESP(WS-RESP)
END-EXEC.
IF WS-RESP = DFHRESP(NORMAL) THEN
...
ELSE IF WS-RESP = DFHRESP(ENDFILE) THEN
SET EOF-CUST-FILE TO TRUE
ELSE
...
END-IF.
READ-NEXT-EXIT
EXIT.
...
The STARTBR command starts the browse session for sequential access. The READNEXT command retrieves records one by one. The loop continues until the last record is reached (ENDFILE). The ENDBR command is issued to close the browse session.
Error Conditions -
Eror Condition | RESP | RESP2 | Reason |
---|---|---|---|
FILENOTFOUND | 12 | 1 | A file name in the FILE option is not defined to CICS. |
NOTFND | 13 | 80 | An attempt to retrieve a record based on the search argument provided is unsuccessful. |
13 | 81 | XRBA was specified and the value of RIDFLD was greater than 4 GB but the data set is not an extended addressing ESDS. | |
DUPKEY | 15 | 140 | A record is accessed by way of an alternate index with the NONUNIQUEKEY attribute and another alternate index record with the same key follows. |
INVREQ | 16 | 20 | The FILE definition does not allow updates. |
16 | 25 | The KEYLENGTH, GENERIC was specified on the STARTBR or the last RESETBR and the value of KEYLENGTH was greater than the full key length. | |
16 | 26 | The KEYLENGTH option is specified for a nongeneric browse and the specified length does not equal the length defined for the data set. | |
16 | 34 | The REQID, SYSID or file name does not match with any successful STARTBR command. | |
16 | 37 | The type of record identification used to access a data set during the browse has been changed. | |
16 | 42 | The KEYLENGTH, GENERIC was specified on the STARTBR or the last RESETBR and the value of KEYLENGTH was less than zero. | |
16 | 54 | UPDATE is not allowed because the file is not a VSAM file that is accessed in RLS mode. | |
16 | 55 | NOSUSPEND is specified on a READ request to a non-RLS mode file. | |
IOERR | 17 | 120 | There is an I/O error during the READNEXT operation. |
ENDFILE | 20 | 90 | An end-of-file condition is detected during the browse. |
ILLOGIC | 21 | 110 | VSAM error occurs that is not in one of the other CICS response categories. |
LENGERR | 22 | 10 | Neither the LENGTH nor the SET option is specified for a file with variable-length records or a BDAM file with undefined-format records. |
22 | 11 | The length of the record read with the INTO option exceeds the value specified in the LENGTH option. | |
22 | 13 | An incorrect length is specified for a file with fixed-length records. | |
SYSIDERR | 53 | 130 | The SYSID name specified is neither the local region nor a remote system or the link to the remote system is closed. |
ISCINVREQ | 54 | 70 | The remote system indicates a failure that does not correspond to a known condition. |
NOTAUTH | 70 | 101 | A resource security check has failed on FILE (filename). |
LOCKED | 100 | 106 | An attempt is being made to read a record either specifying the UPDATE keyword or specifying CONSISTENT or REPEATABLE, but the record is locked by a retained lock. |
RECORDBUSY | 101 | 107 | The NOSUSPEND keyword is specified and the record is locked by an active lock. AEX9 |