Monday 28 July 2014

COBOL Invalid Key Clause | INVALID KEY Phrase in COBOL | COBOL Invalid Key Example.

File handling in COBOL is remarkably convenient. COBOL READ statement is used to read data from the file, and the file could be a sequential file or VSAM. COBOL INVALID KEY Phrase or simply COBOL INVALID KEY clause is used to handle an exception while reading data from the file. 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.  

Agenda.

  • Introduction.
  • What is the INVALID KEY phrase in COBOL?
  • COBOL INVALID KEY phrase Syntax.
  • COBOL INVALID KEY Example.
  • Conclusion.

Introduction.

File handling is an important feature of any programming language. In COBOL, file handling is exceptionally simple. There are only four statements (i.e. COBOL READ statement, COBOL WRITE statement, COBOL DELETE statement, and COBOL REWRITE statement)  that can be used to access/update data in the sequential file or indexed file, or VSAM cluster.

What is the INVALID KEY Phrase in COBOL?

The INVALID KEY clause can be used with either the READ, WRITE, DELETE, and REWRITE statement for indexed files; it ensures that file records have valid keys. 

Important Point:- A valid key means, the correct key value of the records that are present in the file.    

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-NAME
                INVALID KEY  imperative statements
                NOT INVALID KEY imperative statements
    END-READ.

 INVALID KEY in COBOL, COBOL WRITE INVALID KEY CLAUSE

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.

MOVE '10093'   INTO EMPID.
READ EMP-MAST  INTO WS-EMP-REC
     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.


Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on - 

► Youtube
► Facebook 
► Reddit

Thank you for your support. 
Mainframe Forum™
Created with Artisteer

No comments:

Post a Comment

New In-feed ads