You are on page 1of 118

COBOL (COMMON

BUSINESS ORIENTED
LANGUAGE)

Overview
COBOL Fundamentals

DAY5
FILES

FIXED RECORD LENGTH FILE


EMP-NO ENAME EAGESALARY DEPTNO

1-------1011-----------------25-28------35--38

1000000345RAJESH KRISHNAMOORTHY03308900.50030

1000000346RANDY DEDOSAUZ 04407900.50030

1000000347MAHESH RAMARAJAN 05509900.50030

VARIABLE RECORD LENGTH


FILE

SALESIDSNAME QTY1 LOC1 PRICE1 QTY2 LOC2 PRICE2 QTY3 LOC3 PRICE3 QTY4 LOC4 PRICE4

S101RAJESH 0030 IL 5000 0040 MI 4000 0050 NY 5000 60 CA 6000

S102RAMESH 0040 PA 4000 0060 WI 6000

S103RAGAVAN 0080 MI 8000


Introduction to File processing – Basic terms

 Field Field type and Field size.

 Record Record-Size, Fixed length records and


Variable length records.

 File Master files, Transaction files.


ACCOUNT File – Master File
Payment History- Transaction File
Files, Records,
Fields.

 We use the term FIELD to


describe an item of information
DATA DIVISION.
we are recording about an object
FILE SECTION. (e.g. StudentName,
FD STUDENT-FILE.
DateOfBirth, CourseCode).
01 STUDENT-REC.
 We use the term RECORD to
05 STUD-NAME PIC X(25).
05 DOB PIC X(10). describe the collection of fields
05 COURSE-CODE PIC X(15). which record information about
05 COURSE-NM PIC X(25).
an object
05 COURSE-TYPE PIC X(5).
(e.g. a StudentRecord is a
collection of fields
recording information
about a student).

 We use the term FILE to describe


a collection of one or more
occurrences (instances) of a
Files, Records,
Fields.

STUDENTS
StudId StudName DateOfBirth
9723456 COUGHLAN 10091961
9724567 RYAN 31121976
9534118 COFFEY 23061964 occurrences
9423458 O'BRIEN 03111979
9312876 SMITH 12121976

DATA DIVISION. Record Type


FILE SECTION. (Template)
FD StudentFile. (Structure)
01 StudentDetails.
02 StudId PIC 9(7).
02 StudName PIC X(8).
02 DateOfBirth PIC X(8).
How files are
processed.

Read a record
Based on Structure

Rec1 Read
OPEN Process Store in
Rec2 Record
FILE (One at Record Output File
Rec3
Rec4 A Time )
Record
Buffers

Program
IDENTIFICATION DIVISION.
etc.
ENVIRONMENT DIVISION.
etc.
Record Instance DATA DIVISION.
DISK FILE SECTION.

STUDENTS RecordBuffer
Declaration
Creating a Student
Record

Student Details.
01 StudentDetails.
 Student Id. 02 StudentId PIC 9(7).
 Student Name. 02 StudentName.
Surname 03 Surname PIC X(8).
Initials 03 Initials PIC XX.
 Date of Birth 02 DateOfBirth.
Year of Birth 03 YOBirth PIC 99.
Month of Birth 03 MOBirth PIC 99.
Day of Birth 03 DOBirth PIC 99.
 Course Code 02 CourseCode PIC X(4).
 Value of grant 02 Grant PIC 9(4).
 Gender 02 Gender PIC X.
Describing the record buffer in
COBOL
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 DateOfBirth.
03 YOBirth PIC 9(2).
03 MOBirth PIC 9(2).
03 DOBirth PIC 9(2).
02 CourseCode PIC X(4).
02 Grant PIC 9(4).
02 Gender PIC X.

 The record type/template/buffer of every file used in


a program must be described in the FILE SECTION by
means of an FD (file description) entry.

 The FD entry consists of the letters FD and an


