Agenda.
- Introduction.
- What is the INVALID KEY phrase in COBOL?
- COBOL INVALID KEY phrase Syntax.
- COBOL INVALID KEY Example.
- Conclusion.
Introduction.
What is the INVALID KEY Phrase in COBOL?
The following errors can be detected when INVALID KEY is coded with a WRITE statement:
- a key field that is not in sequence (i.e. Incorrect or Invalid).
- a key field has a duplicate value (i.e. Duplicate key).
- a key field has a blank value (i.e. Null value).
Whenever any of the above conditions are met, the INVALID KEY clause will be executed as an imperative statement.
Whenever you code a NOT INVALID KEY phrase, it is executed only if the statement completes successfully. The INVALID KEY and NOT INVALID KEY phrases are not executed when the operation fails for any other reason than INVALID KEY. Instead, the program passes control to the end of the statement after performing any associated ERROR declarative.
COBOL INVALID KEY phrase Syntax.
As mention earlier, INVALID KEY Clause can be used with the COBOL READ statement, COBOL REWRITE statement and COBOL WRITE statements. The following figure showcases the usage of the INVALID KEY Clause in all three statements.
READ FILE-NAMEINVALID KEY imperative statements
NOT INVALID KEY imperative statements
END-READ.
COBOL INVALID KEY Example.
The following example demonstrates how the INVALID KEY clause can be used to trap error conditions. If a record with key-value as '10093' is not found in the file then the INVALID KEY imperative statement will be executed. Otherwise NOT INVALID KEY clause imperative statement will be executed. In a similar way, you can use the INVALID KEY clause in REWRITE, READ, WRITE and DELETE statements.
INVALID KEY
DISPLAY 'RECORD NOT FOUND!'
NOT INVALID KEY
DISPLAY 'RECORD FOUND!'
END-READ.
Conclusion.
Finally, this marks an end to the INVALID KEY Clause in COBOL. The INVALID KEY phrase in COBOL or COBOL INVALID KEY clause can be used with either the COBOL READ, COBOL WRITE, COBOL DELETE, and COBOL REWRITE statement for indexed files. In this article, you'll learn the basics of INVALID KEY in COBOL, and how to code COBOL INVALID Phrase while writing the COBOL program. Also, do check out COBOL LEVEL 88 Condition.
No comments:
Post a Comment