Copybook References Example


Scenario - Using same copybook for two different files.

Copybook -

Student copybook

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION. 
       PROGRAM-ID. DREFCL. 
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01 WS-STDREC1.
          COPY STDREC. 

       01 WS-STDREC2.
          COPY STDREC.

       PROCEDURE DIVISION.

           MOVE 1       TO STD-NO     OF WS-STDREC1.
           MOVE 'NAME1' TO STD-NAME   OF WS-STDREC1.
           MOVE 'MALE'  TO STD-GENDER OF WS-STDREC1.

           MOVE WS-STDREC1  TO WS-STDREC2.

           DISPLAY 'WS-STDREC1:  ' WS-STDREC1.
           DISPLAY 'WS-STDREC2:  ' WS-STDREC2. 

           STOP RUN.

Output -

Data references for copy library output

Explaining Example -

In the above example:

  • STDREC is a copybook added under records WS-STDREC1 and WS-STDREC2.
  • STD-NO, STD-NAME, and STD-GENDER are the variables in the copybook STDREC. These variables are not unique and should have reference record names while used in the program. So, STD-NO under WS-STDREC1 refers as STD-NO OF WS-STDREC1 or STD-NO IN WS-STDREC1. Similarly, references should specify for STD-NAME and STD-GENDER.