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-n
is a file described in the File Section with an FD.data-n
is a data item.key-name-n
is a data element that has been named as a key for an indexed file.statement-n
is an imperative statement.
General Rules:
file-1
must beOPEN Input
orI-O
before theREAD
statement is executed.- The Format 1
READ
statement applies to files declared withACCESS MODE IS SEQUENTIAL
orACCESS MODE IS DYNAMIC
. - A successful
READ
statement 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
NEXT
andPREVIOUS
phrases indicate in which direction theREAD
is to move the file pointer in the sequentialREAD
operation. - The file pointer is initially placed at the beginning of the file after a successful
OPEN
statement. - In files described with
ORGANIZATION IS SEQUENTIAL
, aREAD NEXT
statement after anOPEN
statement retrieves the first record in the file. - In files described with
ORGANIZATION IS SEQUENTIAL
, theREAD NEXT
andREAD PREVIOUS
statements 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 NEXT
statement retrieves the next physical record in the file after the position marked by theRELATIVE KEY
, and increments therelative-key
data field. TheREAD PREVIOUS
statement retrieves the previous physical record in the file after the position marked by theRELATIVE KEY
and decrements therelative-key
data field. - In files described with
ORGANIZATION IS INDEXED
, theREAD NEXT
andREAD PREVIOUS
statements 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
START
statement. For details, see the START statement section. - The
INTO
phrase causes aMOVE
todata-1
after the data has been retrieved in the record area of the File Description (FD). - The
IGNORING LOCK
phrase ignores locks held by other programs. IGNORING LOCK
andWITH IGNORE LOCK
are synonyms.- The
WITH LOCK
phrase causes the record to be locked. A locked record cannot beREAD WITH LOCK
or updated by another program. WITH LOCK
andWITH KEPT LOCK
are synonyms.- The
WITH NO LOCK
phrase allows access to the record by other programs. - The
WITH IGNORE LOCK
phrase ignores locks held by other programs. - The
WITH WAIT
phrase causes theREAD
statement to wait for a lock held on record to be released. - The
AT END
condition is triggered when the end of the file is reached in a sequence ofREAD NEXT
statements, or when the beginning of the file is reached in a sequence ofREAD PREVIOUS
statements. - If the
AT END
condition is triggered, and theAT END
phrase is included in theREAD
statement, theFILE STATUS
variable is set to “10”, thestatement-list
within the scope of theAT END
phrase is executed, and control is then passed to the next statement after theREAD
. - The
NOT AT END
condition is triggered when the sequentialREAD
is successful. When theNOT AT END
phrase is included in theREAD
statement, and theREAD
is successful, thestatement-list
within the scope of theNOT AT END
phrase 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-n
is a file described in the File Section with an FD.data-n
is a data item.key-name-n
is a data element that has been named as a key for an indexed file.statement-n
is an imperative statement.
General Rules:
file-1
must beOPEN Input
orI-O
before theREAD
statement is executed.- The Format 2
READ
statement applies to files declared withACCESS MODE IS RANDOM
orACCESS MODE IS DYNAMIC
. - The
INTO
phrase causes aMOVE
todata-1
after the data has been retrieved in the record area of the File Description (FD). - The
KEY
clause is optional. - If the
KEY
clause 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
KEY
clause, 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
READ
statement 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
READ
statement is made using an alternate key that allows duplicates, the first record that matches the given key is retrieved. - The
INTO
phrase causes aMOVE
todata-1
after the data has been retrieved in the record area of the File Description (FD). - The
WITH LOCK
phrase causes the record to be locked. A locked record cannot beREAD WITH LOCK
or updated by another program. WITH LOCK
andWITH KEPT LOCK
are synonyms.- The
IGNORING LOCK
phrase ignores locks held by other programs. IGNORING LOCK
andWITH IGNORE LOCK
are synonyms.- The
WITH NO LOCK
phrase allows access to the record by other programs. - The
WITH WAIT
phrase causes the READ statement to wait for a lock held on record to be released. - The
INVALID KEY
condition is triggered in aREAD
on a relative file when aRELATIVE KEY
data field does not contain a positive integer value. - The
INVALID KEY
condition is triggered in aREAD
on an indexed file when theRECORD KEY
is not found. - When the
INVALID KEY
condition is triggered, and theINVALID KEY
clause is included in theREAD
statement, theFILE STATUS
variable is set to “23”, thestatement-list
within the scope of theINVALID KEY
phrase is executed, and control is then passed to the next statement after theREAD
. - The
NOT INVALID KEY
condition is triggered when the READ is successful. When theNOT INVALID KEY
phrase is included in theREAD
statement, and theREAD
is successful, thestatement-list
within the scope of theNOT INVALID KEY
phrase is executed, and control is then passed to the next statement after theREAD
. - The
READ
statement updates theFILE STATUS
variable. - If a
READ
statement 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.