READ Statement
The READ statement retrieves records from files.
Format 1
A Format 1 READ is a Sequential READ, which retrieves the NEXT or PREVIOUS record, as determined by the current file pointer.
READ file-1 {NEXT } RECORD INTO data-1
{PREVIOUS}
[{IGNORING LOCK }]
{WITH LOCK }
{WITH NO LOCK }
{WITH IGNORE LOCK }
{WITH WAIT }
{WITH KEPT LOCK }
[ AT END statement-1 ]
[ NOT AT END statement-2 ]
[ END-READ ]
Syntax:
file-nis a file described in the File Section with an FD.data-nis a data item.key-name-nis a data element that has been named as a key for an indexed file.statement-nis an imperative statement.
General Rules:
file-1must beOPEN InputorI-Obefore theREADstatement is executed.- The Format 1
READstatement applies to files declared withACCESS MODE IS SEQUENTIALorACCESS MODE IS DYNAMIC. - A successful
READstatement retrieves a record from an indexed, relative, or sequential file, and stores the record in the data area described in the File Description (FD) in the File Section of the program. - The
NEXTandPREVIOUSphrases indicate in which direction theREADis to move the file pointer in the sequentialREADoperation. - The file pointer is initially placed at the beginning of the file after a successful
OPENstatement. - In files described with
ORGANIZATION IS SEQUENTIAL, aREAD NEXTstatement after anOPENstatement retrieves the first record in the file. - In files described with
ORGANIZATION IS SEQUENTIAL, theREAD NEXTandREAD PREVIOUSstatements retrieve the next/previous physical record in the file after/before the current position of the file pointer. - In files described with
ORGANIZATION IS RELATIVE, theREAD NEXTstatement retrieves the next physical record in the file after the position marked by theRELATIVE KEY, and increments therelative-keydata field. TheREAD PREVIOUSstatement retrieves the previous physical record in the file after the position marked by theRELATIVE KEYand decrements therelative-keydata field. - In files described with
ORGANIZATION IS INDEXED, theREAD NEXTandREAD PREVIOUSstatements retrieve the next/previous physical record in the file after/before the current position of the file pointer. - The file pointer may be placed anywhere in the file with the
STARTstatement. For details, see the START statement section. - The
INTOphrase causes aMOVEtodata-1after the data has been retrieved in the record area of the File Description (FD). - The
IGNORING LOCKphrase ignores locks held by other programs. IGNORING LOCKandWITH IGNORE LOCKare synonyms.- The
WITH LOCKphrase causes the record to be locked. A locked record cannot beREAD WITH LOCKor updated by another program. WITH LOCKandWITH KEPT LOCKare synonyms.- The
WITH NO LOCKphrase allows access to the record by other programs. - The
WITH IGNORE LOCKphrase ignores locks held by other programs. - The
WITH WAITphrase causes theREADstatement to wait for a lock held on record to be released. - The
AT ENDcondition is triggered when the end of the file is reached in a sequence ofREAD NEXTstatements, or when the beginning of the file is reached in a sequence ofREAD PREVIOUSstatements. - If the
AT ENDcondition is triggered, and theAT ENDphrase is included in theREADstatement, theFILE STATUSvariable is set to “10”, thestatement-listwithin the scope of theAT ENDphrase is executed, and control is then passed to the next statement after theREAD. - The
NOT AT ENDcondition is triggered when the sequentialREADis successful. When theNOT AT ENDphrase is included in theREADstatement, and theREADis successful, thestatement-listwithin the scope of theNOT AT ENDphrase is executed, and control is then passed to the next statement after theREAD.
Format 2
A Format 2 READ is a KEY’ed READ, which READ's on the relative key of a relative file, or one of the record keys of an indexed file.
READ file-1 RECORD INTO data-1
[{IGNORING LOCK }]
{WITH LOCK }
{WITH NO LOCK }
{WITH IGNORE LOCK }
{WITH WAIT }
{WITH KEPT LOCK }
[ KEY IS { data-2 } ]
{ key-name-1 } ]
[ INVALID KEY statement-3 ]
[ NOT INVALID KEY statement-4 ]
[ END-READ ]
Syntax:
file-nis a file described in the File Section with an FD.data-nis a data item.key-name-nis a data element that has been named as a key for an indexed file.statement-nis an imperative statement.
General Rules:
file-1must beOPEN InputorI-Obefore theREADstatement is executed.- The Format 2
READstatement applies to files declared withACCESS MODE IS RANDOMorACCESS MODE IS DYNAMIC. - The
INTOphrase causes aMOVEtodata-1after the data has been retrieved in the record area of the File Description (FD). - The
KEYclause is optional. - If the
KEYclause is not present, then:- In relative files, the value of the relative key field is used as the key.
- In indexed files, the current value of the primary key stored in the record in the File Section is used as the key.
- The
KEYclause, when used, references keys as follows:- In relative files, the value of the data field referenced is used as the relative key.
- In indexed files, the data field referenced must identify either the primary key or one of the alternate record keys.
- A successful
READstatement retrieves a record from an indexed, or relative file, and stores the record in the data area described in the File Description (FD) in the File Section of the program. - When a
READstatement is made using an alternate key that allows duplicates, the first record that matches the given key is retrieved. - The
INTOphrase causes aMOVEtodata-1after the data has been retrieved in the record area of the File Description (FD). - The
WITH LOCKphrase causes the record to be locked. A locked record cannot beREAD WITH LOCKor updated by another program. WITH LOCKandWITH KEPT LOCKare synonyms.- The
IGNORING LOCKphrase ignores locks held by other programs. IGNORING LOCKandWITH IGNORE LOCKare synonyms.- The
WITH NO LOCKphrase allows access to the record by other programs. - The
WITH WAITphrase causes the READ statement to wait for a lock held on record to be released. - The
INVALID KEYcondition is triggered in aREADon a relative file when aRELATIVE KEYdata field does not contain a positive integer value. - The
INVALID KEYcondition is triggered in aREADon an indexed file when theRECORD KEYis not found. - When the
INVALID KEYcondition is triggered, and theINVALID KEYclause is included in theREADstatement, theFILE STATUSvariable is set to “23”, thestatement-listwithin the scope of theINVALID KEYphrase is executed, and control is then passed to the next statement after theREAD. - The
NOT INVALID KEYcondition is triggered when the READ is successful. When theNOT INVALID KEYphrase is included in theREADstatement, and theREADis successful, thestatement-listwithin the scope of theNOT INVALID KEYphrase is executed, and control is then passed to the next statement after theREAD. - The
READstatement updates theFILE STATUSvariable. - If a
READstatement is unsuccessful, the file pointer is undefined.
Code Sample:
...
SELECT SEQ-FILE-1 ASSIGNTO"SEQ-FILE-1"
ORGANIZATION IS SEQUENTIAL
ACCESS IS SEQUENTIAL
FILE STATUS IS SEQ-FILE-1-STAT.
...
FD SEQ-FILE-1.
01 SEQ-FILE-1-RECORD PIC X(10).
...
77 SEQ-FILE-1-STATPIC XX.
01 WS-SEQ-FILEPIC X(10).
...
READ SEQ-FILE-1 NEXT RECORD.
READ SEQ-FILE-1 NEXT RECORD INTO WS-SEQ-FILE.
READ SEQ-FILE-1 NEXT RECORD IGNORING LOCK.
READ SEQ-FILE-1 NEXT RECORD WITH LOCK.
READ SEQ-FILE-1 NEXT RECORD WITH NO LOCK.
READ SEQ-FILE-1 NEXT RECORD WITH IGNORE LOCK.
READ SEQ-FILE-1 NEXT RECORD WITH WAIT.
READ SEQ-FILE-1 NEXT RECORD WITH KEPT LOCK.
READ SEQ-FILE-1 NEXT RECORD
AT END MOVE "10" TO SEQ-FILE-1-STAT
NOT AT END
CONTINUE
END-READ.
SELECT IDX-FILE-1 ASSIGN TO "IDX-FILE-1"
ORGANIZATION IS INDEXED
ACCESS IS DYNAMIC
RECORD KEY IS IDX-KEY
FILE STATUS IS IDX-FILE-1-STAT.
FD IDX-FILE-1.
01 IDX-FILE-1-RECORD.
03 IDX-KEY PIC X(10).
77 IDX-FILE-1-STAT PIC XX.
01 WS-IDX-FILE PIC X(10).
...
READ IDX-FILE-1 NEXT RECORD.
READ IDX-FILE-1 NEXT RECORD INTO WS-IDX-FILE.
READ IDX-FILE-1 PREVIOUS RECORD.
READ IDX-FILE-1 IGNORING LOCK.
READ IDX-FILE-1 WITH LOCK.
READ IDX-FILE-1 WITH NOLOCK.
READ IDX-FILE-1 WITH IGNORE LOCK.
READ IDX-FILE-1 WITH WAIT.
READ IDX-FILE-1 WITH KEPT LOCK.
READ IDX-FILE-1
INVALID KEY MOVE "23" TO IDX-FILE-1-STAT
NOT INVALID KEY
CONTINUE
END-READ.