Transcript

COBOL (COMMON BUSINESS ORIENTED

LANGUAGE)

Overview

COBOL Fundamentals

DAY5

EMP-NO ENAME EAGESALARY DEPTNO

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

1000000345RAJESH KRISHNAMOORTHY03308900.50030

1000000346RANDY DEDOSAUZ 04407900.50030

1000000347MAHESH RAMARAJAN 05509900.50030

FILES

FIXED 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

VARIABLE RECORD LENGTH FILE

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

Introduction to File processing – Basic terms

Files, Records, Fields.

We use the term FIELD to describe an item of information we are recording about an object

(e.g. StudentName, DateOfBirth, CourseCode).

We use the term RECORD to describe the collection of fields which record information about an object

(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 record type (template).

DATA DIVISION.

FILE SECTION.FD STUDENT-FILE.

01 STUDENT-REC.

05 STUD-NAME PIC X(25). 05 DOB PIC X(10). 05 COURSE-CODE PIC X(15). 05 COURSE-NM PIC X(25). 05 COURSE-TYPE PIC X(5).

Files, Records, Fields.

StudId StudName DateOfBirthStudId StudName DateOfBirth9723456 COUGHLAN 100919619724567 RYAN 311219769534118 COFFEY 230619649423458 O'BRIEN 031119799312876 SMITH 12121976

StudId StudName DateOfBirthStudId StudName DateOfBirth9723456 COUGHLAN 100919619724567 RYAN 311219769534118 COFFEY 230619649423458 O'BRIEN 031119799312876 SMITH 12121976

STUDENTSSTUDENTS

DATA DIVISION.FILE SECTION.FD StudentFile.01 StudentDetails. 02 StudId PIC 9(7). 02 StudName PIC X(8). 02 DateOfBirth PIC X(8).

DATA DIVISION.FILE SECTION.FD StudentFile.01 StudentDetails. 02 StudId PIC 9(7). 02 StudName PIC X(8). 02 DateOfBirth PIC X(8).

occurrencesoccurrences

Record Type Record Type (Template)(Template)(Structure)(Structure)

How files are processed.

Rec1Rec2Rec3Rec4

OPEN

FILE

Read

Record

(One at

A Time )

Process

Record

Store inOutput File

Read a record

Based on Structure

Record Buffers

IDENTIFICATION DIVISION.etc.ENVIRONMENT DIVISION.etc.DATA DIVISION.FILE SECTION.

ProgramProgram

RecordBufferRecordBuffer DeclarationDeclaration

STUDENTS

DISK Record Instance

Creating a Student Record

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.

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.

Student Details.Student Details.

Describing the record buffer in COBOL

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.

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.

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.

STUDENTS

The Select and Assign Clause.

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.

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT StudentFile ASSIGN TO DDNAME.

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.

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT StudentFile ASSIGN TO DDNAME.

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.

Physical File

Select and Assign Syntax.

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.

SELECT FileName ASSIGN TO ExternalFileReference

[ORGANIZATION IS LINE

RECORD SEQUENTIAL].

COBOL file handling Verbs

OPENBefore 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.

READThe 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.

CLOSEYou 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

When you open a file you have to indicate to the system what how you want to use it (e.g. INPUT, OUTPUT, EXTEND) so that the system can manage the file correctly.

Opening a file does not transfer any data to the record buffer, it simply provides access.

OPEN InternalFileName ...

INPUT

OUTPUT

EXTEND

INPUT Moden

output Mode

Extend Mode

Only used for

READing Records

In A File

Only used for WRITing

Records In A File

Only used for APPENDing

Records at end of File

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.

FF rr aa nn kk CC uu rr tt aa ii nn99 33 33 44 55 66 77 LL MM 00 55 11

StudentID StudentName Course.

StudentRecord

FF rr aa nn kk CC uu rr tt aa ii nn99 33 33 44 55 66 77 LL MM 00 55 11

EOF

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.

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

Students.Dat

TT hh oo mm aa ss HH ee aa ll yy 99 33 88 33 77 11 55 LL MM 00 66 88

StudentID StudentName Course.

StudentRecord

F r a n k C u r t a i n9 3 3 4 5 6 7 L M 0 5 1TT hh oo mm aa ss HH ee aa ll yy99 33 88 33 77 11 55 LL MM 00 66 88

EOF

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.

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

Students.Dat

IDENTIFICATION DIVISION.PROGRAM-ID. SeqWrite.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.WORKING-STORAGE SECTION.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 END-PERFORM. CLOSE StudentFile. STOP RUN.

GetStudentDetails. DISPLAY ‘ENTER STUDENT-ID’. ACCEPT STUDENTID. DISPLAY ‘ENTER STUDENT NAME with Intial:’. ACCEPT STUDENTNAME. DISPLAY ‘ENTER COURSECODE’. ACCEPT COURSECODE. DISPLAY ‘ENTER GENDER:’. ACCEPT GENDER. DISPLAY ‘ENTER THE CHOICE OF CONTINUATION (Y/N):’. ACCEPT CHOICE.

IDENTIFICATION DIVISION.PROGRAM-ID. SeqWrite.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.WORKING-STORAGE SECTION.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 END-PERFORM. CLOSE StudentFile. STOP RUN.

GetStudentDetails. DISPLAY ‘ENTER STUDENT-ID’. ACCEPT STUDENTID. DISPLAY ‘ENTER STUDENT NAME with Intial:’. ACCEPT STUDENTNAME. DISPLAY ‘ENTER COURSECODE’. ACCEPT COURSECODE. DISPLAY ‘ENTER GENDER:’. ACCEPT GENDER. DISPLAY ‘ENTER THE CHOICE OF CONTINUATION (Y/N):’. ACCEPT CHOICE.

Assign File To DDNAME

(Physical Sequential File)

Declaring File Structure

Opened the File For Writing Recs

Getting values for

Records to be written

In File

Write a Record in a File

Sequential write

Write cont..

IDENTIFICATION DIVISION.PROGRAM-ID. SeqRead.AUTHOR. Michael Coughlan.

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT StudentFile ASSIGN TO dd-name

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 StudentFile READ StudentFile AT END MOVE HIGH-VALUES TO StudentDetails END-READ PERFORM UNTIL StudentDetails = HIGH-VALUES DISPLAY StudentId SPACE StudentName SPACE CourseCode READ StudentFile AT END MOVE HIGH-VALUES TO StudentDetails END-READ END-PERFORM CLOSE StudentFile STOP RUN.

IDENTIFICATION DIVISION.PROGRAM-ID. SeqRead.AUTHOR. Michael Coughlan.

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT StudentFile ASSIGN TO dd-name

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 StudentFile READ StudentFile AT END MOVE HIGH-VALUES TO StudentDetails END-READ PERFORM UNTIL StudentDetails = HIGH-VALUES DISPLAY StudentId SPACE StudentName SPACE CourseCode READ StudentFile AT END MOVE HIGH-VALUES TO StudentDetails END-READ END-PERFORM CLOSE StudentFile STOP RUN.

Assign File To DDNAME

(Physical Sequential File)

Declaring File Structure

Opened the File For Reading Recs

Display values at SPOOL

Read a Record in a File till

END OF File is reached

Close a File which Is Opened

PERFORM UNTIL StudentRecord = HIGH-VALUES READ StudentRecords AT END MOVE HIGH-VALUES TO StudentRecord END-READEND-PERFORM.

FF rr aa nn kk CC uu rr tt aa ii nn99 33 33 44 55 66 77 LL MM 00 55 11

StudentID StudentName Course.

StudentRecord

FF rr aa nn kk CC uu rr tt aa ii nn99 33 33 44 55 66 77 LL MM 00 55 11T h o m a s H e a l y9 3 8 3 7 1 5 L M 0 6 8T o n y O ‘ B r i a n9 3 4 7 2 9 2 L M 0 5 1B i l l y D o w n e s9 3 7 8 8 1 1 L M 0 2 1

EOF

How the READ works

TT hh oo mm aa ss HH ee aa ll yy 99 33 88 33 77 11 55 LL MM 00 66 88

StudentID StudentName Course.

StudentRecord

F r a n k C u r t a i n9 3 3 4 5 6 7 L M 0 5 1TT hh oo mm aa ss HH ee aa ll yy99 33 88 33 77 11 55 LL MM 00 66 88T o n y O ‘ B r i a n9 3 4 7 2 9 2 L M 0 5 1B i l l y D o w n e s9 3 7 8 8 1 1 L M 0 2 1

EOF

PERFORM UNTIL StudentRecord = HIGH-VALUES READ StudentRecords AT END MOVE HIGH-VALUES TO StudentRecord END-READEND-PERFORM.

How the READ works

TT oo nn yy OO ‘‘ BB rr ii aa nn 99 33 44 77 22 99 22 LL MM 00 55 11

StudentID StudentName Course.

StudentRecord

F r a n k C u r t a i n9 3 3 4 5 6 7 L M 0 5 1T h o m a s H e a l y9 3 8 3 7 1 5 L M 0 6 8TT oo nn yy OO ‘‘ BB rr ii aa nn99 33 44 77 22 99 22 LL MM 00 55 11B i l l y D o w n e s9 3 7 8 8 1 1 L M 0 2 1

EOF

PERFORM UNTIL StudentRecord = HIGH-VALUES READ StudentRecords AT END MOVE HIGH-VALUES TO StudentRecord END-READEND-PERFORM.

How the READ works

BB ii ll ll yy DD oo ww nn ee ss 99 33 77 88 88 11 11 LL MM 00 22 11

StudentID StudentName Course.

StudentRecord

F r a n k C u r t a i n9 3 3 4 5 6 7 L M 0 5 1T h o m a s H e a l y9 3 8 3 7 1 5 L M 0 6 8T o n y O ‘ B r i a n9 3 4 7 2 9 2 L M 0 5 1BB ii ll ll yy DD oo ww nn ee ss99 33 77 88 88 11 11 LL MM 00 22 11

EOF

PERFORM UNTIL StudentRecord = HIGH-VALUES READ StudentRecords AT END MOVE HIGH-VALUES TO StudentRecord END-READEND-PERFORM.

How the READ works

StudentID StudentName Course.

StudentRecord

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

EOF

HIGH-VALUES

PERFORM UNTIL StudentRecord = HIGH-VALUES READ StudentRecords AT END MOVE HIGH-VALUES TO StudentRecord END-READEND-PERFORM.

How the READ works

Sequential read.

SEQUENTIAL READ Program cont..

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 verb

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.

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.

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.

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.

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.

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.

01 PARM-LIST.

05 PART-NO PIC X(4).

05 USA-SALES PIC 9(5).

PROCEDURE DIVISION.

CALL “PROGA”

USING PARM-LIST.

PROGA

LINKAGE SECTION.

01 USING-LIST.

10 PART-ID PIC X(4).

10 SALES PIC 9(5).

PROCEDURE DIVISION

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.CALL "ProgramName" USING P1, P2, P3, P4.

PROCEDURE DIVISION USING P2, P4, P1, P3.PROCEDURE DIVISION USING P2, P4, P1, P3.

CALL Parameters

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

PROCEDURE DIVISION USING P2, P4, P1, P3.PROCEDURE DIVISION USING P2, P4, P1, P3.

Positions Correspond - Not NamesPositions Correspond - Not Names

Parameter Passing Mechanisms

CALL .. BYCALL .. BYREFERENCEREFERENCE

CALLedCALLedProgramProgram

Parameter Passing Mechanisms

CALL .. BYCALL .. BYREFERENCEREFERENCE

CALLedCALLedProgramProgram

Address of Address of

Data ItemData Item

DirectionDirectionof Data Flowof Data Flow

Parameter Passing Mechanisms

CALL .. BYCALL .. BYREFERENCEREFERENCE

CALLedCALLedProgramProgram

Address of Address of

Data ItemData Item

CALLCALL .. BY.. BYCONTENTCONTENT

CALLedCALLedProgramProgram

Copy ofCopy ofData ItemData Item

DirectionDirectionof Data Flowof Data Flow

Parameter Passing Mechanisms

CALL .. BYCALL .. BYREFERENCEREFERENCE

CALLedCALLedProgramProgram

Address of Address of

Data ItemData Item

CALL .. BYCALL .. BYCONTENTCONTENT

CALLedCALLedProgramProgram

Copy ofCopy ofData ItemData Item

Address of Address of CopyCopy

Data Data ItemItem

DirectionDirectionof Data Flowof Data Flow

DirectionDirectionof Data Flowof Data Flow

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 USINGBY CONTENT WS-NUM1

BY REFERENCE WS-NUM2

BY REFERENCE WS-NUM3.

Passing by reference and value

CALL

Example

SAMPLE CALL PROGRAM

Overview

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.

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.

The StudentFile is a sequential file sequenced upon ascending StudentId.

Write a program to display the number of students taking each course. How?

DATA DIVISION.DATA DIVISION.FILE SECTION.FILE SECTION.FD StudentFile.FD StudentFile.01 StudentDetails.01 StudentDetails. 02 StudentId PIC 9(7). 02 StudentId PIC 9(7). 02 StudentName. 02 StudentName. 03 Surname PIC X(8). 03 Surname PIC X(8). 03 Initials PIC XX. 03 Initials PIC XX. 02 DateOfBirth. 02 DateOfBirth. 03 YOBirth PIC 9(2). 03 YOBirth PIC 9(2). 03 MOBirth PIC 9(2). 03 MOBirth PIC 9(2). 03 DOBirth PIC 9(2). 03 DOBirth PIC 9(2). 02 CourseCode PIC X(4). 02 CourseCode PIC X(4). 02 Grant PIC 9(4). 02 Grant PIC 9(4). 02 Gender PIC X. 02 Gender PIC X.

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.

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.

Sort Example.

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.

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.

SORTSORTProcessProcess

WorkFileWorkFile

How the SORT works.

SORT WorkFile ON ASCENDING KEY WSalesmanNum USING SalesFile GIVING SortedSalesFile.

SalesFileSalesFile SortedSalesFileSortedSalesFile

UnsortedRecords

SortedRecords

INPUT FILE

OUTPUT File

SIMPLE SORT PROGRAM

SIMPLE SORT PROGRAM

RUN JCL

SIMPLE SORT OUTPUT

COMPLEX SORT

Overview

COBOL – SORTInput 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.

SORTSORTProcessProcess

How the INPUT PROCEDURE works.

SORT WorkFile ON ASCENDING KEY WSalesmanNum INPUT PROCEDURE IS SelectHatSales GIVING SortedSalesFile.

WorkFileWorkFile

SalesFileSalesFile SortedSalesFileSortedSalesFile

SortedRecords

SelectHatSalesSelectHatSales

UnsortedHat

Records

UnsortedRecords

OPEN INPUT InFileNameREAD InFileName RECORD

PERFORM UNTIL Condition RELEASE SDWorkRec READ InFileName RECORDEND-PERFORM

CLOSE InFile

OPEN INPUT InFileNameREAD InFileName RECORD

PERFORM UNTIL Condition RELEASE SDWorkRec READ InFileName RECORDEND-PERFORM

CLOSE InFile

INPUT PROCEDURE Template

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.

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.

INPUT PROCEDURE - ExampleINPUT PROCEDURE - Example

COBOL – SORTInput 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 – SORTOutput Procedure – Syntax

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

{OUTPUT PROCEDURE IS proc-3 }

{GIVING file-2 . . . }

COBOL – SORTReturn

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 – SORTOutput 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 – SORTOutput 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 – SORTTypical 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

top related