0% found this document useful (0 votes)
208 views

Cobol

COBOL is a structured programming language used to develop business applications. It has English-like elements and is close to English in readability. A COBOL program is divided into four main divisions: identification, environment, data, and procedure. The procedure division contains the program logic and uses statements like DISPLAY, ACCEPT, MOVE, COMPUTE, and STOP RUN to process data and terminate the program.

Uploaded by

Mahesh Ketkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
208 views

Cobol

COBOL is a structured programming language used to develop business applications. It has English-like elements and is close to English in readability. A COBOL program is divided into four main divisions: identification, environment, data, and procedure. The procedure division contains the program logic and uses statements like DISPLAY, ACCEPT, MOVE, COMPUTE, and STOP RUN to process data and terminate the program.

Uploaded by

Mahesh Ketkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 105

COBOL Programming

COBOL

COBOL stands for Common Business Oriented Language. It is a


structured language used to develop business applications. COBOL
instruction set is close to English which makes the program more
readable. It contains English like elements – verbs, sentences,
statements, sections, divisions. COBOL applications deals with
large volumes of data which are stored in file based systems or
database systems.

A COBOL program is divided into the following 4 divisions :


1. IDENTIFICATION DIVISION
2. ENVIRONMENT DIVISION.
3. DATA DIVISION.
4. PROCEDURE DIVISION.

IDENTIFICATION DIVISION
This division contains the in-memory name of the program, author
name, date written and date compiled. The in-memory name of the
program is mandatory.

ENVIRONMENT DIVISION.
This division has two sections: INPUT-OUTPUT SECTION and
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION has attributes of input and output files
used by the program.
CONFIGURATION SECTION has the name of source computer and object
computer used for compiling and executing the program.

DATA DIVISION.
This division has 3 sections: FILE SECTION, WORKING-STORAGE
SECTION and LINKAGE SECTION.
FILE SECTION defines the structure of the file.
WORKING-STORAGE SECTION defines the variables used in a program.
LINKAGE SECTION defines the variables received from the main
program.

PROCEDURE DIVISION.
This division contains the program logic.

_____________________________________________________________________________________
PAGE 1
COBOL Programming

Operators

ARITHMETICAL OPERATORS

+ ADDITION
- SUBTRACTION
* MULTIPLICATION
/ DIVISION
** EXPONENTIATION

RELATIONAL OPERATORS

< IS LESS THAN


> IS GREATER THAN
<= IS LESS THAN OR EQUAL TO
>= IS GREATER THAN OR EQUAL TO
= IS EQUAL TO
NOT = NOT EQUAL TO

LOGICAL OPERATORS

AND
OR
NOT

_____________________________________________________________________________________
PAGE 2
COBOL Programming

Variable Declarations

In COBOL, variables are declared using level numbers. Valid


level numbers in COBOL are 01, 02 to 49, 66, 77 and 88.

01, 02 to 49 level numbers

Syntax:
01 record-name.
05 child-element PIC data-type(width).
05 child-element PIC data-type(width).

Example 1:
01 WS-EMPLOYEE-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-SALARY PIC 9(06).

Example 2:
01 WS-EMPLOYEE-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME.
10 WS-F-NAME PIC X(10).
10 WS-M-NAME PIC X(10).
10 WS-S-NAME PIC X(10).
05 WS-SALARY PIC 9(06).

Points:
1. Name specified at the 01 level is called as Record name.
2. There are 3 data types in COBOL :
9 – Numeric, X – Alphanumeric, A - Alphabetic
3. Child element can have any level number from 02 to 49.
4. Level number of the child element must be higher than that
of the parent.
5. Parent element should not have a PIC clause. The data type
of the parent is assumed to be alphanumeric.
6. Child element must have a PIC clause and can have an
optional VALUE clause.
7. Record name or parent element is also known as a group
item.
8. Child element is also known as an elementary item.

_____________________________________________________________________________________
PAGE 3
COBOL Programming

66 level – RENAMES clause

Syntax:
01 record-name.
05 child-1 PIC data-type(width).
05 child-2 PIC data-type(width).
05 child-3 PIC data-type(width).
05 child-4 PIC data-type(width).
05 child-5 PIC data-type(width).
66 user-name RENAMES child-3 thru child-5.
05 child-6 PIC data-type(width).

Example:
01 WS-EMPLOYEE-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-ADD1 PIC X(10).
05 WS-ADD2 PIC X(10).
05 WS-CITY PIC X(10).
05 WS-PIN PIC 9(06).
66 ADDRESS RENAMES WS-ADD1 THRU WS-PIN.
05 WS-PHONE PIC 9(06).

RENAMES clause renames a subset of child items with a logical


name.

_____________________________________________________________________________________
PAGE 4
COBOL Programming

77 level number - Elementary Items.

Syntax:
77 variable-name PIC data-type(width) VALUE value.

Example:
77 WS-NAME PIC X(10) VALUE SPACES.
77 WS-EMPNO PIC 9(03) VALUE 0.

Points:
1. VALUE clause is used to initialize the elementary item.
VALUE clause is optional.
2. Variables declared at 77 level cannot be sub-divided.

_____________________________________________________________________________________
PAGE 5
COBOL Programming

88 level number – Conditional Names.

Syntax 1:
88 conditional-name VALUE value.
88 conditional-name VALUE value1 thru value2.

Example:
77 WS-OPTION PIC 9(01).
88 VALID-OPTION VALUE 1 THRU 5.
88 INVALID-OPTION VALUE 6.


IF VALID-OPTION
DISPLAY “OPTION IS VALID”
ELSE
DISPLAY “OPTION IS INVALID”
END-IF.

Points:
1. Conditional names are used to replace a condition in IF,
EVALUATE, PERFORM UNTIL statements.
2. One or more conditional names can be defined for a
variable.
3. To define a conditional name in WORKING-STORAGE SECTION, it
must be coded immediately below the variable for which the
conditional name is to be given.
4. Conditional name can represent one value, multiple values
or a range of values.

_____________________________________________________________________________________
PAGE 6
COBOL Programming

INPUT-OUTPUT STATEMENTS

DISPLAY statement

Syntax:
DISPLAY “message” / variable.

Example:
DISPLAY “HELLO”.
DISPLAY “SALARY =”, WS-SALARY.

ACCEPT statement

Syntax:
ACCEPT variable.

Example:
ACCEPT WS-NAME.

Points:
1. DISPLAY command is used to display a message or variable
contents in the spool.
2. ACCEPT command is used to receive input in a program. The
data to be received must be given in SYSIN in RUN jcl.
3. More than one variables can be displayed using DISPLAY
command. Each of the variables must be comma separated.
4. Only one variable can be specified with ACCEPT command.

_____________________________________________________________________________________
PAGE 7
COBOL Programming

PROGRAM TERMINATION COMMANDS

Syntax:
STOP RUN.

Syntax:
EXIT PROGRAM.

Syntax:
GOBACK.

Points:
1. STOP RUN command must be given in the main program. It will
take the control out of the program.
2. EXIT PROGRAM command must be given in the sub program. It
will return the control to the main program from the sub
program.
3. GOBACK command can be given either in the main program or
sub program. If given in the main program it will take the
control out of the program. If given in the sub program, it
will return the control to the main program from the sub
program.
4. One program can have any number of STOP RUN, GOBACK and
EXIT PROGRAM commands.

_____________________________________________________________________________________
PAGE 8
COBOL Programming

ASSIGNMENT/ CALCULATION statements

MOVE statement

Syntax:
MOVE variable-1 TO variable-2.

Example:
MOVE 5 TO WS-NUM.
MOVE “HELLO” TO WS-STRING.

COMPUTE statement

Syntax:
COMPUTE variable = expression
ON SIZE ERROR
statement-1
NOT ON SIZE ERROR
statement-2
END-COMPUTE.

Example:
COMPUTE WS-BALANCE = WS-BALANCE + WS-DEPAMT
ON SIZE ERROR
DISPLAY “BALANCE AMOUNT EXCEEDS THE LIMIT”
NOT ON SIZE ERROR
DISPLAY “BALANCE =”, WS-BALANCE
END-COMPUTE.

ADD statement

Syntax:
ADD variable-1/value-1 to variable-2.

Example:
ADD WS-DEPAMT TO WS-BALANCE.
ADD 500 TO WS-BALANCE.

_____________________________________________________________________________________
PAGE 9
COBOL Programming

SUBTRACT statement

Syntax:
SUBTRACT variable-1/value-1 FROM variable-2.

Example:
SUBTRACT WS-WITHAMT FROM WS-BALANCE.
SUBTRACT 300 FROM WS-BALANCE.

MULTIPLY statement

Syntax:
MULTIPLY variable-1 BY variable-2/value-1.

Example:
MULTIPLY WS-NUM1 BY WS-NUM2.
MULTIPLY WS-NUM BY 2.

DIVIDE statement

Syntax:
DIVIDE variable-1 BY variable-2 GIVING variable-3
REMAINDER variable-4.

Example:
DIVIDE WS-YEAR BY 4 GIVING WS-QUOT REMAINDER WS-REM.

_____________________________________________________________________________________
PAGE 10
COBOL Programming

Program: Program to calculate total and percentage of a student.

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 WS-ROLLNO PIC 9(03) VALUE 0.
77 WS-NAME PIC X(10) VALUE SPACES.
77 WS-MKS1 PIC 9(02) VALUE 0.
77 WS-MKS2 PIC 9(02) VALUE 0.
77 WS-MKS3 PIC 9(02) VALUE 0.
77 WS-TOTAL PIC 9(03) VALUE 0.
77 WS-PERCENT PIC 9(02) VALUE 0.
PROCEDURE DIVISION.
MAIN-PARA.
ACCEPT WS-ROLLNO.
ACCEPT WS-NAME.
ACCEPT WS-MKS1.
ACCEPT WS-MKS2.
ACCEPT WS-MKS3.
COMPUTE WS-TOTAL = WS-MKS1 + WS-MKS2 + WS-MKS3.
COMPUTE WS-PERCENT = WS-TOTAL / 3.
DISPLAY “TOTAL :”, WS-TOTAL.
DISPLAY “PERCENT :”, WS-PERCENT.
STOP RUN.

The above program accepts roll number, name and marks in 3


subjects. It then calculates the total and percentage and
displays the total and percentage of the student.

_____________________________________________________________________________________
PAGE 11
COBOL Programming

FIGURATIVE CONSTANTS

Figurative constant represents a fixed value in COBOL.


Following are the figurative constants used in a COBOL program.

ZERO/ ZEROS/ ZEROES

SPACE/ SPACES

LOW-VALUE

HIGH-VALUE

QUOTE/ QUOTES

ALL

Examples:

77 WS-NUM PIC 9(03) VALUE ZERO.


77 WS-NAME PIC X(10) VALUE SPACES.
77 WS-STRING PIC X(80) VALUE ALL “*”.

77 WS-FLAG PIC X(03) VALUE SPACES.




MOVE LOW-VALUE TO WS-FLAG.

_____________________________________________________________________________________
PAGE 12
COBOL Programming

FILLER

FILLER is used to when we do not want to give a name to a


field or a variable. It represents a block of data without a
formal name. FILLER term can be used in the DATA DIVISION. It
cannot be used in PROCEDURE DIVISION.

Example:

01 WS-EMPLOYEE-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(07).
05 FILLER PIC X(50).

If the FILLER term is omitted in the statement, it is


assumed to be a FILLER.

_____________________________________________________________________________________
PAGE 13
COBOL Programming

IF statement

IF statement

Syntax:
IF condition
Statement-1
END-IF.
Statement-2.

Example:
COMPUTE WS-BALANCE = WS-BALANCE – WS-DEP-AMT.
IF WS-BALANCE = 0
DISPLAY “ZERO BALANCE”
END-IF.
DISPLAY “BALANCE =”, WS-BALANCE.

IF ELSE statement

Syntax:
IF condition
Statement-1
Statement-2
ELSE
Statement-3
END-IF.
Statement-4.