internal file name.
The Select and Assign
Clause.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StudentFile
ASSIGN TO DDNAME.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
Physical File 01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
STUDENTS 03 Initials PIC XX.
02 DateOfBirth.
03 YOBirth PIC 9(2).
03 MOBirth PIC 9(2).
03 DOBirth PIC 9(2).
02 CourseCode PIC X(4).
02 Grant PIC 9(4).
02 Gender PIC X.

 The internal file name used in the FD entry is connected to


an external file (on disk or tape) by means of the Select
and Assign clause.
Select and Assign
Syntax.

SELECT FileName ASSIGN TO ExternalFileReference


 LINE 
[ORGANIZATION IS   SEQUENTIAL].
 RECORD 

 LINE SEQUENTIAL means each record is followed


by the carriage return and line feed characters.

 RECORD SEQUENTIAL means that the file consists


of a stream of bytes. Only the fact that we know
the size of each record allows us to retrieve them.
COBOL file handling
Verbs

 OPEN
Before your program can access the data in an input file or place data in an
output file you must make the file available to the program by OPENing it.

 READ
The READ copies a record occurrence/instance from the file and places it in
the record buffer.

 WRITE
The WRITE copies the record it finds in the record buffer to the file.

 CLOSE
You must ensure that (before terminating) your program closes all the files it
has opened. Failure to do so may result in data not being written to the file or
users being prevented from accessing the file.
OPEN and CLOSE verb
syntax

  INPUT  
  
OPEN   OUTPUT  InternalFileName ...
  EXTEND  
  
 When you open a file
Only used for you have to indicate to
READing Records the system what how
INPUT Moden
you want to use it (e.g.
In A File
INPUT, OUTPUT,
EXTEND) so that the
Only used for WRITing system can manage the
file correctly.
output Mode Records In A File
 Opening a file does not
transfer any data to the
Only used for APPENDing
record buffer, it simply
Extend Mode Records at end of File provides access.
READ verb
syntax

READ FILE-NAME { INTO IDENTIFIER }

AT END { Impreative Statement }

 The InternalFilename specified must be a file that


has been OPENed for INPUT.

 Using INTO Identifier clause causes the data to be


read into the record buffer and then copied from
there to the specified Identifier in one operation.
 When this option is used there will be two copies
of the data. It is the equivalent of a READ
followed by a MOVE.

 AT END clause it find out whether Read Operation


has reached END OF File .
WRITE
Syntax.

WRITE record-name FROM IDENTIFIER .


 To WRITE data to a file move the data to the
record buffer (declared in the FD entry) and then
WRITE the contents of record buffer to the file.
How the WRITE
works

OPEN OUTPUT StudentFile.


MOVE "9334567Frank Curtain LM051" TO StudentDetails.
WRITE StudentDetails.
MOVE "9383715Thomas Healy LM068" TO StudentDetails.
WRITE StudentDetails.
CLOSE StudentFile.
STOP RUN.

StudentRecord
StudentID StudentName Course.
9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1

Students.Dat
9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1
EO
F
How the WRITE
works

OPEN OUTPUT StudentFile.


MOVE "9334567Frank Curtain LM051" TO StudentDetails.
WRITE StudentDetails.
MOVE "9383715Thomas Healy LM068" TO StudentDetails.
WRITE StudentDetails.
CLOSE StudentFile.
STOP RUN.

StudentRecord
StudentID StudentName Course.
9 3 8 3 7 1 5 T h o ma s H e a l y L M 0 6 8

