Open In App

COBOL – Copy Statement

COPY statement is used to bring into a program a series of prewritten COBOL entries that have been stored in a library. A copy statement inserts the copybook inside a COBOL program, copybook refers to a member which holds all the variables declared inside it. The COPY statement is a library statement that places the prewritten text in a COBOL compilation unit. Prewritten source code entries can be included in a compilation unit at compile time. Thus, an installation can use standard file descriptions, record descriptions, or procedures without recoding them. These entries and procedures can then be saved in user-created libraries; they can then be included in programs and class definitions by means of the COPY statement.

Syntax:



COPY copybook-name
   [REPLACING "string_1" BY "string_2"]

Where,

  1. The copybook-name refers to a file containing the source string.
  2. The string_1 refers to the string to be replaced.
  3. The string_2 refers to the replacing string.

Advantages of using COPY:

Following are the advantages of using the COPY statement in COBOL:



Let us take the example of Copy in COBOL.

Example:




IDENTIFICATION DIVISION.
PROGRAM-ID. DEMO3.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
     
    COPY COPYBOOK.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
     
    ACCEPT ST_ID
    ACCEPT FIRST_NAME
    ACCEPT LAST_NAME
    DISPLAY STUDENT.
    STOP RUN.

The above code is using the below copybook with the help of keyword COPY inside the Working storage section when the COBOL is compiled the below code is inserted in the place of COPY COPYBOOK.




01 STUDENT.
 20 ST_ID         PIC 9(5).
 20 FIRST_NAME    PIC X(20).
 20 LAST_NAME     PIC X(20).

Output:

 


Article Tags :