COBOL VSAM Return Code: Here’s an Example of How to Define It

how to define vsam return codes
[how to define VSAM return codes]

Understanding COBOL and VSAM File Status Codes

COBOL and VSAM file status codes often differ, and interviewers frequently ask about this topic.

How to Define File Status Code for ESDS

In the ENVIRONMENT Division

For an ESDS

SELECT file-name
 ASSIGN TO UT-AS-ddname
 ORGANIZATION IS SEQUENTIAL
 ACCESS MODE IS SEQUENTIAL
 FILE-STATUS IS data-name-1 data-name-2.
 Notes:

Notes

The ORGANIZATION and ACCESS clauses are optional as the default is SEQUENTIAL

Boots does not use the PASSWORD clause.

FILE STATUS Clause Details

The FILE STATUS clause monitors the execution of each input-output request for the file. This optional clause, when you specify it, causes the system to move a value into data-name-1 after each input-output request that explicitly or implicitly refers to this file. You define this field in the Data Division (although not in the File Section) as a two-byte alphanumeric item, and it must not be the object of an OCCURS DEPENDING ON clause.

You can code data-name-2 in VS COBOL II programs to pass native VSAM return code information to the COBOL program. Define this field as a group item of 6 bytes in the Working-Storage Section or Linkage Section of the Data Division. The first 2 bytes of data-name-2 hold the VSAM return code in binary notation, which can take a value of 0, 8, or 12. The next 2 bytes hold the VSAM function code in binary notation, with a value of 1, 2, 3, 4, or 5.
The last 2 bytes of data-name-2 hold the VSAM feedback code, again in binary, with a value from 0 through 255.

The system sets the Function Code and Feedback Code only when the Return Code is non-zero. If it sets them while the Return Code is zero, you cannot rely on the contents of those fields.

Example Code

Example
SELECT VSAM-MASTER ASSIGN TO UT-AS-VSMAST
FILE STATUS IS STATUS-CODE VSAM-RETURN-CODE.

WORKING-STORAGE SECTION.
01 STATUS-CODE PIC XX.
01 VSAM-RETURN-CODE.
   05 VSAM-R15-RETURN-CODE PIC S99 COMP.
   05 VSAM-FUNCTION-CODE   PIC S9 COMP.
   05 VSAM-FEEDBACK-CODE   PIC S999 COMP

Also Read