Students.Dat
9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1
9 3 8 3 7 1 5 T h o ma s H e a l y L M 0 6 8
EOF
IDENTIFICATION DIVISION.
PROGRAM-ID. SeqWrite.
ENVIRONMENT DIVISION. Assign File To DDNAME
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO DDNAME (Physical Sequential File)
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails. Declaring File Structure
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 CourseCode PIC X(4).
02 Gender PIC X.
WORKING-STORAGE SECTION. Opened the File For Writing Recs
01 CHOICE PIC X(1) VALUE ‘Y’.
PROCEDURE DIVISION.
001-MAIN-PARA.
OPEN OUTPUT StudentFile.
DISPLAY "Enter student details Enter the choice for Continue ".
PERFORM GetStudentDetails.
PERFORM UNTIL CHOICE = ‘N’
WRITE StudentDetails
PERFORM GetStudentDetails Write a Record in a File
END-PERFORM.
CLOSE StudentFile.
STOP RUN.
GetStudentDetails.
DISPLAY ‘ENTER STUDENT-ID’. Getting values for
ACCEPT STUDENTID.
DISPLAY ‘ENTER STUDENT NAME with Intial:’. Records to be written
ACCEPT STUDENTNAME.
DISPLAY ‘ENTER COURSECODE’.
ACCEPT COURSECODE. In File
DISPLAY ‘ENTER GENDER:’.
ACCEPT GENDER.
DISPLAY ‘ENTER THE CHOICE OF CONTINUATION (Y/N):’.
ACCEPT CHOICE.
Sequential write
Write cont..
IDENTIFICATION DIVISION.
PROGRAM-ID. SeqRead.
AUTHOR. Michael Coughlan. Assign File To DDNAME
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION. (Physical Sequential File)
FILE-CONTROL.
SELECT StudentFile ASSIGN TO dd-name
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION. Declaring File Structure
FD StudentFile.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 CourseCode PIC X(4). Opened the File For Reading Recs
02 Gender PIC X.
PROCEDURE DIVISION.
Begin.
OPEN INPUT StudentFile
READ StudentFile
AT END MOVE HIGH-VALUES TO StudentDetails Read a Record in a File till
END-READ
PERFORM UNTIL StudentDetails = HIGH-VALUES
DISPLAY StudentId SPACE StudentName SPACE CourseCode END OF File is reached
READ StudentFile
AT END MOVE HIGH-VALUES TO StudentDetails
END-READ
END-PERFORM
CLOSE StudentFile
STOP RUN.
Display values at SPOOL

Close a File which Is Opened


How the READ
works

StudentRecord
StudentID StudentName Course.
9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1

9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1
9 3 8 3 7 1 5 T h o ma s H e a l y L M 0 6 8
9 3 4 7 2 9 2 T o n y O ‘ B r i a n L M 0 5 1
9 3 7 8 8 1 1 B i l l y D o w n e s L M 0 2 1
EOF

PERFORM UNTIL StudentRecord = HIGH-VALUES


READ StudentRecords
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ
END-PERFORM.
How the READ
works

StudentRecord
StudentID StudentName Course.
9 3 8 3 7 1 5 T h o ma s H e a l y L M 0 6 8

9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1
9 3 8 3 7 1 5 T h o ma s H e a l y L M 0 6 8
9 3 4 7 2 9 2 T o n y O ‘ B r i a n L M 0 5 1
9 3 7 8 8 1 1 B i l l y D o w n e s L M 0 2 1
EOF

PERFORM UNTIL StudentRecord = HIGH-VALUES


READ StudentRecords
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ
END-PERFORM.
How the READ
works

StudentRecord
StudentID StudentName Course.
9 3 4 7 2 9 2 T o n y O ‘ B r i a n L M 0 5 1

9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1
9 3 8 3 7 1 5 T h o ma s H e a l y L M 0 6 8
9 3 4 7 2 9 2 T o n y O ‘ B r i a n L M 0 5 1
9 3 7 8 8 1 1 B i l l y D o w n e s L M 0 2 1
EOF

PERFORM UNTIL StudentRecord = HIGH-VALUES


READ StudentRecords
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ
END-PERFORM.
How the READ
works

StudentRecord
StudentID StudentName Course.
9 3 7 8 8 1 1 B i l l y D o w n e s L M 0 2 1

9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1
9 3 8 3 7 1 5 T h o ma s H e a l y L M 0 6 8
9 3 4 7 2 9 2 T o n y O ‘ B r i a n L M 0 5 1
9 3 7 8 8 1 1 B i l l y D o w n e s L M 0 2 1
EOF

PERFORM UNTIL StudentRecord = HIGH-VALUES


