COBOL Manual for TNS/E Programs (H06.08+, J06.03+)
accessed by the READ statement; therefore, for an indexed or queue file, the program
logic must verify that the prime record key in the logical record has the same value as
the prime record key of the record previously read.
◦ Random-access or dynamic-access files
For relative files with random or dynamic access, the record replaced is the one indicated
by the value of the RELATIVE KEY item. If the old record does not exist, an invalid-key
condition occurs.
For indexed files with random or dynamic access or for files being accessed through
alternate keys using random or dynamic access, the prime record key data item (the
RECORD KEY item) selects the old record to be replaced. The value of alternate keys can
differ in the new records, unless this would create duplicate key values for a key whose
values must be unique.
• UNLOCK Phrase
If the UNLOCK phrase appears, the rewrite operation also assures that the record is not in
the locked state at the completion of the rewrite operation. If the record was not in the locked
state at the beginning of the rewrite operation, no report of this is made to the program.
• Invalid-Key Condition
Any of these circumstances can lead to an invalid-key condition:
◦ The access mode is sequential, the file organization is indexed, and the prime record
key value of the logical record is not equal to the value of the prime record key of the
last record read. This results in a completion status of 21.
◦ The access mode is random or dynamic, and the specified relative key value or prime
record key value does not correspond to that of any record existing in the file. This results
in I-O status code “23.”
◦ One of the alternate key values in the logical record is equal to the value of that key in
a record that already exists in the file, and the DUPLICATES phrase is not specified for
that key. This results in an I-O status code of “22.”
When the invalid-key condition exists, the rewrite operation does not occur and execution of
the REWRITE statement is unsuccessful. The content of the record area is unaffected. If the
INVALID KEY phrase is specified, control passes to the imperative statement in that phrase
and no USE procedure is executed. If the INVALID KEY phrase is not specified but an applicable
USE procedure exists, that procedure is executed.
When the invalid-key condition does not exist, and the rewrite operation is successful, and
the NOT INVALID KEY phrase is specified, control passes to the imperative statement in that
phrase.
Example 130 REWRITE Statement for Indexed File
READ MASTER-IN WITH LOCK
IF NO-ERROR
PERFORM GET-INPUT
REWRITE MASTER-RECORD WITH UNLOCK
IF NO-ERROR
ADD 1 TO UPDATE-COUNTER
ELSE UNLOCKRECORD MASTER-IN
END-IF
ELSE
...
REWRITE 427