Example:
IF WS-NUM1 > WS-NUM2
MOVE WS-NUM1 TO WS-MAX
ELSE
MOVE WS-NUM2 TO WS-MAX
END-IF.

_____________________________________________________________________________________
PAGE 14
COBOL Programming

CLASS CONDITIONS

Class condition is used to check if the contents of a


variable is numeric, alphabetic, alphabetic-lower, alphabetic-
upper. Class conditions can be used in a program if you want to
validate the data in the variable in terms of data type before
using the variable for processing.

Following are the class conditions in COBOL :

NUMERIC
ALPHABETIC
ALPHABETIC-LOWER
ALPHABETIC-UPPER

Example:

77 WS-STRING PIC X(05) VALUE “ABCDE”.




IF WS-STRING IS NUMERIC
DISPLAY “CONTAINS NUMBERS”
ELSE
IF WS-STRING IS ALPHABETIC
DISPLAY “CONTAINS ALPHABETS”
ELSE
DISPLAY “CONTAINS ALPHANUMERIC CHARACTERS”
END-IF
END-IF.

_____________________________________________________________________________________
PAGE 15
COBOL Programming

EVALUATE statement

EVALUATE statement

Syntax 1:
EVALUATE variable
WHEN value-1
Statement-1
WHEN value-2
Statement-2
WHEN value-3
Statement-3
WHEN OTHER
Statement-4
END-EVALUATE.
Statement-5.

Example:
EVALUATE WS-OPTION
WHEN 1
DISPLAY “OPTION 1 SELECTED”
WHEN 2
DISPLAY “OPTION 2 SELECTED”
WHEN 3
DISPLAY “OPTION 3 SELECTED”
WHEN OTHER
DISPLAY “INVALID OPTION”
END-EVALUATE.
DISPLAY “OVER”.

_____________________________________________________________________________________
PAGE 16
COBOL Programming

Syntax 2:
EVALUATE TRUE
WHEN condition-1
Statement-1
WHEN condition-2
Statement-2
WHEN OTHER
Statement-4
END-EVALUATE.
Statement-5.

Example:
EVALUATE TRUE
WHEN PER < 35
DISPLAY “FAIL”
WHEN PER >= 35 AND PER < 50
DISPLAY “THIRD GRADE”
WHEN PER >= 50 AND PER < 60
DISPLAY “SECOND GRADE”
WHEN PER >= 60 AND PER < 75
DISPLAY “FIRST GRADE”
WHEN OTHER
DISPLAY “DISTINCTION”
END-EVALUATE.
DISPLAY “PGM OVER”.

_____________________________________________________________________________________
PAGE 17
COBOL Programming

PERFORM statement

PERFORM statement.

Syntax:
PERFORM para-name.

Example:
MAIN-PARA.
DISPLAY “HELLO”.
PERFORM PARA-1.
DISPLAY “BYE”.
STOP RUN.
PARA-1.
DISPLAY “IN PARA-1”.

PERFORM .. TIMES

Syntax:
PERFORM para-name num TIMES.

Example:
MAIN-PARA.
DISPLAY “HELLO”.
PERFORM PARA-1 3 TIMES.
DISPLAY “BYE”.
STOP RUN.
PARA-1.
DISPLAY “IN PARA-1”.

Points:
1. PERFORM statement is used to make the program logic
modular.
2. PERFORM para-1 statement will transfer the control to para-
1, execute all the statements of para-1 and then return
back and execute the next statement after PERFORM para-1
statement.
3. The TIMES option will execute the paragraph number of times
mentioned before TIMES.

_____________________________________________________________________________________
PAGE 18
COBOL Programming

PERFORM .. UNTIL

Syntax:
PERFORM para-name UNTIL condition.

Example:
MAIN-PARA.
DISPLAY “HELLO”.
MOVE 1 TO WS-CTR.
PERFORM PARA-1 UNTIL WS-CTR = 5.
DISPLAY “BYE”.
STOP RUN.
PARA-1.
DISPLAY “IN PARA-1”.
ADD 1 TO WS-CTR.

Points:
1. The above command will keep executing the statements of the
paragraph as long as the condition does not satisfy. The
moment the condition satisfies, the control will pass to
the next statement after PERFORM UNTIL statement.

_____________________________________________________________________________________
PAGE 19
COBOL Programming

PERFORM .. VARYING .. UNTIL

Syntax:
PERFORM para-name VARYING variable
FROM initial-value BY step-value
UNTIL condition.

Example:
MAIN-PARA.
DISPLAY “HELLO”.
PERFORM PARA-1 WS-CTR FROM 1 BY 1 UNTIL WS-CTR = 5.
DISPLAY “BYE”.
STOP RUN.
PARA-1.
DISPLAY “IN PARA-1”.

Points:
1. The above PERFORM statement will keep executing the
statements of the paragraph as long as the condition does
not satisfy. The moment the condition satisfies, the
control will be passed to the next statement after PERFORM
VARYING UNTIL statement.
2. FROM clause in the statement initializes the variable with
the value specified after FROM. This clause is executed
before checking the condition for the first time.
3. BY clause will increment/decrement the variable by the
value specified after BY. This clause is executed after
executing all the statements of the paragraph.
4. After the BY clause is executed, COBOL will check the
condition specified after UNTIL clause. If the condition
does not satisfy, it will execute the statements of the
paragraph mentioned with the PERFORM VARYING UNTIL
statement. If the condition does not satisfy, the control
will be passed to the next statement after PERFORM VARYING
UNTIL statement.

_____________________________________________________________________________________
PAGE 20
COBOL Programming

PERFORM .. THRU

Syntax:
PERFORM para-name-1 THRU para-name-2.

Example:
MAIN-PARA.
DISPLAY “HELLO”
PERFORM ACCEPT-PARA THRU DISPLAY-PARA.
DISPLAY “PROGRAM OVER”.
STOP RUN.
ACCEPT-PARA.
DISPLAY “IN ACCEPT-PARA”.
VALIDATE-PARA.
DISPLAY “IN VALIDATE-PARA”.
COMPUTE-PARA.
DISPLAY “IN COMPUTE-PARA”.
DISPLAY-PARA.
DISPLAY “IN DISPLAY-PARA”.

Points:
1. PERFORM .. THRU will execute all the paragraphs beginning
from para-name-1 to para-name-2 in the sequence of the
paragraphs.
2. A new paragraph can be inserted in between the paragraphs
at the desired position without altering the PERFORM
statement.

_____________________________________________________________________________________
PAGE 21
COBOL Programming

EDIT characters

Edit characters are used to format the data and display the
formatted data. The following are some of the edit characters in
COBOL :

Z : replace leading 0 with a space


* : replace leading 0 with an *
, : display , at the designated position
. : display . at the designated position
+ : display a + before positive number and
– before a negative number

For displaying the formatted data, a variable needs to be


declared containing any of the edit characters in the PIC
clause. Such a variable is called as an edit variable.

Then the variable which contains the data to be formatted must


be moved to the edit variable and then the edit variable must be
displayed.

Example:

77 WS-NUM PIC 9(07) VALUE 123.


77 WS-E1-NUM PIC ZZZ9999.
77 WS-E2-NUM PIC ZZ,Z9,999.
77 WS-E3-NUM PIC ZZ,Z9,999.00.


MOVE WS-NUM TO WS-E1-NUM.
MOVE WS-NUM TO WS-E2-NUM.
MOVE WS-NUM TO WS-E3-NUM.
DISPLAY WS-E1-NUM.
DISPLAY WS-E2-NUM.
DISPLAY WS-E3-NUM.

The following points must be noted while using edit variables :


01.Edit variables must not have a VALUE clause at declaration.
02.Edit variables cannot take part in an arithmetical
operation.

_____________________________________________________________________________________
PAGE 22
COBOL Programming

03.Result of an arithmetical operation can be assigned to an


edit variable.
Signed variable

Signed variable is declared by typing S after PIC clause in


declaration. A signed variable can contain a positive value, a
negative value or an unsigned value. This sign is not included
in the width of the variable.

The following table is used by COBOL for Signed variable :

Digit Positive Negative Unsigned


0 { } 0
1 A J 1
2 B K 2
3 C L 3
4 D M 4
5 E N 5
6 F O 6
7 G P 7
8 H Q 8
9 I R 9

If the number assigned to the signed variable is a positive


number, it will refer to Positive column. If the number assigned
to the signed variable is a negative number, it will refer to
Negative column. If the number assigned to a signed variable is
an unsigned number, it will refer to the Unsigned column.

Based on the number assigned to the signed variable, it


will search the least significant digit of the number in the
Digits column and replace the sign (+ -) and the least
significant digit of the number with the character mentioned in
the selected column corresponding to the digit. This character
will replace the last digit of the number

Examples:
77 WS-NUM1 PIC 9(03) VALUE 457. => 457
77 WS-NUM2 PIC S9(03) VALUE +438. => 43H
77 WS-NUM3 PIC S9(03) VALUE -890. => 89}

_____________________________________________________________________________________
PAGE 23
COBOL Programming

TABLE HANDLING

Introduction
1. A table is defined as a group of locations storing data of
the same type.

2. Location of a table starts from 1.

3. OCCURS clause in the program defines the number of


locations of the table.

4. If T is a table containing 5 locations and if WS-LOCN is 3,


T(WS-LOCN) will refer to the data in the 3rd location in the
table.

5. Expression is not allowed in brackets i.e. T(WS-LOCN + 1).

6. Table can be single dimensional or multi-dimensional.


Depending on the compiler, we can have up to 7 dimensions
in a table.

7. Tables are of two types : Sequential Tables and Indexed


Tables.

_____________________________________________________________________________________
PAGE 24
COBOL Programming

Sequential Tables
Syntax:
01 name.
05 table-name OCCURS size TIMES.
10 data-element-1 PIC data-type(width).
10 data-element-2 PIC data-type(width).
10 data-element-3 PIC data-type(width).
10 data-element-4 PIC data-type(width).

Example:
01 TABLE-1.
05 EMPLOYEE-TABLE OCCURS 5 TIMES.
10 T-EMPNO PIC 9(03).
10 T-NAME PIC X(10).
10 T-LOCN PIC X(10).
10 T-SALARY PIC 9(06).

_____________________________________________________________________________________
PAGE 25
COBOL Programming

Program: To Input data in the table and display the table.

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TABLE-1.
05 EMPLOYEE-TABLE OCCURS 5 TIMES.
10 T-EMPNO PIC 9(03).
10 T-NAME PIC X(10).
10 T-LOCN PIC X(10).
10 T-SALARY PIC 9(06).
77 WS-LOCN PIC 9(01) VALUE 1.
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM INPUT-PARA 5 TIMES.
MOVE 1 TO WS-LOCN.
PERFORM DISPLAY-PARA 5 TIMES.
STOP RUN.
*-----------------------------------------------------------
INPUT-PARA.
ACCEPT T-EMPNO(WS-LOCN).
ACCEPT T-NAME(WS-LOCN).
ACCEPT T-LOCN(WS-LOCN).
ACCEPT T-SALARY(WS-LOCN).
ADD 1 TO WS-LOCN.
*-----------------------------------------------------------
DISPLAY-PARA.
DISPLAY T-EMPNO(WS-LOCN), “:”, T-NAME(WS-LOCN), “:”,
T-LOCN(WS-LOCN), “:”, T-SALARY(WS-LOCN).
ADD 1 TO WS-LOCN.
*-----------------------------------------------------------

The above program defines a one dimensional table to store


employee number, name, location and salary details of 5
employees. The PROCEDURE DIVISION accepts the data of 5
employees (INPUT-PARA) and then displays the data of 5 employees
(DISPLAY-PARA).

_____________________________________________________________________________________
PAGE 26
COBOL Programming

INDEXED TABLES

Syntax:
01 name.
05 table-name OCCURS size TIMES INDEXED BY index.
10 data-element-1 PIC data-type(width).
10 data-element-2 PIC data-type(width).
10 data-element-3 PIC data-type(width).
10 data-element-4 PIC data-type(width).