READ StudentRecords
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ
END-PERFORM.
How the READ
works

StudentRecord
StudentID StudentName Course.
                        

HIGH-VALUES
9 3 3 4 5 6 7 F r a n k C u r t a i n L M 0 5 1
9 3 8 3 7 1 5 T h o ma s H e a l y L M 0 6 8
9 3 4 7 2 9 2 T o n y O ‘ B r i a n L M 0 5 1
9 3 7 8 8 1 1 B i l l y D o w n e s L M 0 2 1
EOF

PERFORM UNTIL StudentRecord = HIGH-VALUES


READ StudentRecords
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ
END-PERFORM.
Sequential read.
SEQUENTIAL READ Program cont..
REWRITE verb

REWRITE is used to update an existing record in the file which has been
read in the program

Syntax

REWRITE record-name [FROM data-name]


[END-REWRITE]

Note:
The REWRITE statement can only be used if the file is opened in the I-O
mode and its execution must be preceded by the successful READ
statement on the file.

The REWRITE statement replaces last read record


REWRITE

 If a file is opened in the I-O mode and a record has been read
successfully into the record buffer, then we
 can use the REWRITE statement to update an existing record.
Similar to the WRITE statement, the REWRITE
 statement can be used with FROM option for writing data
directly from a WORKING-STORAGE variable to the
 required file.
IDENTIFICATION DIVISION.
PROGRAM-ID. SeqWR.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO DDNAME
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 CourseCode PIC X(4).
02 Gender PIC X.

PROCEDURE DIVISION.
Begin.
OPEN INPUT-OUTPUT StudentFile
READ StudentFile
AT END MOVE HIGH-VALUES TO StudentDetails
END-READ
IF STUDENTID = S10012
MOVE ‘C108’ TO Coursecode
REWRITE STUDENTDETAILS
END-IF.
PERFORM UNTIL StudentDetails = HIGH-VALUES
DISPLAY StudentId SPACE StudentName SPACE CourseCode
READ StudentFile
AT END MOVE HIGH-VALUES TO StudentDetails
END-READ
IF STUDENTID = S10012
MOVE ‘C108’ TO Coursecode
REWRITE STUDENTDETAILS
END-IF
END-PERFORM
CLOSE StudentFile
STOP RUN.
Sequential Re-write
SEQUENTIAL REWRITE Cont….
The CALL Verb
Objectives

 Define CALL statement


 CALL BY CONTENT/REFERENCE
 Define LINKAGE SECTION
 Cover LINKAGE SECTION & JCL Parameters
CALL Syntax

CALL statement transfers control from one object program to


another within run unit
CALL Example

CALL “PROGA” USING….


ON EXCEPTION SET GOOD-CALL TO TRUE
NOT ON EXCEPTION SET BAD-CALL TO TRUE
END-CALL
CALL BY CONTENT/REFERENCE

 CALL BY REFERENCE technique allows the sub-program to access


and process the data-items in the caller’s storage

 CALL BY CONTENT technique allows the sub-program to access and


process a copy of the data-items from the caller’s storage. The sub-
program can not change the original data values in the caller’s
storage
LINKAGE SECTION

 The LINKAGE SECTION of DATA DIVISION describes data made


available from another program

 Storage is not RESERVED

 Value clause can not be specified for items other than level-88 items

 EXTERNAL clause can not be specified in LINKAGE SECTION


CALL with LINKAGE Example

DATA DIVISION.

WORKING-STORAGE SECTION.
PROGA
01 PARM-LIST.
05 PART-NO PIC X(4).
LINKAGE SECTION.
05 USA-SALES PIC 9(5).
01 USING-LIST.

10 PART-ID PIC X(4).
PROCEDURE DIVISION.
10 SALES PIC 9(5).

CALL “PROGA”
PROCEDURE DIVISION
USING PARM-LIST.
USING USING-LIST.

….
GOBACK.
LINKAGE SECTION & JCL Parameters

 User-parameters can be passed to the COBOL program being


