For sequential access, the READ statement makes the next logical record from a file available to the object program. For random access, the READ statement makes a specified record from a direct-access file available to the object program.
When the READ statement is executed, the associated file must be open in INPUT or I-O mode.
You must specify either the NEXT phrase or the PREVIOUS phrase for files in dynamic access mode, which are retrieved sequentially.
You must specify either the NEXT or PREVIOUS phrase for files in dynamic access mode, which are retrieved sequentially.
If you specify READ...PREVIOUS and no previous logical record exists, the AT END condition occurs and the READ statement is unsuccessful.
When you specify READ...PREVIOUS, the setting of the file position indicator is used to determine which record to make available according to the following rules:
identifier-1 must be a valid receiving field for the selected sending record description entry in accordance with the rules of the MOVE statement.
The record areas associated with file-name-1 and identifier-1 must not be the same storage area.
When there is only one record description associated with file-name-1 or all the records and the data item referenced by identifier-1 describe an elementary alphanumeric item or an alphanumeric group item, the result of the execution of a READ statement with the INTO phrase is equivalent to the application of the following rules in the order specified:
If identifier-1 is a date field, then the implied MOVE statement is performed according to the behavior described under Moves involving date fields.
When there are multiple record descriptions associated with file-name-1 and they do not all describe an alphanumeric group item or elementary alphanumeric item, the following rules apply:
The KEY IS phrase can be specified only for indexed files. data-name-1 must identify a record key associated with file-name-1. data-name-1 can be qualified; it cannot be subscripted.
For sequential access, both the AT END phrase and an applicable EXCEPTION/ERROR procedure can be omitted.
For information about at-end condition processing, see AT END condition.
Both the INVALID KEY phrase and an applicable EXCEPTION/ERROR procedure can be omitted.
For information about INVALID KEY phrase processing, see Invalid key condition.
This explicit scope terminator serves to delimit the scope of the READ statement. END-READ permits a conditional READ statement to be nested in another conditional statement. END-READ can also be used with an imperative READ statement. For more information, see Delimited scope statements.
If more than one record description entry is associated with file-name-1, those records automatically share the same storage area; that is, they are implicitly redefined. After a READ statement is executed, only those data items within the range of the current record are replaced; data items stored beyond that range are undefined. The following figure illustrates this concept. If the range of the current record exceeds the record description entries for file-name-1, the record is truncated on the right to the maximum size. In either of these cases, the READ statement is successful and the I-O status is set to 04 indicating a record length conflict has occurred.

Format 1 must be used for all files in sequential access mode.
Execution of a format-1 READ statement retrieves the next logical record from the file. The next record accessed is determined by the file organization.
The NEXT RECORD is the next record in a logical sequence of records. The NEXT phrase need not be specified; it has no effect on READ statement execution.
If SELECT OPTIONAL is specified in the file-control entry for this file, and the file is absent during this execution of the object program, execution of the first READ statement causes an at-end condition; however, since no file is present, the system-defined end-of-file processing is not performed.
If the file position indicator indicates that no next logical record exists, or that an optional input file is not present, the following occurs in the order specified:
Both the AT END phrase and an applicable EXCEPTION/ERROR procedure can be omitted.
When the at-end condition occurs, execution of the READ statement is unsuccessful. The contents of the associated record area are undefined and the file position indicator is set to indicate that no valid next record has been established.
If an at-end condition does not occur during the execution of a READ statement, the AT END phrase is ignored, if specified, and the following actions occur:
If no USE AFTER STANDARD EXCEPTION procedure is specified, control is transferred to the end of the READ statement or to imperative-statement-2, if specified.
Following the unsuccessful execution of a READ statement, the contents of the associated record area are undefined and the file position indicator is set to indicate that no valid next record has been established. Attempts to access or move data into the record area following an unsuccessful read can result in a segmentation violation.
The NEXT RECORD is the next logical record in the key sequence.
The PREVIOUS RECORD is the preceding logical record in the key sequence.
For indexed files, the key sequence is the sequence of ascending values of the current key of reference. For relative files, the key sequence is the sequence of ascending values of relative record numbers for records that exist in the file.
Before the READ statement is executed, the file position indicator must have been set by a successful OPEN, START, or READ statement. When the READ statement is executed, the record indicated by the file position indicator is made available if it is still accessible through the path indicated by the file position indicator.
If the record is no longer accessible (because it has been deleted, for example), the file position indicator is updated to point to the next (or previous) existing record in the file, and that record is made available.
For files in sequential access mode, the NEXT phrase need not be specified.
For files in dynamic access mode, the NEXT phrase (or the PREVIOUS phrase) must be specified for sequential record retrieval.
This condition exists when the file position indicator indicates that no next logical record exists (or no previous record exists) or that an optional input file is not present. See the discussion on PREVIOUS RECORD above.
If neither an at-end nor an invalid key condition occurs during the execution of a READ statement, the AT END or the INVALID KEY phrase is ignored, if specified. The same actions occur as when the at-end condition does not occur with sequential files (see AT END condition).
When an ALTERNATE RECORD KEY with DUPLICATES is the key of reference, file records with duplicate key values are made available in the order in which they were placed in the file.
If the RELATIVE KEY clause is specified for this file, READ statement execution updates the RELATIVE KEY data item to indicate the relative record number of the record being made available.
Format 2 must be specified for indexed and relative files in random access mode, and also for files in the dynamic access mode when record retrieval is random.
Execution of the READ statement depends on the file organization, as explained in the following sections.
Execution of a format-2 READ statement causes the value of the key of reference to be compared with the value of the corresponding key data item in the file records, until the first record having an equal value is found. The file position indicator is positioned to this record, which is then made available. If no record can be so identified, an INVALID KEY condition exists, and READ statement execution is unsuccessful. (See “Invalid key condition” under Common processing facilities.)
If the KEY phrase is not specified, the prime RECORD KEY becomes the key of reference for this request. When dynamic access is specified, the prime RECORD KEY is also used as the key of reference for subsequent executions of sequential READ statements, until a different key of reference is established.
When the KEY phrase is specified, data-name-1 becomes the key of reference for this request. When dynamic access is specified, this key of reference is used for subsequent executions of sequential READ statements, until a different key of reference is established.
Execution of a format-2 READ statement sets the file position indicator pointer to the record whose relative record number is contained in the RELATIVE KEY data item, and makes that record available.
If the file does not contain such a record, the INVALID KEY condition exists, and READ statement execution is unsuccessful. (See “Invalid key condition” under Common processing facilities.)
The KEY phrase must not be specified for relative files.
For files with indexed or relative organization, dynamic access mode can be specified in the file-control entry. In dynamic access mode, either sequential or random record retrieval can be used, depending on the format used.
Format 1 with the NEXT phrase must be specified for sequential retrieval. All other rules for sequential access apply.