COPY Statement
COPY Statement Example
Scenario1 - Insert a copybook into the program.
Copybook - MATEPK.COBOL.COPYLIB(EMPREC)
----+----1----+----2----+----3----+----4----+----5----
05 EMP-NUM PIC 9(05).
05 EMP-NAME PIC X(10).
05 EMP-DESG PIC X(15).
05 EMP-SALARY PIC 9(10).
Code -
----+----1----+----2----+----3----+----4----+----5----
01 EMP-REC.
COPY EMPREC.
Listing after compilation -
----+----1----+----2----+----3----+----4----+----5----
01 EMP-REC.
* COPY EMPREC.
05 EMP-NUM PIC 9(05).
05 EMP-NAME PIC X(10).
05 EMP-DESG PIC X(15).
05 EMP-SALARY PIC 9(10).
Scenario2 - Insert a copybook into the program by replacing EMP by INP.
Copybook - MATEPK.COBOL.COPYLIB(EMPREC)
----+----1----+----2----+----3----+----4----+----5----
05 EMP-NUM PIC 9(05).
05 EMP-NAME PIC X(10).
05 EMP-DESG PIC X(15).
05 EMP-SALARY PIC 9(10).
Code -
----+----1----+----2----+----3----+----4----+----5----
01 EMP-REC.
COPY EMPREC REPLACING "EMP" BY "INP".
Listing after compilation -
----+----1----+----2----+----3----+----4----+----5----
01 EMP-REC.
* COPY EMPREC REPLACING "EMP" BY "INP".
05 INP-NUM PIC 9(05).
05 INP-NAME PIC X(10).
05 INP-DESG PIC X(15).
05 INP-SALARY PIC 9(10).
Scenario3 - Insert a copybook into the program by replacing EMP with delimiter by INPUT.
Copybook - MATEPK.COBOL.COPYLIB(EMPREC)
----+----1----+----2----+----3----+----4----+----5----
01 :EMP:-REC.
05 :EMP:-NUM PIC 9(05).
05 :EMP:-NAME PIC X(10).
05 :EMP:-DESG PIC X(15).
05 :EMP:-SALARY PIC 9(10).
Code -
----+----1----+----2----+----3----+----4----+----5----+
COPY EMPREC REPLACING "==:EMP:==" BY "==INPUT==".
Listing after compilation -
----+----1----+----2----+----3----+----4----+----5----+
* COPY EMPREC REPLACING "==:EMP:==" BY "==INPUT==".
01 INPUT-REC.
05 INPUT-NUM PIC 9(05).
05 INPUT-NAME PIC X(10).
05 INPUT-DESG PIC X(15).
05 INPUT-SALARY PIC 9(10).
Scenario4 - Insert a copybook into the program with 2 replacements.
Copybook - MATEPK.COBOL.COPYLIB(EMPREC)
----+----1----+----2----+----3----+----4----+----5----+
01 EMP-REC.
05 EMP-NUM PIC 9(05).
05 EMP-NAME PIC X(10).
05 EMP-DESG PIC X(15).
05 EMP-SALARY PIC 9(10).
Code -
----+----1----+----2----+----3----+----4----+----5----+
COPY EMPREC REPLACING ==01 EMP-REC== BY ==01 CUST-INP==
==05 EMP== BY ==03 CUST-INP==.
Listing after compilation -
----+----1----+----2----+----3----+----4----+----5----+
* COPY EMPREC REPLACING ==01 EMP-REC== BY ==01 CUST-INP==
* ==05 EMP== BY ==03 CUST-INP==.
01 CUST-INP.
05 CUST-INP-NUM PIC 9(05).
05 CUST-INP-NAME PIC X(10).
05 CUST-INP-DESG PIC X(15).
05 CUST-INP-SALARY PIC 9(10).