The READ statement makes records available to the program from the program's data files.
Format 1
READ file-name [NEXT ] RECORD
[PREVIOUS]
[BACKWARD]
[ WITH [NO ] LOCK ]
[KEPT]
[ INTO dest ]
[ ALLOWING UPDATERS ]
[ AT END statement-1 ]
[ NOT AT END statement-2 ]
[ END-READ ]
Format 2
READ file-name RECORD
[ WITH [NO ] LOCK ]
[KEPT]
[ INTO dest ]
[ ALLOWING UPDATERS ]
[ KEY IS key-name ]
[ INVALID KEY statement-1 ]
[ NOT INVALID KEY statement-2 ]
[ END-READ ]
Syntax Rules
- file-name is the name of a file described in the Data Division. It may not be a sort file.
- dest is a data item.
- key-name is the name of a data item specified as a record key for
file-name.
- statement-1 and
statement-2 are imperative statements.
- Format 1 must be used for sequential access files.
- The KEY phrase can be used only for indexed files.
- dest may not occupy any of the storage area used by the record area of
file-name.
- BACKWARD and PREVIOUS are equivalent.
- The NEXT or PREVIOUS phrase must be specified for a Format 1 READ for dynamic access mode files.
- The PREVIOUS phrase may not be specified for a sequential organization file.
- The word KEPT is treated as commentary.
- The LOCK, INTO and ALLOWING phrases may appear in any order.
General Rules
- The file referenced by a READ statement must be open in the INPUT or I-O mode when the statement executes.
- For sequential access mode files, if neither NEXT nor PREVIOUS is used, NEXT is implied.
- A successful READ statement causes the file's record area to be filled with the record retrieved from the file.
- If the record read is smaller than the record area, the excess characters are left unmodified unless the file has automatic
trailing space removal specified. In this case, the record is padded with spaces.
- The READ statement updates the value of the associated FILE STATUS variable.
- A successful Format 1 READ statement retrieves a record from the file according to the following rules:
- The last OPEN, READ, or START verb used for the file determines which record is retrieved. Other file operations do not affect
which record is retrieved.
- If an OPEN verb was the last verb to affect the file position, then the first record is retrieved if the NEXT phrase is used
(or implied). If the PREVIOUS phrase is used, an end-of-file condition occurs.
- If the last verb to affect the file position was a successful START statement, then the record selected by that START statement
is returned, regardless of whether the NEXT or PREVIOUS phrase was used.
- If the last verb to affect the file position was a successful READ statement, then the following or the preceding record is
retrieved, depending on the NEXT or PREVIOUS phrase used.
- For sequential and relative files, the record ordering is based on the physical ordering of the records in the file (relative
files are physically ordered by ascending record numbers).
- For indexed files, the record ordering is based on the logical ordering of the current Key of Reference. The Key of Reference
is set by the last successful OPEN, READ, or START statement executed for the file.
- When a Format 1 READ statement executes, the preceding rule may indicate that no next logical record exists. When this happens,
the following occurs:
- The at-end condition is set and the appropriate FILE STATUS is set.
- If the AT END phrase is specified, statement-1 executes. Control does not proceed to a USE AFTER EXCEPTION statement.
- If no AT END phrase is specified, but an appropriate USE AFTER EXCEPTION procedure exists, that procedure is executed with
an implied return to the end of the READ statement.
- If neither case (b) nor (c) applies, then an error message is printed and the program halts.
- If the at-end condition does not occur, and no other exception causes the USE AFTER EXCEPTION procedure to execute, the NOT
AT END phrase (if any) is used and statement-2 is executed.
- For a relative file, a Format 1 READ updates the contents of the file's RELATIVE KEY data item to reflect the record number
of the returned record.
- When the program is sequentially accessing records from an indexed file that contains records with duplicated alternate key
values, those records are returned in the same order in which they were created. These duplicate values can be created by
WRITE or REWRITE statements. (These records may be reordered in the process of rebuilding the file on another key.)
For sites using the RMS file system, please note that when a set of records having duplicate keys is encountered, RMS returns
only the first record in the set.
- A Format 2 READ statement provides you with the ability to read records in random order by specifying appropriate key values.
A Format 2 READ statement on a relative file retrieves the record whose record number is specified by the file's RELATIVE
KEY data item.
- For indexed files, a Format 2 READ statement retrieves the record that contains the same key value as the corresponding data
item in the file's record area. The key used is the one named in the KEY phrase of the READ statement. If no KEY phrase is
used, the file's primary key is implied. The key used becomes the file's current Key of Reference for future Format 1 READ
statements. For key values that are duplicated, the record that corresponds to the first of the sequence of duplicated values
(as described in General Rule 10 above) is returned.
- After successfully retrieving a record, a Format 2 READ statement sets the file's File Position Indicator to the next logical
record according to General Rule 6.
- If a Format 2 READ cannot find a record with the appropriate key value, the invalid-key condition exists. When this happens
the following occurs:
- If the INVALID KEY phrase is specified,
statement-1 executes. Control does not proceed to a USE AFTER EXCEPTION statement.
- If no INVALID KEY phrase is specified, but an appropriate USE AFTER EXCEPTION procedure exists, that procedure is executed
with an implied return to the end of the READ statement.
- If neither case (a) nor (b) applies, then an error message is printed and the program halts.
- If the NOT INVALID KEY phrase is used and the invalid-key condition does not exist, and no other condition causes a USE AFTER
EXCEPTION procedure to execute,
statement-2 is executed.
- If an applicable USE AFTER EXCEPTION procedure exists, it executes whenever a condition occurs that results in a non-zero
file status. However, it does not execute if the condition is
invalid-key and an INVALID KEY phrase is used, or if the condition is at-end and an AT END phrase is used.
- If a READ statement is unsuccessful, the current file position and the current Key of Reference are both set to be undefined.
See General Rules 22 and 23 for exceptions.
- The INTO phrase causes the contents of the file's record area to be moved to
dest according to the rules of the MOVE statement. This move occurs after the record is retrieved, but only if the statement is
successful.
- The WITH NO LOCK and ALLOWING UPDATERS phrases are equivalent. They both cause the record to be read without record locking.
In the default mode, any successful READ on a file open in the I-O mode causes the retrieved record to be locked. A locked
record may not be read (with lock) or updated by another program. Once locked, a record remains locked until any other I/O
statement in the program that locked it is executed for the file. (An exception to this is files that hold multiple record
locks--see
File-Control Paragraph for details.) Once another I/O statement is executed for the file, the currently locked record becomes unlocked, even if
the I/O is unsuccessful.
- The WITH NO LOCK and ALLOWING UPDATERS phrases are implied for a file open in the INPUT mode, and thus have no effect.
- For files with manual record locking mode the WITH NO LOCK phrase is implied. For such a file to place a record lock, it must
specify WITH LOCK on the READ statement. See
File-Control Paragraph for more information.
- Normally, a read that fails due to a record lock will return the appropriate FILE STATUS. In RM/COBOL compatibility mode,
however, if the file has no applicable USE AFTER EXCEPTION procedure available, the program will wait until the record becomes
unlocked. It will then read the record and proceed normally. Note that this can result in deadlock. This feature is provided
for RM/COBOL compatibility. Because of the danger in using it, it is not recommended.
- The current Key of Reference and current file position are not modified by a record locked condition. This allows a program
to wait an appropriate amount of time for the record to become unlocked and then try executing the same READ statement without
having to re-establish the current file position.
- If the end of a file is reached by a READ NEXT statement, a subsequent READ PREVIOUS statement will return the last record
in the file. Similarly, if the beginning of a file is reached by a READ PREVIOUS statement, a READ NEXT statement will retrieve
the first record of the file.
- If the NEXT SENTENCE option is used, control passes to the next executable sentence. Note that the ANSI standard states that
NEXT SENTENCE is an archaic feature and its use should be avoided.
- The IBM DOS/VS COBOL
-Cv compatibility mode supports Reversed File Reads.