Summary -

In this topic, we described about the below sections -

READ NEXT statement used to read the next 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 NEXT statement. For sequential access, the READ NEXT statement makes the next logical record from a file available to the object program.

Syntax: sequential retrieval

READ NEXT Syntax

file-name-1 -

Specifies the file-name defined in data division FD entry.

NEXT RECORD -

Reads the next record in the logical sequence of records. NEXT is optional when the access mode is sequential and has no effect on READ statement execution. Either the NEXT phrase or the PREVIOUS phrase must specify for files reading in dynamic access mode.

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 NEXT 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 NEXT statement, the AT END phrase is ignored if specified.

END-READ phrase -

The END-READ used to end the scope of the READ NEXT statement. END-READ is not required when READ NEXT statement ended with period. END-READ can also be used with an imperative READ NEXT statement.

Note! If the FILE-STATUS clause is specified, the associated file status key is updated when the READ NEXT statement is executed.

Practical Example - FILE READ NEXT

Below example is used to display all the records after the specified student number from student records.

Code:


 IDENTIFICATION DIVISION.                                         
 PROGRAM-ID. INDXFILD.                                            
 ENVIRONMENT DIVISION.                                            
 INPUT-OUTPUT SECTION.                                            
 FILE-CONTROL.                                                    
     SELECT FILE1 ASSIGN TO DISK1.                               
     ORGANIZATION IS INDEXED.                                    
     ACCESS MODE IS DYNAMIC.                                     
     RECORD KEY  IS STD-NO.                                       
     FILE STATUS IS WS-FS.                                        
 DATA DIVISION.                                                   
 FILE SECTION.                                                    
 FD  FILE1.                                                      
 01 STD-REC.                                                     
     02 STD-NO          PIC 9(03).                                
     02 STD-NAME        PIC X(20).                                
     02 STD-GENDER      PIC X(07).                               
     02 FILLER          PIC X(50).                               
 WORKING-STORAGE SECTION.                                        
 77 WS-FS               PIC 9(02).                              
 01 WS-EOF-SW           PIC X(01) VALUE 'N'.                     
    88 EOF-SW           VALUE 'Y'.                              
    88 NOT-EOF-SW       VALUE 'N'.                              
 PROCEDURE DIVISION.                                            
     ACCEPT STD-NO.                                             
     DISPLAY 'FILE BROWSING...'.                                 
     OPEN INPUT FILE1.                                           

     START FILE1 KEY EQUAL STD-NO.
     INVALID KEY DISPLAY 'NO STUDENT FOUND WITH GIVEN STD-NO'
     NOT INVALID KEY PERFORM READ-NEXT-RECS. 

     CLOSE FILE1.                                                
     DISPLAY STD-REC.  

     STOP RUN.                                                    

 READ-NEXT-RECS.

     PERFORM UNTIL EOF-SW                                         
        READ NEXT FILE1                                           
        AT END MOVE 'Y'  TO WS-EOF-SW                             
           DISPLAY 'RECORD READ : ' STD-REC                       
     END-PERFORM. 

 READ-NEXT-RECS-EXIT.
     EXIT.