Example:
01 TABLE-1.
05 EMPLOYEE-TABLE OCCURS 5 TIMES INDEXED BY IDX.
10 T-EMPNO PIC 9(03).
10 T-NAME PIC X(10).
10 T-LOCN PIC X(10).
10 T-SALARY PIC 9(06).

_____________________________________________________________________________________
PAGE 27
COBOL Programming

Program: To input data in an indexed table and display the table


contents.

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TABLE-1.
05 EMPLOYEE-TABLE OCCURS 5 TIMES INDEXED BY IDX.
10 T-EMPNO PIC 9(03).
10 T-NAME PIC X(10).
10 T-LOCN PIC X(10).
10 T-SALARY PIC 9(06).
77 WS-LOCN PIC 9(01) VALUE 1.
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM INPUT-PARA 5 TIMES.
SET IDX TO 1.
PERFORM DISPLAY-PARA 5 TIMES.
STOP RUN.
*-----------------------------------------------------------
INPUT-PARA.
ACCEPT T-EMPNO(WS-LOCN).
ACCEPT T-NAME(WS-LOCN).
ACCEPT T-LOCN(WS-LOCN).
ACCEPT T-SALARY(WS-LOCN).
ADD 1 TO WS-LOCN.
*-----------------------------------------------------------
DISPLAY-PARA.
DISPLAY T-EMPNO(IDX), “:”, T-NAME(IDX), “:”,
T-LOCN(IDX), “:”, T-SALARY(IDX).
SET IDX UP BY 1.
*-----------------------------------------------------------

The above program defines an indexed table to store employee


number, name, location and salary of 5 employees. The PROCEDURE
DIVISION accepts the details of the 5 employees (ACCEPT-PARA)
and then sequentially displays the details of the 5 employees
(DISPLAY-PARA) using the index.

_____________________________________________________________________________________
PAGE 28
COBOL Programming

SEARCH command

Syntax:
01 name.
05 table-name OCCURS size TIMES INDEXED BY index.
10 data-element-1 PIC data-type(width).
10 data-element-2 PIC data-type(width).
10 data-element-3 PIC data-type(width).
10 data-element-4 PIC data-type(width).

SEARCH 01-level-table-name
AT END
Statement-1
WHEN condition
Statement-2
END-SEARCH.
Statement-3.

Points:
1. SEARCH command is used to search an element in an indexed
table.
2. If the condition specified with WHEN satisfies any of the
location in the table, statement-2 will get executed and
the control will be passed to statement-3.
3. If the condition specified does not satisfy any location in
the table, statement-1 will get executed and the control
will be passed to statement-3.
4. Index must be used in the condition in SEARCH command since
SEARCH command searches based on the index.
5. WHEN clause in SEARCH can be typed as
WHEN variable = table-element OR
WHEN table-element = variable
6. Any relational operators can be used in WHEN.
7. One SEARCH command can have more than one WHEN clauses.

_____________________________________________________________________________________
PAGE 29
COBOL Programming

Program: To search an element in an indexed table using SEARCH.

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TABLE-1.
05 EMPLOYEE-TABLE OCCURS 5 TIMES INDEXED BY IDX.
10 T-EMPNO PIC 9(03).
10 T-NAME PIC X(10).
10 T-LOCN PIC X(10).
10 T-SALARY PIC 9(06).
77 WS-LOCN PIC 9(01) VALUE 1.
77 WS-CODE PIC 9(03) VALUE 0.
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM INPUT-PARA 5 TIMES.
SET IDX TO 1.
PERFORM SEARCH-PARA.
STOP RUN.
*-----------------------------------------------------------
INPUT-PARA.
ACCEPT T-EMPNO(WS-LOCN).
ACCEPT T-NAME(WS-LOCN).
ACCEPT T-LOCN(WS-LOCN).
ACCEPT T-SALARY(WS-LOCN).
ADD 1 TO WS-LOCN.
*-----------------------------------------------------------
SEARCH-PARA.
SET IDX TO 1.
ACCEPT WS-CODE.
SEARCH EMPLOYEE-TABLE
AT END
DISPLAY “SEARCH FAILED”
WHEN WS-CODE = T-EMPNO(IDX)
DISPLAY T-NAME(IDX), “:”, T-SALARY(IDX)
END-SEARCH.

The above program accepts employee details of 5 employees in an


indexed table. It then accepts an employee number in WS-CODE and

_____________________________________________________________________________________
PAGE 30
COBOL Programming

uses SEARCH command to search WS-CODE in the table using


sequential search.

SEARCH ALL command

Syntax:
01 name.
05 table-name OCCURS n TIMES
ASCENDING/DESCENDING IS data-element1
INDEXED BY index.
10 data-element1 PIC data-type(width).
10 data-element2 PIC data-type(width).
10 data-element3 PIC data-type(width).
10 data-element4 PIC data-type(width).


SEARCH ALL 01-level-table-name
AT END
Statement-1
WHEN condition
Statement-2
END-SEARCH.
Statement-3.

Points:
1. SEARCH ALL command is used to search an element using
binary search technique in an indexed table.
2. If the condition specified with WHEN satisfies any of the
location in the table, statement-2 will get executed and
the control will be passed to statement-3.
3. If the condition specified does not satisfy any location in
the table, statement-1 will get executed and the control
will be passed to statement-3.
4. Index must be used in the condition in SEARCH ALL command
since SEARCH command searches on the index.
5. WHEN clause in SEARCH ALL must be typed as
WHEN table-element = variable
6. SEARCH ALL can have only one WHEN clause.
7. Only = operator can be used for comparison in WHEN clause.

_____________________________________________________________________________________
PAGE 31
COBOL Programming

Program: To search in an indexed table using SEARCH ALL.

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TABLE-1.
05 EMPLOYEE-TABLE OCCURS 5 TIMES
ASCENDING KEY IS T-EMPNO INDEXED BY IDX.
10 T-EMPNO PIC 9(03).
10 T-NAME PIC X(10).
10 T-LOCN PIC X(10).
10 T-SALARY PIC 9(06).
77 WS-LOCN PIC 9(01) VALUE 1.
77 WS-CODE PIC 9(03) VALUE 0.
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM INPUT-PARA 5 TIMES.
SET IDX TO 1.
PERFORM SEARCH-PARA.
STOP RUN.
*-----------------------------------------------------------
INPUT-PARA.
ACCEPT T-EMPNO(WS-LOCN).
ACCEPT T-NAME(WS-LOCN).
ACCEPT T-LOCN(WS-LOCN).
ACCEPT T-SALARY(WS-LOCN).
ADD 1 TO WS-LOCN.
*-----------------------------------------------------------
SEARCH-PARA.
ACCEPT WS-CODE.
SEARCH ALL EMPLOYEE-TABLE
AT END
DISPLAY “SEARCH FAILED”
WHEN T-EMPNO(IDX) = WS-CODE
DISPLAY T-NAME(IDX), “:”, T-SALARY(IDX)
END-SEARCH.

The above program accepts employee details 5 employees in an


indexed table. It then accepts the employee number (WS-CODE) to

_____________________________________________________________________________________
PAGE 32
COBOL Programming

be searched in the table and uses SEARCH ALL command to search


the employee number in the table using binary search.
Index v/s Subscript

Index
01.Index is used with indexed tables for search operation.
02.Index must not be declared in WORKING-STORAGE SECTION.
03.Index represents the displacement value (address) of a
table element.
04.Index is unique to each table.
05.Index value cannot be displayed in a program.
06.SET command is used to assign a value to an index.
SET IDX TO 1.
07.SET .. UP BY/DOWN BY is used to add/ subtract a value from
an index.
SET IDX UP BY 1.
SET IDX DOWN BY 1.
08.Index must be used in SEARCH/ SEARCH ALL command.

Subscript
01.Subscript can be used with sequential tables for search
operation.
02.Subscript must be declared in WORKING-STORAGE SECTION.
03.Subscript represents a location number of a table element.
04.Subscript can be shared with multiple tables.
05.Subscript value can be displayed in a program.
06.MOVE command is used to assign a value to a subscript.
MOVE 1 TO WS-SUB.
07.ADD/SUBTRACT/COMPUTE is used to add/subtract a value from a
subscript.
ADD 1 TO WS-SUB.
SUBTRACT 1 FROM WS-SUB.
08.Subscript should not be used in SEARCH/ SEARCH ALL command.

_____________________________________________________________________________________
PAGE 33
COBOL Programming

FILE HANDLING

Points:
 Files can be used in a COBOL program to read, write, update
or delete records.
 COBOL program can perform file operations on VSAM files
(KSDS, ESDS, RRDS) and non-VSAM files (PS).
 Before performing an operation on a file, it must be opened
in the specific mode in the program. After performing an
operation on a file, it must be closed in the program.
 A record can be inserted, appended, read, updated and
deleted in a KSDS.
 A record can be appended, read and updated in an ESDS or a
PS. A record cannot be inserted or deleted in an ESDS or a
PS.
 A record can be inserted, appended, read, updated and
deleted in a RRDS.
 OPEN command is used to open a file.
 CLOSE command is used to close a file.
 WRITE command is used to write a record in a file.
 READ command is used to read a record from a file.
 REWRITE command is used to update a record in a file.
 DELETE command is used to delete a record in a file.
 In a COBOL program, attributes of the file must be
mentioned in INPUT-OUTPUT SECTION of ENVIRONMENT DIVISION.
Structure of the file must be mentioned in FILE SECTION of
DATA DIVISION.

_____________________________________________________________________________________
PAGE 34
COBOL Programming

KSDS FILE COMMANDS

ENVIRONMENT DIVISION entries

Syntax:
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT logical-file-name ASSIGN TO ddname
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL/ RANDOM/ DYNAMIC
RECORD KEY IS fs-primary-key
ALTERNATE RECORD KEY IS fs-alt-key WITH DUPLICATES
FILE STATUS IS WS-FILE-STATUS.

Example:
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS FS-EMPNO
ALTERNATE RECORD KEY IS FS-LOCN WITH DUPLICATES
FILE STATUS IS WS-FILE-STATUS.

File opening Modes

Before any operation is performed on a data file, the file must


be opened. Following are the modes in which KSDS can be opened.

 OUTPUT mode is used to write a record in a KSDS cluster for


the first time. Access mode can be SEQUENTIAL or RANDOM.

 EXTEND mode is used to add a record in a KSDS cluster.


Access mode must be SEQUENTIAL.

 I-O mode is used to insert, update or delete a record in a


KSDS cluster. Access mode can be RANDOM or DYNAMIC.

_____________________________________________________________________________________
PAGE 35
COBOL Programming

 INPUT is used to read a record from a KSDS cluster. Access


mode can be SEQUENTIAL or RANDOM or DYNAMIC
OPEN statement

Syntax:
OPEN mode logical-file-name.

Example:
OPEN INPUT FILE-1.
IF WS-FILE-STATUS = “00”
DISPLAY “OPEN SUCCESS”
ELSE
DISPLAY “OPEN FAILED”
END-IF.

CLOSE statement

Syntax:
CLOSE logical-file-name.

Example:
CLOSE FILE-1.
IF WS-FILE-STATUS = “00”
DISPLAY “OPEN SUCCESS”
ELSE
DISPLAY “OPEN FAILED”
END-IF.

Points:
1. Logical-file-name is associated with a ddname in SELECT
clause in ENVIRONMENT DIVISION. The physical-file-name is
associate with the same ddname in RUN JCL. The ddname is a
link between the logical-file-name and physical-file-name.

2. WS-FILE-STATUS is a variable declared in FILE STATUS IS


clause of the file in ENVIRONMENT DIVISION. It must be
declared as X(02) in WORKING-STORAGE SECTION.

3. WS-FILE-STATUS will contain the execution status of a file


operation : OPEN, CLOSE, READ, WRITE, REWRITE, DELETE,
START. If the file operation is successful, FILE-STATUS-
_____________________________________________________________________________________
PAGE 36
COBOL Programming

VARIABLE will have a value “00”. If the file operation is