executed via the PARM parameter of the EXEC statement:
//STEPNAM EXEC PGM=XXXX,…,
// PARM=‘USER-PARAMETER’

 Access to the user-parameter string requires LINKAGE SECTION


definitions:
CALL
Parameters

CALL "ProgramName" USING P1, P2, P3, P4.

PROCEDURE DIVISION USING P2, P4, P1, P3.


CALL
Parameters

CALL "ProgramName" USING P1, P2, P3, P4.

PROCEDURE DIVISION USING P2, P4, P1, P3.

Positions Correspond - Not Names


Parameter Passing
Mechanisms

CALL .. BY CALLed
REFERENCE Program
Parameter Passing
Mechanisms
Address of
Data Item
CALL .. BY CALLed
REFERENCE Direction Program
of Data Flow
Parameter Passing
Mechanisms
Address of
Data Item
CALL .. BY CALLed
REFERENCE Direction Program
of Data Flow

CALL .. BY CALLed
CONTENT Program

Copy of
Data Item
Parameter Passing
Mechanisms
Address of
Data Item
CALL .. BY CALLed
REFERENCE Direction Program
of Data Flow

Direction
CALL .. BY of Data Flow CALLed
CONTENT Program

Data Address of
Item
Copy of Copy
Data Item
Passing by reference and value

COBOL provides two ways of passing parameters to


the called program
using the CALL statement. They are -

1) By REFERENCE

 CALL SUBPGM1 USING WS-NUM1.


2) BY VALUE

 CALL SUBPGM1 USING


BY CONTENT WS-NUM1
BY REFERENCE WS-NUM2
BY REFERENCE WS-NUM3.
CALL

Example
SAMPLE CALL
PROGRAM

Overview
CALLING – CALLED program. An
example
The CALLING program
CALLING – CALLED program. An
example
The CALLED program
Define variables
passed by the calling
program in the
linkage section

Procedure division
We can use the variables
statement modified to
passed by the calling
include the passed
program, in the procedure
variables from the calling
division of the called
program in the same
program. The name need
sequence as given in the
not be the same.
call statement
CALLING – CALLED program. An
example

Output spool display


CALL - Types

Basically there are two types of calls –

c) STATIC call
A static call occurs when you use the CALL
statement in a program that is compiled
with the NODYNAM compiler option. Regardless of
whether it is called or not, a
statically called program is loaded into storage
CALL TYPES (DYNAMIC call )

 A dynamic call occurs when you use the CALL


statement in a program compiled with the DYNAM
compiler option.

 In this case the CALLing and CALLed programs are


each processed separately by the link-editor. A
dynamically called program is loaded only when it is
called at run time.

 We generally use a dynamic call statement when


you are concerned about ease of maintenance.
Applications do not have to be re-link-edited when
Dynamically called subprograms are changed.
Dynamic vs. Static Calls

 Dynamic calls take more processing than static calls.


However, a dynamic call may use less total storage than a
static call.
 A statically called program cannot be deleted, but a
dynamically called program can be deleted.
 Using a dynamic call and then a CANCEL statement to delete
the dynamically called program after it is no longer needed in
the application (and not after each CALL to it) may require
less storage than using a static call.
 Use Dynamic CALL if the subprograms are very large or less
frequently used (called only on a few conditions).
 In such case, the use of static calls might require too much
main storage. Less total storage may be required to call and
cancel one, then call and cancel another, THAN to statically
call both.
STATIC CALL EXAMPLE

Overview
Static Call – Simple Program
STATIC CALL: Main Program
STATIC CALL: Compile JCL
STATIC CALL: RUN JCL
Dynamic call :Sub Program
Dynamic Call : Main Program
Dynamic Call Main Pgm Compile JCL
DYNAMIC CALL MAIN PROGRAM RUN
JCL
Rules for coding CALLed programs

 The called program needs to have a LINKAGE SECTION. This


must appear after the WORKING-STORAGE SECTION in the
DATA DIVISION. The variables in the linkage section have to
independent items.
 The PROCEDURE DIVISION needs to have a USING clause.
