In this topic, we described about the below sections -
READ PREVIOUS statement used to read the previous records from the current reading position of the file. Only one record can be read from the file.
The file must be opened in INPUT or I-O mode to perform the READ PREVIOUS statement. For sequential access, the READ PREVIOUS statement makes the next logical record from a file available to the object program.
Syntax: sequential retrieval
Specifies the file-name defined in data division FD entry.
PREVIOUS RECORD -
Reads the previous record in the logical sequence of records. PREVIOUS phrase applies to indexed and relative files with DYNAMIC access mode. Either the NEXT or PREVIOUS phrase must specify for files reading in dynamic access mode.
If READ...PREVIOUS specified and no previous logical record exists, the AT END condition occurs and the READ statement is unsuccessful.
If READ...PREVIOUS specified, the file position indicator setting is used to determine which record to make available according to the following rules -
- If the file position indicator indicates that no valid previous record has been established, the READ is unsuccessful.
- If the file position indicator is positioned by the execution of an OPEN statement, the AT END condition occurs.
- If the file position indicator is established by a previous START statement, the first existing record in the file whose relative record number (if a relative file) or whose key value (if an indexed file) is less than or equal to the file position indicator is selected.
- If the file position indicator is established by a previous READ statement, the first existing record in the file whose relative record number (if a relative file) or whose key value (if an indexed file) is less than the file position indicator is selected.
INTO identifier-1 -
INTO Clause used to store the retrieved record into the identifier-1. Identifier-1 is the receiving field. Identifier-1 must be a valid receiving the file record in accordance with the rules of the MOVE statement.
If identifier-1 is a date field, then the implied MOVE statement is performed according to the behaviour described under Moves involving date fields.
AT END phrase -
For sequential access, both the AT END phrase and an applicable EXCEPTION/ERROR procedure can be omitted. AT END phrase executed when no next logical record exists or an optional input file is not present.
If the AT END phrase is specified and occurred, control is transferred to imperative-statement-1 in the AT END phrase. If the AT END phrase is not specified and an applicable USE AFTER STANDARD EXCEPTION procedure exists, the procedure is executed.
Both the AT END phrase and an applicable EXCEPTION/ERROR procedure can be omitted. When the at-end condition occurs, execution of the READ PREVIOUS statement is unsuccessful. The contents of the associated record area are undefined.
If an at-end condition does not occur during the execution of a READ PREVIOUS statement, the AT END phrase is ignored if specified.
END-READ phrase -
The END-READ used to end the scope of the READ PREVIOUS statement. END-READ is not required when READ PREVIOUS statement ended with period. END-READ can also be used with an imperative READ PREVIOUS statement.