not successful, WS-FILE-STATUS will have non-zero value.

WRITE statement

Syntax 1:
WRITE fs-record FROM ws-record
INVALID KEY
Statement-1
NOT INVALID KEY
Statement-2
END-WRITE.
Statement-3.

Example:
WRITE FS-EMP-RECORD FROM WS-EMP-RECORD
INVALID KEY
DISPLAY “WRITE ERRROR”, WS-FILE-STATUS
NOT INVALID KEY
DISPLAY “WRITE SUCCESSFUL”
END-WRITE.

Points:
1. WRITE command will first move the data from ws-record to
fs-record and then write the data from fs-record to the
file.

2. If the WRITE command is successful, it will enter NOT


INVALID KEY block, execute statement-2 and the control will
be passed to statement-3.

3. If the WRITE command is not successful, it will enter


INVALID KEY block, execute statement-1 and the control will
be passed to statement-3.

4. ACCESS MODE in ENVIRONMENT DIVISION must be RANDOM.

5. File must be opened in OUTPUT or I-O mode.

6. With this WRITE syntax, a record can be written for the


first time (OUTPUT mode) or it can be inserted/appended (I-
O mode) in a KSDS cluster.

_____________________________________________________________________________________
PAGE 37
COBOL Programming

_____________________________________________________________________________________
PAGE 38
COBOL Programming

Syntax 2:
WRITE fs-record FROM ws-record.
IF file-status-variable = “00”
Statement-1
ELSE
Statement-2
END-IF.
Statement-3.

Example:
WRITE FS-EMP-RECORD FROM WS-EMP-RECORD.
IF WS-FILE-STATUS = “00”
DISPLAY “WRITE SUCCESSFUL”
ELSE
DISPLAY “WRITE ERRROR”, WS-FILE-STATUS
END-IF.

Points:
1. WRITE command will first move the data from ws-record to
fs-record and then write the data from fs-record to the
file.

2. If the WRITE command is successful, file status variable


will be “00”, statement-1 will get executed and the control
will be passed to statement-3.

3. IF the WRITE command is not successful, file status


variable will be non-zero, statement-2 will get executed
and the control will be passed to statement-3.

4. ACCESS MODE is ENVIRONMENT DIVISION must be SEQUENTIAL or


RANDOM.

5. File can be opened in OUTPUT or EXTEND mode.

6. With this WRITE syntax, a record can be written for the


first time (OUTPUT) or appended (EXTEND mode) in a KSDS
cluster. While appending, the primary key of the new record
must be higher than the primary key value of the last
record in the cluster.

_____________________________________________________________________________________
PAGE 39
COBOL Programming

Program: To Write a record in a KSDS.

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS FS-EMPNO
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN OUTPUT FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 40
COBOL Programming

PROCESS-PARA.
PERFORM ACCEPT-PARA.
PERFORM WRITE-PARA.
*-----------------------------------------------------------
ACCEPT-PARA.
ACCEPT WS-EMPNO.
ACCEPT WS-NAME.
ACCEPT WS-LOCN.
ACCEPT WS-SALARY.
*-----------------------------------------------------------
WRITE-PARA.
WRITE FS-RECORD FROM WS-RECORD.
IF WS-ERR = “00”
DISPLAY “1 RECORD SAVED”
ELSE
DISPLAY WS-ERR, “:WRITE ERROR”
END-IF.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”
END-IF.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 41
COBOL Programming

READ statement

Syntax 1:
READ logical-file-name INTO ws-record
INVALID KEY
Statement-1
NOT INVALID KEY
Statement-2
END-READ.
Statement-3.

Example:
MOVE 105 TO FS-EMPNO.
READ FILE-1 INTO WS-EMP-RECORD
INVALID KEY
DISPLAY “READ ERRROR”, WS-FILE-STATUS
NOT INVALID KEY
DISPLAY WS-EMP-RECORD
END-READ.

Points:
1. The primary key value to be searched must first be moved to
fs-primary-key-variable. READ command will match the value
in fs-primary-key-variable with the primary key field data
in the cluster.

2. If the READ command finds the primary key in the cluster,


it will enter NOT INVALID KEY block, execute statement-2
and the control will be passed to statement-3.

3. IF the READ command is not able to find the primary key in


the cluster, it will enter INVALID KEY block, execute
statement-1 and the control will be passed to statement-3.

4. ACCESS MODE must be RANDOM or DYNAMIC.

5. File can be opened in INPUT or I-O mode.

_____________________________________________________________________________________
PAGE 42
COBOL Programming

Syntax 2:
READ logical-file-name NEXT RECORD INTO ws-record
AT END
Statement-1
NOT AT END
Statement-2
END-READ.
Statement-3.

Example:
MOVE 105 TO FS-EMPNO.
READ FILE-1 NEXT RECORD INTO WS-EMP-RECORD
AT END
DISPLAY “READ ERRROR”, WS-FILE-STATUS
NOT AT END
DISPLAY WS-EMP-RECORD
END-READ.

Points:
1. With this WRITE syntax, the KSDS cluster will be read
sequentially. Each time the READ command is executed, it
will read the current record and place the data of the
record in ws-record variable.

2. If the READ command reads a record successfully, it will


enter NOT AT END block, execute statement-2 and control
will be passed to statement-3.

3. If the READ command reads end of file, it will enter AT END


block, execute statement-1 and the control will be passed
to statement-3.

4. ACCESS MODE must be DYNAMIC.

5. File can be opened in INPUT or I-O mode.

_____________________________________________________________________________________
PAGE 43
COBOL Programming

Syntax 3:
READ logical-file-name INTO ws-record
AT END
Statement-1
NOT AT END
Statement-2
END-READ.
Statement-3.

Example:
MOVE 105 TO FS-EMPNO.
READ FILE-1 INTO WS-EMP-RECORD
AT END
DISPLAY “READ ERRROR”, WS-FILE-STATUS
NOT AT END
DISPLAY WS-EMP-RECORD
END-READ.

Points:
1. With this WRITE syntax, the KSDS cluster will be read
sequentially. Each time the READ command is executed, it
will read the current record and place the data of the
record in ws-record variable.

2. If the READ command reads a record successfully, it will


enter NOT AT END block, execute statement-2 and the control
will be passed to statement-3.

3. IF the READ command read end of file, it will enter AT END


block, execute statement-1 and the control will be passed
to statement-3.

4. ACCESS MODE must be SEQUENTIAL.

5. File must be opened in INPUT mode.

_____________________________________________________________________________________
PAGE 44
COBOL Programming

Program: To query a record in a KSDS

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS FS-EMPNO
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
77 WS-CODE PIC 9(03) VALUE 0.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN INPUT FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 45
COBOL Programming

PROCESS-PARA.
ACCEPT WS-CODE.
PERFORM READ-PARA.
*-----------------------------------------------------------
READ-PARA.
MOVE WS-CODE TO FS-EMPNO.
READ FILE-1 INTO WS-EMP-RECORD
INVALID KEY
DISPLAY WS-ERR,”: WS-EMPNO NOT FOUND”
NOT INVALID KEY
DISPLAY “NAME :”, WS-NAME
DISPLAY “LOCATION :”, WS-LOCN
DISPLAY “SALARY :”, WS-SALARY
END-READ.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”
END-IF.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 46
COBOL Programming

Program: To display all records of a KSDS

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL
RECORD KEY IS FS-EMPNO
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN INPUT FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 47
COBOL Programming

PROCESS-PARA.
PERFORM READ-PARA UNTIL WS-ERR = “10”.
*-----------------------------------------------------------
READ-PARA.
READ FILE-1 INTO WS-EMP-RECORD
AT END
DISPLAY WS-ERR,”: END OF FILE”
NOT AT END
DISPLAY WS-EMP-RECORD
END-READ.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”
END-IF.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 48
COBOL Programming

REWRITE statement

Syntax:
REWRITE fs-record FROM ws-record
INVALID KEY
Statement-1
NOT INVALID KEY
Statement-2
END-REWRITE.
Statement-3.

Example:
REWRITE FS-EMP-RECORD FROM WS-EMP-RECORD
INVALID KEY
DISPLAY “REWRITE ERRROR”, WS-FILE-STATUS
NOT INVALID KEY
DISPLAY “WRITE SUCCESSFUL”
END-REWRITE.

Points:
1. REWRITE command will first move the data from ws-record to
fs-record and then rewrite the data from fs-record to the
file.

2. If the REWRITE command is successful, it will enter NOT


INVALID KEY block, execute statement-2 and the control will
be passed to statement-3.

3. IF the REWRITE command is not successful, it will enter


INVALID KEY block, execute statement-1 and the control will
be passed to statement-3.

4. Before rewriting the record of a primary key value, it must


be searched using READ command.

_____________________________________________________________________________________
PAGE 49
COBOL Programming

Program: To update a record in a KSDS

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS FS-EMPNO
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
77 WS-CODE PIC 9(03) VALUE 0.
77 WS-NEW-LOCN PIC X(10) VALUE SPACES.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN I-O FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.

_____________________________________________________________________________________
PAGE 50
COBOL Programming

*-----------------------------------------------------------
PROCESS-PARA.
ACCEPT WS-CODE.
ACCEPT WS-NEW-LOCN.
PERFORM READ-PARA.
*-----------------------------------------------------------
READ-PARA.
MOVE WS-CODE TO FS-EMPNO.
READ FILE-1 INTO WS-EMP-RECORD
INVALID KEY
DISPLAY WS-ERR,”: WS-EMPNO NOT FOUND”
NOT INVALID KEY
PERFORM UPDATE-PARA
END-READ.
*-----------------------------------------------------------
UPDATE-PARA.
IF WS-LOCN = WS-NEW-LOCN
DISPLAY “CANNOT RELOCATE”
ELSE
MOVE WS-NEW-LOCN TO WS-LOCN
ADD 1000 TO WS-SALARY
PERFORM REWRITE-PARA
END-IF.
*-----------------------------------------------------------
REWRITE-PARA.
REWRITE FS-EMP-RECORD FROM WS-EMP-RECORD.
IF WS-ERR = “00”
DISPLAY “1 RECORD UPDATED”
ELSE
DISPLAY WS-ERR, “:REWRITE ERROR”
END-IF.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”
END-IF.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 51
COBOL Programming

DELETE statement

Syntax:
DELETE logical-file-name RECORD
INVALID KEY
Statement-1
NOT INVALID KEY
Statement-2
END-DELETE.
Statement-3.

Example:
DELETE FILE-1 RECORD
INVALID KEY
DISPLAY “DELETE ERRROR”, WS-FILE-STATUS
NOT INVALID KEY
DISPLAY “DELETE SUCCESSFUL”
END-DELETE.

Points:
1. DELETE command will delete the current record from the
cluster.

2. If the DELETE command is successful, it will enter NOT


INVALID KEY block, execute statement-2 and the control will
be passed to statement-3.

3. IF the DELETE command is not successful, it will enter


INVALID KEY block, execute statement-1 and the control will
be passed to statement-3.

4. Before deleting the record, it must be searched using READ


command.

_____________________________________________________________________________________
PAGE 52
COBOL Programming

Program: To delete a record in a KSDS

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS FS-EMPNO
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
77 WS-CODE PIC 9(03) VALUE 0.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN I-O FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 53
COBOL Programming

PROCESS-PARA.
ACCEPT WS-CODE.
PERFORM READ-PARA.
*-----------------------------------------------------------
READ-PARA.
MOVE WS-CODE TO FS-EMPNO.
READ FILE-1 INTO WS-EMP-RECORD
INVALID KEY
DISPLAY WS-ERR,”: WS-EMPNO NOT FOUND”
NOT INVALID KEY
PERFORM DELETE-PARA
END-READ.
*-----------------------------------------------------------
DELETE-PARA.
DELETE FILE-1 RECORD.
IF WS-ERR = “00”
DISPLAY “1 RECORD DELETED”
ELSE
DISPLAY WS-ERR, “:DELETE ERROR”
END-IF.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”
END-IF.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 54
COBOL Programming