This identifies the variables passed to the program and
their ordering.
 Entries in the LINKAGE SECTION can be in any order, but
the entries in the USING clause must be in the order of
their usage in the CALL statement of the CALLing program.
 Instead of a STOP RUN statement, the called program must
contain an EXIT PROGRAM statement to transfer control
back to the calling program.
SORTING AND MERGING
DATA FILES

Overview
COBOL –Sorting

 Records in files must be sorted into specific sequences for


Updating, Querying or Generating Reports.

 Sorting is a common procedure used for arranging


records into a specific order so that sequential
processing can be performed.

 Sorting is done on the basis of a key field


COBOL –
Sort syntax
SORT File-name-1

{ ON DESCENDING KEY Data-name-1. . . }


ASCENDING

USING File-name-2
GIVING File-name-3.

 Multiple keys can be used for sorting.


 Records may be sorted using either numeric or non-numeric key
fields.
COBOL –
Sort e.g.

SORT SORT-FILE
ON ASCENDING KEY EMP-NO
ON ASCENDING KEY E-NAME
ON ASCENDING KEY E-LEVEL
USING INPUT-FILE
GIVING OUTPUT-FILE.

 INPUT FILE : File of unsorted records.


 SORT FILE : File for temporary storage during sorting.
 OUTPUT FILE : File of sorted output records.
COBOL –Sort – SD

 Sort file is defined with an SD entry and has no label


records clause.

E.g.:

SD SORT-FILE.
01 SORT-REC.
05 S-DEPTNO PIC 99.
05 S-DEPTNAME PIC X(10).
COBOL –
Sort – Input Procedure
 Sort statement can also be used to perform some processing of
incoming records just before they are sorted.

 The input procedure processes data from the incoming file prior to
sorting.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 DateOfBirth.
03 YOBirth PIC 9(2).
03 MOBirth PIC 9(2).
03 DOBirth PIC 9(2).
02 CourseCode PIC X(4).
02 Grant PIC 9(4).
02 Gender PIC X.
 The StudentFile is a sequential file sequenced upon
ascending StudentId.
 Write a program to display the number of students
taking each course. How?
Simplified Sort
Syntax.

 The WorkFileName identifies a temporary work file that the


SORT process uses for the sort. It is defined in the FILE
SECTION using an SD entry.

 Each SortKeyIdentifier identifies a field in the record of the


work file upon which the file will be sequenced.

 When more than one SortKeyIdentifier is specified, the keys


decrease in significance from left to right (leftmost key is most
significant, rightmost is least significant).

 InFileName and OutFileName, are the names of the input and


output files. These files are automatically opened by the SORT.
When the SORT executes they must not be already open.
Sort
Example.
FD SalesFile.
01 SalesRec.
02 FILLER PIC X(10).
SD WorkFile.
01 WorkRec.
02 WSalesmanNum PIC 9(5).
02 FILLER PIC X(5).
FD SortedSalesFile.
01 SortedSalesRec.
02 SalesmanNum PIC 9(5).
02 ItemType PIC X.
02 QtySold PIC 9(4).
PROCEDURE DIVISION.
Begin.
SORT WorkFile ON ASCENDING KEY WSalesmanNum
USING SalesFile
GIVING SortedSalesFile.
OPEN INPUT SortedSalesFile.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT WorkFile ASSIGN TO DDNAME

SD WorkFile.
01 WorkRecord.
02 ProvinceCode PIC 9.
02 SalesmanCode PIC 9(5).
02 FILLER PIC X(19).

PROCEDURE DIVISION.
Begin.
SORT WorkFile ON ASCENDING KEY ProvinceCode
DESCENDING KEY SalesmanCode
USING UnsortedSales
GIVING SortedSales.

OPEN INPUT SortedSales.


How the SORT
works.

SalesFile SortedSalesFile

Unsorted Sorted
Records Records
SORT
Process

WorkFile

SORT WorkFile ON ASCENDING KEY


WSalesmanNum
USING SalesFile
GIVING SortedSalesFile.
INPUT FILE
OUTPUT File
SIMPLE SORT PROGRAM
SIMPLE SORT PROGRAM
RUN JCL
SIMPLE SORT OUTPUT
COMPLEX SORT

Overview
COBOL – SORT
Input Procedure – Syntax

SORT file-name-1
{ ON ASCENDING KEY data-name-1. . . }. . .
DESCENDING

{ INPUT PROCEDURE IS procedure-name-1


[ {THRU / THROUGH} ] procedure-name-2 ] }

{ USING file-name-2 . . . }

GIVING file-name-3.
How the INPUT PROCEDURE
works.

SalesFile SortedSalesFile

Unsorted Sorted
Records Records
Unsorted
Hat SORT
Records
Process

SelectHatSales
WorkFile

SORT WorkFile ON ASCENDING KEY WSalesmanNum


INPUT PROCEDURE IS SelectHatSales
GIVING SortedSalesFile.
INPUT PROCEDURE
Template

OPEN INPUT InFileName


READ InFileName RECORD
PERFORM UNTIL Condition
RELEASE SDWorkRec
READ InFileName RECORD
END-PERFORM
CLOSE InFile
INPUT PROCEDURE - Example
FD SalesFile.
01 SalesRec.
88 EndOfSales VALUE HIGH-VALUES.
02 FILLER PIC 9(5).
02 FILLER PIC X.
88 HatRecord VALUE "H".
02 FILLER PIC X(4).
SD WorkFile.
01 WorkRec.
02 WSalesmanNum PIC 9(5).
02 FILLER PIC X(5).
FD SortedSalesFile.
01 SortedSalesRec.
02 SalesmanNum PIC 9(5).
02 ItemType PIC X.
02 QtySold PIC 9(4).
PROCEDURE DIVISION.
Begin.
SORT WorkFile ON ASCENDING KEY WSalesmanNum
INPUT PROCEDURE IS SelectHatSales
GIVING SortedSalesFile.
COBOL – SORT
Input Procedure – e.g.
SORT WorkFile ON ASCENDING KEY WSalesmanNum
INPUT PROCEDURE IS SelectHatSales
GIVING SortedSalesFile.

In the paragraph check-valid-para :


 Open input file.
 Check for validity
 Release the record
 Close the file

After that control is passed to SORT.


COBOL – SORT Release

 The input procedure opens the input file, processes input


records and releases them into the sort file. It is similar
to writing a record to the sort file.

 The format of RELEASE is :

RELEASE Sort-record-name-1
[ FROM Identifier-1 ]
COBOL – SORT Release (cont’d)

For releasing the processed record for Sorting :

 Move input record to the sort record.


 Release each sort record, which makes it available for
sorting.

E.g.

RELEASE-PARA.
MOVE IN-REC TO SORT-REC.
RELEASE SORT-REC.
COBOL – SORT Typical Program

MAIN-PARA.
SORT SORT-FILE ON ASCENDING KEY ORDER-NO
INPUT PROCEDURE TEST-PARA GIVING OUT-FILE.
STOP RUN.
TEST-PARA.
OPEN INPUT IN-FILE.
PERFORM PROCESS-PARA UNTIL NO-MORE-RECORDS = ‘NO’
CLOSE IN-FILE.

PROCESS-PARA.
READ IN-FILE AT END MOVE ‘NO’ TO NO-MORE-RECORDS.
IF QTY = ZEROS
CONTINUE
ELSE
MOVE IN-REC TO SORT-REC
RELEASE SORT-REC
END-IF.
Complex Sort :INPUT FILE
COMPLEX SORT:INPUT PROCEDURE
COMPLEX SORT:INPUT PROCEDURE
COMPLEX SORT: INPUT PROCEDURE
COMPLEX SORT:RUN JCL
COMPLEX SORT : OUTPUT FILE
COMPLEX SORT : Input File
COBOL SORT: OUTPUT
PROCEDURE
COBOL – SORT Output Procedure

 In case of sort if the giving option is used, then the sorted records