START command

Syntax:
START logical-file-name KEY = fs-prim-key/ fs-alt-key
INVALID KEY
Statement-1
NOT INVALID KEY
Statement-2
END-START.
Statement-3.

Example:
START FILE-1 KEY = FS-EMPNO
INVALID KEY
DISPLAY “START ERROR”, WS-FILE-STATUS
NOT INVALID KEY
DISPLAY “START SUCCEEDED”
END-START.

Points:
1. START command will search the primary key value/ alternate
key value in the respective index component.

2. If START finds the primary key/ alternate key in the index


component, it will enter NOT INVALID KEY block, execute
statement-2 and the control will be passed to statement-3.

3. If START is unable to find the primary key/ alternate key


in the index component, it will enter INVALID KEY block,
execute statement-1 and the control will be passed to
statement-3.

4. START command does not read the record. It only places the
record pointer on the record.

5. ACCESS MODE must be SEQUENTIAL or DYNAMIC.

6. File can be opened in INPUT or I-O mode.

_____________________________________________________________________________________
PAGE 55
COBOL Programming

Program: To display records from a specific record in a KSDS

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS FS-EMPNO
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
77 WS-CODE PIC 9(03) VALUE 0.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN INPUT FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 56
COBOL Programming

PROCESS-PARA.
ACCEPT WS-CODE.
PERFORM START-PARA.
*-----------------------------------------------------------
START-PARA.
MOVE WS-CODE TO FS-EMPNO.
START FILE-1 KEY = FS-EMPNO
INVALID KEY
DISPLAY WS-ERR,”: WS-EMPNO NOT FOUND”
NOT INVALID KEY
PERFORM READ-PARA UNTIL WS-ERR = “10”
END-READ.
*-----------------------------------------------------------
READ-PARA.
READ FILE-1 NEXT RECORD INTO WS-EMP-RECORD
AT END
DISPLAY WS-ERR, “: END OF FILE”
NOT AT END
DISPLAY WS-EMP-RECORD
END-READ.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”
END-IF.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 57
COBOL Programming

ESDS/PS FILE COMMANDS

ENVIRONMENT DIVISION entries

Syntax:
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT logical-file-name ASSIGN TO AS-ddname/ ddname
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
ALTERNATE RECORD KEY IS fs-alt-key WITH DUPLICATES
FILE STATUS IS ws-err.

AS-ddname is given for an ESDS. ddname is given for a PS.

Example:
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1.
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-ERR.

File opening Modes

Before any operation is performed on a data file, the file must


be opened. Following are the modes in which an ESDS cluster/ PS
can be opened.

 OUTPUT mode is used to write a record in an ESDS cluster/


PS for the first time.

 EXTEND mode is used to add a record in an ESDS cluster/ PS.

 I-O mode is used to update a record in an ESDS cluster/ PS.

 INPUT is used to read a record from an ESDS cluster/ PS.

_____________________________________________________________________________________
PAGE 58
COBOL Programming

OPEN statement

Syntax:
OPEN mode logical-file-name.

Example:
OPEN OUTPUT FILE-1.
OPEN INPUT FILE-2.

CLOSE statement

Syntax:
CLOSE logical-file-name.

Example:
CLOSE FILE-1.
CLOSE FILE-2.

_____________________________________________________________________________________
PAGE 59
COBOL Programming

WRITE statement

Syntax:
WRITE fs-record FROM ws-record
AT END
Statement-1
NOT AT END
Statement-2
END-WRITE.
Statement-3.

Example:
WRITE FS-EMP-RECORD FROM WS-EMP-RECORD
AT END
DISPLAY “WRITE ERRROR”, WS-FILE-STATUS
NOT AT END
DISPLAY “WRITE SUCCESSFUL”
END-WRITE.

Points:
1. WRITE command will first move the data from ws-record to
fs-record and then write the data from fs-record to the
file.

2. If the WRITE command is successful, it will enter NOT AT


END block, execute statement-2 and the control will be
passed to statement-3.

3. If the WRITE command is not successful, it will enter AT


END block, executed statement-1 and the control will be
passed to statement-3.

4. File can be opened in OUTPUT or EXTEND mode.

5. With this WRITE syntax, a record can be written for the


first time (OUTPUT mode) or appended (EXTEND mode) in an
ESDS cluster.

_____________________________________________________________________________________
PAGE 60
COBOL Programming

Program: To Write a record in an ESDS.

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO AS-DD1
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN OUTPUT FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 61
COBOL Programming

PROCESS-PARA.
PERFORM ACCEPT-PARA.
PERFORM WRITE-PARA.

*-----------------------------------------------------------
ACCEPT-PARA.
ACCEPT WS-EMPNO.
ACCEPT WS-NAME.
ACCEPT WS-LOCN.
ACCEPT WS-SALARY.
*-----------------------------------------------------------
WRITE-PARA.
WRITE FS-RECORD FROM WS-RECORD.
IF WS-ERR = “00”
DISPLAY “1 RECORD SAVED”
ELSE
DISPLAY WS-ERR, “:WRITE ERROR”
END-IF.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”
END-IF.
*-----------------------------------------------------------

WRITE-PARA can also be coded as follows :


WRITE-PARA.
WRITE FS-RECORD FROM WS-RECORD
AT END
DISPLAY WS-ERR, “:WRITE ERROR”
NOT AT END
DISPLAY “1 RECORD SAVED”
END-WRITE.

_____________________________________________________________________________________
PAGE 62
COBOL Programming

READ statement

Syntax:
READ logical-file-name INTO ws-record
AT END
Statement-1
NOT AT END
Statement-2
END-READ.
Statement-3.

Example:
MOVE 105 TO FS-EMPNO.
READ FILE-1 INTO WS-EMP-RECORD
AT END
DISPLAY “READ ERRROR”, WS-FILE-STATUS
NOT AT END
DISPLAY WS-EMP-RECORD
END-READ.

Points:
1. READ command will read an ESDS cluster/ PS sequentially.
Each time the READ command is executed, it will read the
current record and place the data of the record in ws-
record variable.

2. If the READ command reads a record successfully, it will


enter NOT AT END block, execute statement-2 and the control
will be passed to statement-3.

3. IF the READ command reads end of file, it will enter AT END


block, execute statement-1 and the control will be passed
to statement-3.

4. File must be opened in INPUT mode.

_____________________________________________________________________________________
PAGE 63
COBOL Programming

Program: To query a record in an ESDS

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO AS-DD1
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
77 WS-CODE PIC 9(03) VALUE 0.
77 WS-FOUND PIC X(01) VALUE “N”.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN INPUT FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 64
COBOL Programming

PROCESS-PARA.
ACCEPT WS-CODE.
PERFORM READ-PARA UNTIL WS-ERR = “10”
OR WS-FOUND = “Y”.
IF WS-FOUND = “N”
DISPLAY “EMPNO NOT FOUND”
END-IF.
*-----------------------------------------------------------
READ-PARA.
READ FILE-1 INTO WS-EMP-RECORD
AT END
DISPLAY WS-ERR,”: END OF FILE”
NOT AT END
IF WS-CODE = WS-EMPNO
DISPLAY WS-EMP-RECORD
MOVE “Y” TO WS-FOUND
END-IF
END-READ.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”
END-IF.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 65
COBOL Programming

REWRITE statement

Syntax:
REWRITE fs-record FROM ws-record
AT END
Statement-1
NOT AT END
Statement-2
END-REWRITE.
Statement-3.

Example:
REWRITE FS-EMP-RECORD FROM WS-EMP-RECORD
AT END
DISPLAY “REWRITE ERRROR”, WS-FILE-STATUS
NOT AT END
DISPLAY “WRITE SUCCESSFUL”
END-REWRITE.

Points:
1. REWRITE command will first move the data from ws-record to
fs-record and then rewrite the data from fs-record to the
file.

2. If the REWRITE command is successful, it will enter NOT AT


END block, execute statement-2 and the control will be
passed to statement-3.

3. IF the REWRITE command is not successful, it will enter AT


END block, execute statement-1 and the control will be
passed to statement-3.

4. Before rewriting the record, it must be read using READ


command.

5. File must be opened in I-O mode.

_____________________________________________________________________________________
PAGE 66
COBOL Programming

Program: To update a record in an ESDS

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO AS-DD1
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
RECORD KEY IS FS-EMPNO
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
77 WS-CODE PIC 9(03) VALUE 0.
77 WS-NEW-LOCN PIC X(10) VALUE SPACES.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN I-O FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.

_____________________________________________________________________________________
PAGE 67
COBOL Programming

*-----------------------------------------------------------
PROCESS-PARA.
ACCEPT WS-CODE.
ACCEPT WS-NEW-LOCN.
PERFORM READ-PARA UNTIL WS-ERR = “10”
OR WS-FOUND = “Y”.
IF WS-FOUND = “N”
DISPLAY “EMPNO NOT FOUND”
END-IF.
*-----------------------------------------------------------
READ-PARA.
READ FILE-1 INTO WS-EMP-RECORD
AT END
DISPLAY WS-ERR,”: END OF FILE”
NOT AT END
IF WS-EMPNO = WS-CODE
PERFORM UPDATE-PARA
END-IF
END-READ.
*-----------------------------------------------------------
UPDATE-PARA.
IF WS-LOCN = WS-NEW-LOCN
DISPLAY “EMPLOYEE CANNOT RELOCATE”
ELSE
MOVE WS-NEW-LOCN TO WS-LOCN
PERFORM REWRITE-PARA
END-IF.
*-----------------------------------------------------------
REWRITE-PARA.
REWRITE FS-EMP-RECORD FROM WS-EMP-RECORD.
IF WS-ERR = “00”
DISPLAY “1 RECORD UPDATED”
ELSE
DISPLAY WS-ERR, “:REWRITE ERROR”
END-IF.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”

_____________________________________________________________________________________
PAGE 68
COBOL Programming

END-IF.
*-----------------------------------------------------------
RRDS FILE COMMANDS

ENVIRONMENT DIVISION entries

Syntax:
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT logical-file-name ASSIGN TO ddname
ORGANIZATION IS RELATIVE
ACCESS MODE IS SEQUENTIAL/ RANDOM
RELATIVE KEY IS fs-primary-key
FILE STATUS IS ws-err.

Example:
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RELATIVE KEY IS WS-RECNO
FILE STATUS IS WS-FILE-STATUS.

File opening Modes

Before any operation is performed on a data file, the file must


be opened. Following are the modes in which RRDS can be opened.

 OUTPUT mode is used to write a record in a RRDS cluster for


the first time. Access mode can be SEQUENTIAL or RANDOM.

 EXTEND mode is used to add a record in a RRDS cluster.


Access mode must be SEQUENTIAL.

 I-O mode is used to insert a record, update a record or


delete a record in a RRSDS cluster. Access mode can be
RANDOM or DYNAMIC.
_____________________________________________________________________________________
PAGE 69
COBOL Programming

 INPUT is used to read a record from a RRDS cluster. Access


mode can be SEQUENTIAL or RANDOM or DYNAMIC
OPEN statement

Syntax:
OPEN mode logical-file-name.

Example:
OPEN OUTPUT FILE-1.
OPEN INPUT FILE-2.

CLOSE statement

Syntax:
CLOSE logical-file-name.

Example:
CLOSE FILE-1.
CLOSE FILE-2.

_____________________________________________________________________________________
PAGE 70
COBOL Programming

WRITE statement

Syntax 1:
WRITE fs-record FROM ws-record
INVALID KEY
Statement-1
NOT INVALID KEY
Statement-2
END-WRITE.
Statement-3.

Example:
MOVE 5 TO WS-RECNO.
WRITE FS-EMP-RECORD FROM WS-EMP-RECORD
INVALID KEY
DISPLAY “WRITE ERRROR”, WS-FILE-STATUS
NOT INVALID KEY
DISPLAY “WRITE SUCCESSFUL”
END-WRITE.

Points:
1. WRITE command will first move the data from ws-record to
fs-record and then write the data from fs-record to the
file at the record number mentioned in relative key
variable (WS-RECNO). If a record already exists at the
record number mentioned in relative key variable, WRITE
command will fail.

2. If the WRITE command is successful, it will enter NOT


INVALID KEY block, execute statement-2 and the control will
be passed to statement-3.

3. IF the WRITE command is not successful, it will enter


INVALID KEY block, execute statement-1 and the control will
be passed to statement-3.

4. ACCESS MODE in ENVIRONMENT DIVISION must be RANDOM.

5. File must be opened in OUTPUT or I-O mode.

_____________________________________________________________________________________
PAGE 71
COBOL Programming

6. With this WRITE syntax, a record can be written for the


first time (OUTPUT mode) or it can be inserted/appended (I-
O mode) in a RRDS cluster.

Syntax 2:
WRITE fs-record FROM ws-record.
IF file-status-variable = “00”
Statement-1
ELSE
Statement-2
END-IF.
Statement-3.

Example:
WRITE FS-EMP-RECORD FROM WS-EMP-RECORD.
IF WS-FILE-STATUS = “00”
DISPLAY “WRITE SUCCESSFUL”
ELSE
DISPLAY “WRITE ERRROR”, WS-FILE-STATUS
END-IF.

Points:
1. WRITE command will first move the data from ws-record to
fs-record and then write the data from fs-record to the
file.

2. If the WRITE command is successful, file status variable


will be “00”, statement-1 will get executed and the control
will be passed to statement-3.

3. IF the WRITE command is not successful, file status


variable will be non-zero, statement-2 will get executed
and the control will be passed to statement-3.

4. ACCESS MODE is ENVIRONMENT DIVISION must be SEQUENTIAL.

5. File can be opened in OUTPUT or EXTEND mode.

6. With this WRITE syntax, a record can be written for the


first time (OUTPUT mode) or appended (EXTEND mode) in a
RRDS cluster.

_____________________________________________________________________________________
PAGE 72
COBOL Programming

Program: To Write a record in a RRDS.

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RELATIVE KEY IS WS-RECNO
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
77 WS-RECNO PIC 9(02) VALUE 0.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN I-O FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 73
COBOL Programming

PROCESS-PARA.
PERFORM ACCEPT-PARA.
PERFORM WRITE-PARA.
*-----------------------------------------------------------
ACCEPT-PARA.
ACCEPT WS-RECNO.
ACCEPT WS-EMPNO.
ACCEPT WS-NAME.
ACCEPT WS-LOCN.
ACCEPT WS-SALARY.
*-----------------------------------------------------------
WRITE-PARA.
WRITE FS-RECORD FROM WS-RECORD.
IF WS-ERR = “00”
DISPLAY “1 RECORD SAVED”
ELSE
DISPLAY WS-ERR, “:WRITE ERROR”
END-IF.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”
END-IF.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 74
COBOL Programming

READ statement

Syntax 1:
READ logical-file-name INTO ws-record
INVALID KEY
Statement-1
NOT INVALID KEY
Statement-2
END-READ.
Statement-3.

Example:
MOVE 3 TO WS-RECNO.
READ FILE-1 INTO WS-EMP-RECORD
INVALID KEY
DISPLAY “READ ERRROR”, WS-FILE-STATUS
NOT INVALID KEY
DISPLAY WS-EMP-RECORD
END-READ.

Points:
1. The record number to be searched must first be moved to WS-
RECNO variable. READ command will check if a record exists
at the record number mentioned.

2. If the READ command finds the record at the record number


mentioned, it will enter NOT INVALID KEY block, execute
statement-2 and the control will be passed to statement-3.

3. IF the READ command is not able to find the record at the


record number position, it will enter INVALID KEY block,
execute statement-1 and the control will be passed to
statement-3.

4. ACCESS MODE must be RANDOM or DYNAMIC.

5. File can be opened in INPUT or I-O mode.

_____________________________________________________________________________________
PAGE 75
COBOL Programming

Syntax 2:
READ logical-file-name NEXT RECORD INTO ws-record
AT END
Statement-1
NOT AT END
Statement-2
END-READ.
Statement-3.

Example:
READ FILE-1 NEXT RECORD INTO WS-EMP-RECORD
AT END
DISPLAY “READ ERRROR”, WS-FILE-STATUS
NOT AT END
DISPLAY WS-EMP-RECORD
END-READ.

Points:
1. With this READ syntax, the RRDS cluster will be read
sequentially. Each time the READ command is executed, it
will read the current record and place the data of the
record in ws-record variable.

2. If the READ command reads a record successfully, it will


enter NOT AT END block, execute statement-2 and control
will be passed to statement-3.

3. IF the READ command reads end of file, it will enter AT END


block, execute statement-1 and the control will be passed
to statement-3.

4. ACCESS MODE must be DYNAMIC.

5. File can be opened in INPUT or I-O mode.

_____________________________________________________________________________________
PAGE 76
COBOL Programming

Syntax 3:
READ logical-file-name INTO ws-record
AT END
Statement-1
NOT AT END
Statement-2
END-READ.
Statement-3.

Example:
READ FILE-1 INTO WS-EMP-RECORD
AT END
DISPLAY “READ ERRROR”, WS-FILE-STATUS
NOT AT END
DISPLAY WS-EMP-RECORD
END-READ.

Points:
1. With this READ syntax, the RRDS cluster will be read
sequentially. Each time the READ command is executed, it
will read the current record and place the data of the
record in ws-record variable.

2. If the READ command reads a record successfully, it will


enter NOT AT END block, execute statement-2 and the control
will be passed to statement-3.

3. IF the READ command read end of file, it will enter AT END


block, execute statement-1 and the control will be passed
to statement-3.

4. ACCESS MODE must be SEQUENTIAL.

5. File must be opened in INPUT mode.

_____________________________________________________________________________________
PAGE 77
COBOL Programming

Program: To query a record in a RRDS

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RELATIVE KEY IS WS-RECNO
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
77 WS-RECNO PIC 9(02) VALUE 0.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN INPUT FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.

_____________________________________________________________________________________
PAGE 78
COBOL Programming

*-----------------------------------------------------------
PROCESS-PARA.
ACCEPT WS-RECNO.
PERFORM READ-PARA.
*-----------------------------------------------------------
READ-PARA.
READ FILE-1 INTO WS-EMP-RECORD
INVALID KEY
DISPLAY WS-ERR,”: WS-EMPNO NOT FOUND”
NOT INVALID KEY
DISPLAY “NAME :”, WS-NAME
DISPLAY “LOCATION :”, WS-LOCN
DISPLAY “SALARY :”, WS-SALARY
END-READ.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”
END-IF.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 79
COBOL Programming

REWRITE statement

Syntax:
REWRITE fs-record FROM ws-record
INVALID KEY
Statement-1
NOT INVALID KEY
Statement-2
END-REWRITE.
Statement-3.

Example:
REWRITE FS-EMP-RECORD FROM WS-EMP-RECORD
INVALID KEY
DISPLAY “REWRITE ERRROR”, WS-FILE-STATUS
NOT INVALID KEY
DISPLAY “WRITE SUCCESSFUL”
END-REWRITE.

Points:
1. REWRITE command will first move the data from ws-record to
fs-record and then rewrite the data from fs-record to the
file.

2. If the REWRITE command is successful, it will enter NOT


INVALID KEY block, execute statement-2 and the control will
be passed to statement-3.

3. IF the REWRITE command is not successful, it will enter


INVALID KEY block, execute statement-1 and the control will
be passed to statement-3.

4. Before rewriting the record of a record number, it must be


searched using READ command.

5. ACCESS MODE must be I-O or DYNAMIC.

6. File must be opened in I-O mode.

_____________________________________________________________________________________
PAGE 80
COBOL Programming

Program: To update a record in a RRDS

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RELATIVE KEY IS WS-RECNO
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
77 WS-RECNO PIC 9(02) VALUE 0.
77 WS-NEW-LOCN PIC X(10) VALUE SPACES.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN I-O FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.

_____________________________________________________________________________________
PAGE 81
COBOL Programming

*-----------------------------------------------------------
PROCESS-PARA.
ACCEPT WS-RECNO.
ACCEPT WS-NEW-LOCN.
PERFORM READ-PARA.
*-----------------------------------------------------------
READ-PARA.
READ FILE-1 INTO WS-EMP-RECORD
INVALID KEY
DISPLAY WS-ERR,”: NO RECORD AT RECNO”
NOT INVALID KEY
PERFORM UPDATE-PARA
END-READ.
*-----------------------------------------------------------
UPDATE-PARA.
IF WS-LOCN = WS-NEW-LOCN
DISPLAY “EMPNO CANNOT RELOCATE”
ELSE
MOVE WS-NEW-LOCN TO WS-LOCN
ADD 1000 TO WS-SALARY
PERFORM REWRITE-PARA
END-IF.
*-----------------------------------------------------------
REWRITE-PARA.
REWRITE FS-EMP-RECORD FROM WS-EMP-RECORD.
IF WS-ERR = “00”
DISPLAY “1 RECORD UPDATED”
ELSE
DISPLAY WS-ERR, “:REWRITE ERROR”
END-IF.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”
END-IF.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 82
COBOL Programming

DELETE statement

Syntax:
DELETE logical-file-name RECORD
INVALID KEY
Statement-1
NOT INVALID KEY
Statement-2
END-DELETE.
Statement-3.

Example:
MOVE 3 TO WS-RECNO.
DELETE FILE-1 RECORD
INVALID KEY
DISPLAY “DELETE ERRROR”, WS-FILE-STATUS
NOT INVALID KEY
DISPLAY “DELETE SUCCESSFUL”
END-DELETE.

Points:
1. DELETE command will delete the current record from the
cluster.

2. If the DELETE command is successful, it will enter NOT


INVALID KEY block, execute statement-2 and the control will
be passed to statement-3.

3. IF the DELETE command is not successful, it will enter


INVALID KEY block, execute statement-1 and the control will
be passed to statement-3.

4. Before deleting the record, it must be searched using READ


command.

5. ACCESS MODE must be I-O.

6. File must be opened in I-O mode.

_____________________________________________________________________________________
PAGE 83
COBOL Programming

Program: To delete a record in a RRDS

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RELATIVE KEY IS WS-RECNO
FILE STATUS IS WS-ERR.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-EMP-RECORD.
05 FS-EMPNO PIC 9(03).
05 FS-NAME PIC X(10).
05 FS-LOCN PIC X(10).
05 FS-SALARY PIC 9(06).
WORKING-STORAGE SECTION.
01 WS-EMP-RECORD.
05 WS-EMPNO PIC 9(03).
05 WS-NAME PIC X(10).
05 WS-LOCN PIC X(10).
05 WS-SALARY PIC 9(06).
77 WS-ERR PIC X(02) VALUE SPACES.
77 WS-RECNO PIC 9(02) VALUE 0.
PROCEDURE DIVISION.
*-----------------------------------------------------------
MAIN-PARA.
OPEN I-O FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE OPENED”
PERFORM PROCESS-PARA
PERFORM CLOSE-PARA
ELSE
DISPLAY WS-ERR, “:FILE OPEN ERROR”
END-IF.
STOP RUN.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 84
COBOL Programming

PROCESS-PARA.
ACCEPT WS-RECNO.
PERFORM READ-PARA.
*-----------------------------------------------------------
READ-PARA.
READ FILE-1 INTO WS-EMP-RECORD
INVALID KEY
DISPLAY WS-ERR,”: NO RECORD AT RECNO”
NOT INVALID KEY
PERFORM DELETE-PARA
END-READ.
*-----------------------------------------------------------
DELETE-PARA.
DELETE FILE-1 RECORD.
IF WS-ERR = “00”
DISPLAY “1 RECORD DELETED”
ELSE
DISPLAY WS-ERR, “:DELETE ERROR”
END-IF.
*-----------------------------------------------------------
CLOSE-PARA.
CLOSE FILE-1.
IF WS-ERR = “00”
DISPLAY “FILE CLOSED”
ELSE
DISPLAY WS-ERR, “:FILE CLOSE ERROR”
END-IF.
*-----------------------------------------------------------