are automatically written onto the out-file after they are used.

 Instead of giving option an output procedure can be used.

 In an input procedure we RELEASE records to a sort file rather than


WRITING them. In an output procedure we RETURN records from the
sort file rather than READING them.
COBOL – SORT
Output Procedure – Syntax

SORT file-1 { ON DESCENDING KEY data-name-1..}


ASCENDING

{OUTPUT PROCEDURE IS proc-3 }


{GIVING file-2 . . . }
COBOL – SORT
Return

 Records are returned from the sort file using RETURN statement.

RETURN SORT-FILE-NAME-1
AT END <Imperative statement-1>
[ NOT AT END <imperative statement-
2> ]
[END-RETURN].
COBOL – SORT
Output Procedure – e.g.

MAIN-PARA.
SORT WORK-FILE
USING IN-FILE
OUTPUT PROCEDURE CHECK-PARA.
STOP RUN.

 In the paragraph CHECK-PARA:


 Open output file.
 Return records from sort file.
 Process records before writing to Out-file.
 Close the file.
COBOL – SORT
Output Procedure – e.g.

After the records have been sorted but before they are written into
the output file:

 Move sort record to the output area.


 Write each sort record to the output file.

E.g.:
WRITE-PARA.
WRITE SORT-REC FROM WORK-REC.
COBOL – SORT
Typical Program
MAIN-PARA.
SORT SORT-FILE ON ASCENDING KEY TRANS-NO
USING INPUT-FILE
OUTPUT PROCEDURE CALC-PARA.
STOP RUN.
CALC-PARA.
OPEN OUTPUT OUTPUT-FILE.
PERFORM PROCESS-PARA UNTIL NO-MORE-RECORDS = ‘NO’
CLOSE OUTPUT-FILE.

PROCESS-PARA.
RETURN SORT-FILE AT END MOVE ‘NO’ TO NO-MORE-RECORDS.
IF AMT-OF-PURCHASE > 6000
MOVE 0.02 TO DISCOUNT
ELSE
MOVE 0.00 TO DISCOUNT
END-IF.
WRITE OUT-REC FROM SORT-REC.
COMPLEX SORT: Output Procedure
Complex Sort : Output Procedure
COMPLEX SORT : OUTPUT
PROCEDURE
COMPLEX SORT : RUN JCL
COMPLEX SORT: OUTPUT DATA
COBOL – Merge

 COBOL has a MERGE statement that will combine two or


more files into a single file.

 The MERGE statement automatically handles the opening,


closing, and any I-O (read/write functions) associated with
the files.

 The files to be merged must be in sequence by the key-field


(ascending or descending).
COBOL – Merge
Merge syntax

MERGE file-1 { ON ASCENDING KEY data -1}


DESCENDING
USING file-2 { file-3 } . . .
OUTPUT PROCEDURE IS proc-1
GIVING {file-4}.
COBOL – Merge Typical Program
FILE CONTROL.
SELECT IN-FILE1 ASSIGN TO E-FILE1.
SELECT IN-FILE2 ASSIGN TO E-FILE2.
SELECT M-FILE ASSIGN TO WORK.
SELECT OUT-FILE ASSIGN TO E-FILE.
DATA DIVISION.
FD IN-FILE1.
01 IN-REC1 PIC X(100).
FD IN-FILE2.
01 IN-REC2 PIC X(100).
SD M-FILE.
01 M-REC.
05 KEY-FIELD PIC X(5).
05 REST-OF REC PIC X(100).
FD OUT-FILE.
01 OUT-REC PIC X(100).
PROCEDURE DIVISION.
MAIN-PARA.
MERGE M-FILE ON ASCENDING KEY KEY-FIELD
USING IN-FILE1, IN-FILE2
GIVING OUT-FILE
STOP RUN
MERGE EXAMPLE

Overview
COBOL – Sort & Merge Summary

 SORT is used for sorting records in either ascending or descending


order

 Processing of records can be carried out before or after sorting by


using Input or Output procedures or using both

 Merge is used to merge two or more files


END OF SORT & MERGE

You might also like