_____________________________________________________________________________________
PAGE 85
COBOL Programming

SORT command

SORT command is used to sort the data in COBOL. This


command requires three files for sorting : input file, work file
and output file. When the SORT command is executed, the
following 3 steps will be executed :

1. Copy all the records from the input file to the work file
2. Sort the records in the work file
3. Copy all the sorted records from the work file to the
output file.

The files must not be opened and closed in the COBOL


program since SORT command will open and close the files during
execution.

Syntax:
SORT work-file-logical-name
ON ASCENDING/DESCENDING work-file-column
USING input-file-logical-name
GIVING output-file-logical-name.

_____________________________________________________________________________________
PAGE 86
COBOL Programming

Program: To sort records of a given input file.

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS SEQUENTIAL.
SELECT FILE-2 ASSIGN TO DD2.
SELECT FILE-3 ASSIGN TO DD3
ORGANIZATION IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-INPUT-RECORD.
05 FS-I-EMPNO PIC 9(03).
05 FS-I-NAME PIC X(10).
05 FS-I-LOCN PIC X(10).
05 FS-I-SALARY PIC 9(07).
05 FILLER PIC X(50).
SD FILE-2.
01 FS-WORK-RECORD.
05 FS-W-EMPNO PIC 9(03).
05 FS-W-NAME PIC X(10).
05 FS-W-LOCN PIC X(10).
05 FS-W-SALARY PIC 9(07).
05 FILLER PIC X(50).
FD FILE-3
LABEL RECORDS ARE STANDARD.
01 FS-OUTPUT-RECORD.
05 FS-O-EMPNO PIC 9(03).
05 FS-O-NAME PIC X(10).
05 FS-O-LOCN PIC X(10).
05 FS-O-SALARY PIC 9(07).
05 FILLER PIC X(50).
PROCEDURE DIVISION.
MAIN-PARA.
SORT FILE-2 ON ASCENDING FS-W-NAME
USING FILE-1 GIVING FILE-3.

_____________________________________________________________________________________
PAGE 87
COBOL Programming

STOP RUN.
INPUT PROCEDURE in SORT

INPUT PROCEDURE is used to copy selected records from the input


file to the work file. Sort will then sort the selected records
and place the sorted records in the output file.

Input file must be opened in the program in INPUT mode and it


must be closed after sorting.

Syntax:
SORT work-file-logical-name
ON ASCENDING/DESCENDING work-file-field
INPUT PROCEDURE para-name
GIVING output-file-logical-name

Example:
SORT FILE-2 ON ASCENDING FS-W-NAME
INPUT PROCEDURE PARA-1
GIVING FILE-3.

RELEASE command

RELEASE command must be used to write the record in the work-


file.

Syntax:
RELEASE ws-record FROM fs-record.

Example:
RELEASE WORK-RECORD FROM FS-WORK-RECORD.

_____________________________________________________________________________________
PAGE 88
COBOL Programming

Program: To sort specific records of a given input file.

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS SEQUENTIAL
FILE STATUS IS WS-ERR1.
SELECT FILE-2 ASSIGN TO DD2.
SELECT FILE-3 ASSIGN TO DD3
ORGANIZATION IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-INPUT-RECORD.
05 FS-I-EMPNO PIC 9(03).
05 FS-I-NAME PIC X(10).
05 FS-I-LOCN PIC X(10).
05 FS-I-SALARY PIC 9(07).
05 FILLER PIC X(50).
SD FILE-2.
01 FS-WORK-RECORD.
05 FS-W-EMPNO PIC 9(03).
05 FS-W-NAME PIC X(10).
05 FS-W-LOCN PIC X(10).
05 FS-W-SALARY PIC 9(07).
05 FILLER PIC X(50).
FD FILE-3
LABEL RECORDS ARE STANDARD.
01 FS-OUTPUT-RECORD.
05 FS-O-EMPNO PIC 9(03).
05 FS-O-NAME PIC X(10).
05 FS-O-LOCN PIC X(10).
05 FS-O-SALARY PIC 9(07).
05 FILLER PIC X(50).
WORKING-STORAGE SECTION.
77 WS-ERR1 PIC X(02) VALUE SPACES.
01 WS-INPUT-RECORD.
05 WS-I-EMPNO PIC 9(03).

_____________________________________________________________________________________
PAGE 89
COBOL Programming

05 WS-I-NAME PIC X(10).


05 WS-I-LOCN PIC X(10).
05 WS-I-SALARY PIC 9(07).
05 FILLER PIC X(50).
01 WS-WORK-RECORD.
05 WS-W-EMPNO PIC 9(03).
05 WS-W-NAME PIC X(10).
05 WS-W-LOCN PIC X(10).
05 WS-W-SALARY PIC 9(07).
05 FILLER PIC X(50).
PROCEDURE DIVISION.
*----------------------------------------------------------
MAIN-PARA.
SORT FILE-2 ON ASCENDING FS-W-NAME
INPUT PROCEDURE PARA-1
GIVING FILE-3.
STOP RUN.
*----------------------------------------------------------
PARA-1.
OPEN INPUT FILE-1.
IF WS-ERR1 = “00”
DISPLAY WS-ERR1, “: INPUT FILE OPENED”
PERFORM READ-PARA UNTIL WS-ERR1 = “10”
CLOSE FILE-1
IF WS-ERR1 = “00”
DISPLAY “INPUT FILE CLOSED”
ELSE
DISPLAY WS-ERR1, “: INPUT FILE CLOSE ERROR”
END-IF
ELSE
DISPLAY WS-ERR1, “: INPUT FILE OPEN ERROR”
END-IF.
*----------------------------------------------------------
READ-PARA.
READ FILE-1 INTO WS-INPUT-RECORD
AT END
DISPLAY “END OF INPUT FILE”
NOT AT END
IF WS-I-LOCN = “PUNE”
MOVE WS-INPUT-RECORD TO WS-WORK-RECORD
RELEASE FS-WORK-RECORD FROM WS-WORK-RECORD
END-IF

_____________________________________________________________________________________
PAGE 90
COBOL Programming

END-READ.
*----------------------------------------------------------

_____________________________________________________________________________________
PAGE 91
COBOL Programming

OUTPUT PROCEDURE in SORT

OUTPUT PROCEDURE is used to copy records from the work file


to the output file after sorting. This option can be used if
any formatting is required in the output file. Formatting could
be change in the field sequence in the output file or a
delimiter required to be placed between two fields.

Output file must be opened in the program in OUTPUT mode


and it must be closed after copy operation.

Syntax:
SORT work-file-logical-name
ON ASCENDING/DESCENDING work-file-field
USING input-file-logical-name
OUTPUT PROCEDURE para-name

Example:
SORT FILE-2 ON ASCENDING FS-W-NAME
USING FILE-1
OUTPUT PROCEDURE PARA-1.

RETURN command

RETURN command must be used to read the record from the work-
file.

Syntax:
RETURN work-file-logical-name INTO work-filer-record
AT END
Statement-1
NOT AT END
Statement-2
END-RETURN.

Example:
RETURN FILE-2 INTO WS-WORK-RECORD
AT END
DISPLAY “END OF WORK FILE”
NOT AT END
DISPLAY “RETURN SUCCESS”
END-RETURN.
_____________________________________________________________________________________
PAGE 92
COBOL Programming

Program: To change field sequence in the output file.

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS SEQUENTIAL.
SELECT FILE-2 ASSIGN TO DD2.
SELECT FILE-3 ASSIGN TO DD3
ORGANIZATION IS SEQUENTIAL
FILE STATUS IS WS-ERR3.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-INPUT-RECORD.
05 FS-I-EMPNO PIC 9(03).
05 FS-I-NAME PIC X(10).
05 FS-I-LOCN PIC X(10).
05 FS-I-SALARY PIC 9(07).
05 FILLER PIC X(50).
SD FILE-2.
01 FS-WORK-RECORD.
05 FS-W-EMPNO PIC 9(03).
05 FS-W-NAME PIC X(10).
05 FS-W-LOCN PIC X(10).
05 FS-W-SALARY PIC 9(07).
05 FILLER PIC X(50).
FD FILE-3
LABEL RECORDS ARE STANDARD.
01 FS-OUTPUT-RECORD.
05 FS-O-LOCN PIC X(10).
05 FS-O-NAME PIC X(10).
05 FS-O-SALARY PIC 9(07).
05 FS-O-EMPNO PIC 9(03).
05 FILLER PIC X(50).
WORKING-STORAGE SECTION.
77 WS-ERR3 PIC X(02) VALUE SPACES.
77 WS-EOF PIC X(01) VALUE “N”.
01 WS-WORK-RECORD.

_____________________________________________________________________________________
PAGE 93
COBOL Programming

05 WS-W-EMPNO PIC 9(03).


05 WS-W-NAME PIC X(10).
05 WS-W-LOCN PIC X(10).
05 WS-W-SALARY PIC 9(07).
05 FILLER PIC X(50).
01 WS-OUTPUT-RECORD.
05 WS-O-LOCN PIC X(10).
05 WS-O-NAME PIC X(10).
05 WS-O-SALARY PIC 9(07).
05 WS-O-EMPNO PIC 9(03).
05 FILLER PIC X(50).
PROCEDURE DIVISION.
*----------------------------------------------------------
MAIN-PARA.
SORT FILE-2 ON ASCENDING FS-W-NAME
USING FILE-1
OUTPUT PROCEDURE PARA-1.
STOP RUN.
*----------------------------------------------------------
PARA-1.
OPEN OUTPUT FILE-3.
IF WS-ERR3 = “00”
DISPLAY WS-ERR3, “: OUTPUT FILE OPENED”
PERFORM RETURN-PARA UNTIL WS-EOF = “Y”
CLOSE FILE-3
IF WS-ERR3 = “00”
DISPLAY “OUTPUT FILE CLOSED”
ELSE
DISPLAY WS-ERR3, “: OUTPUT FILE CLOSE ERROR”
END-IF
ELSE
DISPLAY WS-ERR3, “: OUTPUT FILE OPEN ERROR”
END-IF.
*----------------------------------------------------------
RETURN-PARA.
RETURN FILE-2 INTO WS-WORK-RECORD
AT END
DISPLAY “END OF WORK FILE”
MOVE “Y” TO WS-EOF
NOT AT END
PERFORM FORMAT-PARA
PERFORM WRITE-PARA

_____________________________________________________________________________________
PAGE 94
COBOL Programming

END-RETURN.
*----------------------------------------------------------
FORMAT-PARA.
MOVE WS-W-EMPNO TO WS-O-EMPNO.
MOVE WS-W-NAME TO WS-O-NAME.
MOVE WS-W-LOCN TO WS-O-LOCN.
MOVE WS-W-SALARY TO WS-O-SALARY.
*----------------------------------------------------------
WRITE-PARA.
WRITE FS-OUTPUT-RECORD FROM WS-OUTPUT-RECORD.
IF WS-ERR3 = “00”
DISPLAY “1 RECORD SAVED”
ELSE
DISPLAY WS-ERR3, “: WRITE ERROR”
END-IF.
*----------------------------------------------------------

_____________________________________________________________________________________
PAGE 95
COBOL Programming

MERGE command

MERGE command is used to merge the data in COBOL. This


command requires minimum four files for sorting : two or more
input files, one work file and one output file. When the MERGE
command is executed, the following 3 steps will be executed :

1. Copy the records from all the input files to the work file
2. Merge the records in the work file
3. Copy all the merged records from the work file to the
output file.

The files must not be opened and closed in the COBOL


program since MERGE command will open and close the files during
execution.
For the merge operation to work, the following points must be
considered :
1. The input files mentioned in merged must be sorted on the
same field, same sequence.
2. The merge field and merge sequence must be the same as that
of the input files.

Syntax:
MERGE work-file-logical-name
ON ASCENDING/DESCENDING work-file-column
USING in-file1-logical-name,in-file2-logical-name
GIVING out-file-logical-name.

_____________________________________________________________________________________
PAGE 96
COBOL Programming

Program: To merge records of two input files.

IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-1 ASSIGN TO DD1
ORGANIZATION IS SEQUENTIAL.
SELECT FILE-2 ASSIGN TO DD2
ORGANIZATION IS SEQUENTIAL.
SELECT FILE-3 ASSIGN TO DD3.
SELECT FILE-4 ASSIGN TO DD4
ORGANIZATION IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FILE-1
LABEL RECORDS ARE STANDARD.
01 FS-INPUT1-RECORD.
05 FS-I1-NAME PIC X(10).
05 FS-I1-SALARY PIC 9(07).
FD FILE-2
LABEL RECORDS ARE STANDARD.
01 FS-INPUT2-RECORD.
05 FS-I2-NAME PIC X(10).
05 FS-I2-SALARY PIC 9(07).
SD FILE-3.
01 FS-WORK-RECORD.
05 FS-W-NAME PIC X(10).
05 FS-W-SALARY PIC 9(07).
FD FILE-4
LABEL RECORDS ARE STANDARD.
01 FS-OUTPUT-RECORD.
05 FS-O-NAME PIC X(10).
05 FS-O-SALARY PIC 9(07).
PROCEDURE DIVISION.
MAIN-PARA.
MERGE FILE-3 ON ASCENDING FS-W-NAME
USING FILE-1, FILE-2 GIVING FILE-3.
STOP RUN.

_____________________________________________________________________________________
PAGE 97
COBOL Programming

COPY verb

Syntax:
COPY copy-book-member.

Example:
COPY MEM1.

Points:
1. COPY command is used to plug-in code of the copy-book
member at the position where the COPY command is given.

2. Placing the code in a copy-book member makes the code


reusable. The copy-book can be plugged in any program as
applicable.

3. Structure of files can be typed in a copy-book and it can


be plugged in the required program using COPY command. If
the structure of the file needs to be changed, the change
needs to be done only in the copy-book member. The modified
structure will reflect in all the programs where the copy-
book is copied.

4. COPY command must be typed in Area A.

5. COPY command executes on compilation i.e. the code of the


copy-book member is plugged in the program during
compilation.

_____________________________________________________________________________________
PAGE 98
COBOL Programming

CALL statement

Syntax:
CALL ‘subpgm-pgmid’/ ws-variable
USING BY REFERENCE/CONTENT variable-1, variable-2
ON EXCEPTION
Statement-1
NOT ON EXCEPTION
Statement-2
END-CALL.
Statement-3.

Example:
Main Program: MAINPGM
WORKING-STORAGE SECTION.
77 WS-PGM PIC X(08) VALUE SPACES.
77 WS-NUM1 PIC 9(03) VALUE 0.
77 WS-NUM2 PIC 9(03) VALUE SPACES.
PROCEDURE DIVISION.
MAIN-PARA.
MOVE “SPGM” TO WS-PGM.
CALL WS-PGM USING BY REFERENCE WS-NUM1, WS-NUM2
ON EXCEPTION
DISPLAY “CALL FAILED”
NOT ON EXCEPTION
DISPLAY “CALL SUCCEEDED”
END-CALL.

Sub Program: SUBPGM


WORKING-STORAGE SECTION.
77 WS-TEMP PIC 9(03) VALUE 0.
LINKAGE SECTION.
77 LS-NUM1 PIC 9(03).
77 LS-NUM2 PIC 9(03).
PROCEDURE DIVISION USING LS-NUM1, LS-NUM2.
MAIN-PARA.
MOVE WS-NUM1 TO WS-TEMP.
MOVE WS-NUM2 TO WS-NUM1.
MOVE WS-TEMP TO WS-NUM2.
EXIT PROGRAM.

_____________________________________________________________________________________
PAGE 99
COBOL Programming

Points for Main Program:


1. CALL statement is used to call a sub program. After
executing all the statements of the sub-program, the
control will return back to the next statement after CALL
statement.

2. CALL ‘subpgm-pgmid’ is a static technique of CALL


statement. subpgm-pgmid is the program id mentioned in the
sub program.

3. CALL ws-variable is a dynamic technique of CALL statement.


ws-variable must contain the name of the sub program to be
called.

4. If subpgm-pgmid is invalid or if ws-variable contains an


invalid program name, then it will enter the ON EXCEPTION
BLOCK, execute statement-1 and the control will pass to
statement-3.

5. If subpgm-id is valid or if ws-variable contains a valid


program name, then it will first call the sub program,
execute all the statements of the sub program, return to
the main program, enter NOT ON EXCEPTION block, execute
statement-2 and then the control will pass to statement-3.

6. USING clause is specified to pass variables to a sub


program.

7. USING BY CONTENT var-1 clause will pass var-1 to a sub


program by value. If the sub program modifies the contents
of var-1, on returning back to main program it will have
the original value.

8. USING BY REFERENCE var-1 clause will pass var-1 to a sub


program by reference. If the sub program modifies the
contents of var-1, on returning back to main program it
will have the modified value. If BY CONTENT or BY REFERENCE
is not specified in the statement, the default is BY
REFERENCE.

_____________________________________________________________________________________
PAGE 100
COBOL Programming

Points for Sub program:


1. The variables passed from the main program will be assigned
to the variables declared in PROCEDURE DIVISION USING
statement in the sub program. These variables must be
declared in the LINKAGE SECTION without a VALUE clause.

2. EXIT PROGRAM in the sub program will return the control


back to the main program to the next statement after CALL
statement.

Points for static CALL technique:


1. The program-id of the sub program is specified in CALL
statement.

2. The main program and sub program is compiled first to


create the object module. Both the object modules are link-
edited to create one load module which is named after the
main program.

3. If this technique is used, after creating the load module


of the application, if any change is made in the sub
program, the sub program will have to be recompiled to
create the object module and then the main program and sub
program will have to be link-edited to create the updated
load module. This can be time consuming.

Points for dynamic CALL technique:


1. The name of the sub program is stored in a variable and
this variable is specified in CALL statement. The variable
must contain the load module name of the sub program.

2. The main program is compiled and link-edited to create the


load module. The sub program is then compiled and link-
edited to create the load module of the sub program.

3. If this technique is used, after creating the load module


of main program and sub program, if any change is made in
the sub program, only the sub program will have to be
compiled and link-edited again. This saves time.

_____________________________________________________________________________________
PAGE 101
COBOL Programming

PARM parameter

Points:
1. PARM parameter is mentioned in EXEC statement in RUN jcl.
This parameter is used to pass an input data to a COBOL
program.

2. The input data being passed must be assigned to a variable


in the program specified with PROCEDURE DIVISION USING
statement. This variable must be declared in the LINKAGE
SECTION.

3. The first two bytes of the variable is reserved for COBOL.


The data will get assigned to the variable from the 3rd
position.

Program: To pass an input to a program

RUN JCL
//USERIDX JOB NOTIFY=&SYSUID,TIME=(0,10)
//STEP1 EXEC PGM=PGM1,PARM=’101MAHESH 34’
//SYSIN DD *
/*

COBOL PROGRAM
IDENTIFICATION DIVISION.
PROGRAM-ID. PGM.
ENVIRONMENT DIVISION.
DATA DIVISION.
LINKAGE SECTION.
01 LS-RECORD.
05 FILLER PIC X(02).
05 LS-EMPNO PIC 9(03).
05 LS-NAME PIC X(10).
05 LS-AGE PIC 9(02).
PROCEDURE DIVISION USING LS-RECORD.
MAIN-PARA.
DISPLAY “EMPNO:”, LS-EMPNO.
DISPLAY “NAME:”, LS-NAME.
DISPLAY “AGE:”, LS-AGE.
STOP RUN.

_____________________________________________________________________________________
PAGE 102
COBOL Programming

COBOL File Status Codes

00 File Operation Successful

01 More than one primary key value exists for a given


alternate key with duplicates.

10 End of File

22 Duplicate Primary Key for Index/ Relative Files

23 Key value not found with READ or START.

35 Trying to open a non-existing file.

39 Mismatch in the actual record length/ recording mode


and the record length/ recording mode specified in the
program.

41 Trying to open a file which is already open.

42 Trying to close a file which is already closed.

47 Attempt to read a file not opened in INPUT or I-O


mode.

48 Attempt to write in a file not opened in OUTPUT, I-O,


EXTEND mode.

49 Attempt to delete/rewrite a record in a file not


opened in I-O mode.

_____________________________________________________________________________________
PAGE 103
COBOL Programming

COBOL PROGRAM : COMPILATION AND EXECUTION

COMPILER JCL : COBCOMPL


//USERIDX JOB ACCT#,CLASS=A,NOTIFY=&SYSUID,MSGCLASS=A,PRTY=15
//* USE THIS JCL TO COMPILE LINKEDIT COBOL PGM WITH COPYBOOK **
//**********************************************************
//COBOL EXEC PGM=IGYCRCTL,REGION=2048K,PARM='LIB,LIST,TEST'
//STEPLIB DD DSNAME=IGY.SIGYCOMP,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSUT1 DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT2 DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT3 DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT4 DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT5 DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT6 DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT7 DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSIN DD DSN=USERID.COBOL.PGMS(PGM1),DISP=SHR
//SYSLIB DD DSN=USERID.COBOL.COPYBOOK(PGM1),DISP=SHR
//SYSLIN DD DSNAME=USERID.COBOL.OBJLIB(PGM1),DISP=SHR
//**********************************************************
//LKED EXEC PGM=HEWL,REGION=1024K
//SYSLIB DD DSNAME=CEE.SCEELKED,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSLIN DD DSNAME=USERID.COBOL.OBJLIB(PGM1),DISP=SHR
//SYSLMOD DD DSNAME=USERID.COBOL.LOADLIB(PGM1),DISP=SHR
//SYSUT1 DD UNIT=SYSDA,SPACE=(TRK,(10,10))
//**********************************************************

RUN JCL : RUN


//USERIDX JOB ACCT#,CLASS=A,NOTIFY=&SYSUID,MSGCLASS=A
//STEP1 EXEC PGM=PGM1
//STEPLIB DD DSN=USERID.COBOL.LOADLIB,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSABOUT DD SYSOUT=*
//SYSIN DD *
PROVIDE THE INPUT DATA TO THE PROGRAM HERE
/*
// => NULL STATEMENT IN JCL

_____________________________________________________________________________________
PAGE 104
COBOL Programming

Assumption:
01.COBCOMPL is a member which contains the JCL to compile a
COBOL program.
02.RUN is a member which contains the jcl to execute a COBOL
program.
03.USERID.COBOL.PGMS is a PDS which contains the program
source code typed in a member PGM1.
04.USERID.COBOL.OBJLIB is a PDS which contains the object
module of the program source code. The name of the object
module member is the same as program source code member.
05.USERID.COBOL.LOADLIB is a PDS which will contain the load
module of the program source code. The name of the load
module member is the same as program source code member.
06.USERID.COBOL.COPYLIB is a PDS which will contain the copy
book members to be included in a COBOL program using COPY
command.

Compiler JCL : COBCOMPL


01.This is a two step JCL. Step 1 (COBOL) executes a program
named IGYCRCTL and step 2 (LKED) executes a program named
HEWL.
02.IGYCRCTL is a COBOL compiler. This will compile the program
source code stored in the member specified in SYSIN dd-
name, create the object module of the program with the same
name as the program source code member if there are no
errors in the source code and place the object module in
the PDS specified in SYSLIN dd-name.
03.HEWL will link-edit the member specified in SYSLIN dd-name,
create the load module of the program with the same name as
the program source code member if there are no errors in
the object module and place the load module in the PDS
specified in SYSLMOD dd-name.

RUN JCL : RUN


01.This is a one step JCL. This will execute the load module
specifed in STEP1 dd-name.
02.The load module will be searched in the PDS mentioned in
STEPLIB dd-name.

_____________________________________________________________________________________
PAGE 105

You might also like