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
S102RAMESH
0040 PA
4000
0060
WI
6000
S103RAGAVAN
0080 MI
8000
0050
NY
5000
60
CA
6000
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.
DATA DIVISION.
(e.g. StudentName, DateOfBirth, CourseCode).
FILE SECTION. FD STUDENT-FILE. 01 STUDENT-REC. 05 05 05 05 05
STUD-NAME DOB COURSE-CODE COURSE-NM COURSE-TYPE
We use the term FIELD to describe an item of information we are recording about an object
PIC PIC PIC PIC PIC
X(25). X(10). X(15). X(25). X(5).
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
Files, Records, Fields. STUDENTS StudId StudName DateOfBirth 9723456 9724567 9534118 9423458 9312876
COUGHLAN RYAN COFFEY O'BRIEN SMITH
DATA DIVISION. FILE SECTION. FD StudentFile. 01 StudentDetails. 02 StudId 02 StudName 02 DateOfBirth
10091961 31121976 23061964 03111979 12121976
occurrences
Record Type (Template) (Structure) PIC 9(7). PIC X(8). PIC X(8).
How files are processed.
Read a record Based on Structure
Rec1 Rec2 Rec3 Rec4
Read OPEN
Record
Process
Store in
FILE
(One at
Record
Output File
A Time )
Record Buffers Program
DISK STUDENTS
Record Instance
IDENTIFICATION DIVISION. etc. ENVIRONMENT DIVISION. etc. DATA DIVISION. FILE SECTION. RecordBuffer Declaration
Creating a Student Record Student Details. 01 StudentDetails.
Student Id.
02
StudentId
Student Name.
02
StudentName.
PIC 9(7).
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 02 StudentName. 03 Surname 03 Initials 02 DateOfBirth. 03 YOBirth 03 MOBirth 03 DOBirth 02 CourseCode 02 Grant 02 Gender
PIC 9(7). PIC X(8). PIC XX. PIC PIC PIC PIC PIC PIC
9(2). 9(2). 9(2). X(4). 9(4). 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.
Physical File
STUDENTS
DATA DIVISION. FILE SECTION. FD StudentFile. 01 StudentDetails. 02 StudentId 02 StudentName. 03 Surname 03 Initials 02 DateOfBirth. 03 YOBirth 03 MOBirth 03 DOBirth 02 CourseCode 02 Grant 02 Gender
PIC 9(7). PIC X(8). PIC XX. PIC PIC PIC PIC PIC PIC
9(2). 9(2). 9(2). X(4). 9(4). 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 SEQUENTIAL]. [ORGANIZATION IS 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 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.
Only used for
INPUT Moden
READing Records In A File
Only used for WRITing
output Mode
Records
In A File
Only used for APPENDing
Extend Mode
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.
How the WRITE works OPEN OUTPUT StudentFile. MOVE "9334567Frank Curtain WRITE StudentDetails. MOVE "9383715Thomas Healy WRITE StudentDetails. CLOSE StudentFile. STOP RUN.
LM051" TO StudentDetails. LM068" TO StudentDetails.
StudentRecord StudentID
StudentName
9 3 3 4 5 6 7 F r a n k
C u r t a i n
Students.Dat
9 3 3 4 5 6 7 F r a n k
EO F
C u r t a i n
Course. L M 0 5 1
L M 0 5 1
How the WRITE works OPEN OUTPUT StudentFile. MOVE "9334567Frank Curtain WRITE StudentDetails. MOVE "9383715Thomas Healy WRITE StudentDetails. CLOSE StudentFile. STOP RUN.
LM051" TO StudentDetails. LM068" TO StudentDetails.
StudentRecord StudentID
StudentName
9 3 8 3 7 1 5 T h o ma s
H e a l y
Students.Dat
9 3 3 4 5 6 7 F r a n k C u r t a i n 9 3 8 3 7 1 5 T h o ma s H e a l y
EOF
Course. L M 0 6 8
L M 0 5 1 L M 0 6 8
IDENTIFICATION DIVISION. PROGRAM-ID. SeqWrite. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT StudentFile ASSIGN TO DDNAME ORGANIZATION IS LINE SEQUENTIAL.
Assign File To DDNAME (Physical Sequential File)
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 Write a Record in a File 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.
Getting values for Records to be written In File
Sequential write
Write cont..
IDENTIFICATION DIVISION. PROGRAM-ID. SeqRead. AUTHOR. Michael Coughlan.
Assign File To DDNAME
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 02 StudentName. 03 Surname 03 Initials 02 CourseCode 02 Gender
(Physical Sequential File)
Declaring File Structure PIC 9(7). PIC PIC PIC PIC
X(8). XX. X(4). X.
Opened the File For Reading Recs
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.
Read a Record in a File till END OF File is reached
Display values at SPOOL
Close a File which Is Opened
How the READ works StudentRecord StudentID
9 3 3 4 5 6 7 F r a n k
9 9 9 9
3 3 3 3
3 8 4 7
4 3 7 8
5 7 2 8
Course.
StudentName
6 1 9 1
7 5 2 1
F T T B
r h o i
a o n l
C u r t a i n
n k C u ma s H y O ‘ B l y D o
r e r w
t a i n
a l a e
i n y n s
L M 0 5 1
L L L L
EOF PERFORM UNTIL StudentRecord = HIGH-VALUES READ StudentRecords AT END MOVE HIGH-VALUES TO StudentRecord END-READ END-PERFORM.
M M M M
0 0 0 0
5 6 5 2
1 8 1 1
How the READ works StudentRecord StudentID
9 3 8 3 7 1 5 T h o ma s
9 9 9 9
3 3 3 3
3 8 4 7
4 3 7 8
5 7 2 8
Course.
StudentName
6 1 9 1
7 5 2 1
F T T B
r h o i
a o n l
H e a l y
n k C u ma s H y O ‘ B l y D o
r e r w
t a i n
a l a e
i n y n s
L M 0 6 8
L L L L
EOF PERFORM UNTIL StudentRecord = HIGH-VALUES READ StudentRecords AT END MOVE HIGH-VALUES TO StudentRecord END-READ END-PERFORM.
M M M M
0 0 0 0
5 6 5 2
1 8 1 1
How the READ works StudentRecord StudentID
9 3 4 7 2 9 2 T o n y
9 9 9 9
3 3 3 3
3 8 4 7
4 3 7 8
5 7 2 8
Course.
StudentName
6 1 9 1
7 5 2 1
F T T B
r h o i
a o n l
O ‘ B r i a n
n k C u ma s H y O ‘ B l y D o
r e r w
t a i n
a l a e
i n y n s
L M 0 5 1
L L L L
EOF PERFORM UNTIL StudentRecord = HIGH-VALUES READ StudentRecords AT END MOVE HIGH-VALUES TO StudentRecord END-READ END-PERFORM.
M M M M
0 0 0 0
5 6 5 2
1 8 1 1
How the READ works StudentRecord StudentID
9 3 7 8 8 1 1 B i l l y
9 9 9 9
3 3 3 3
3 8 4 7
4 3 7 8
5 7 2 8
Course.
StudentName
6 1 9 1
7 5 2 1
F T T B
r h o i
a o n l
D o w n e s
n k C u ma s H y O ‘ B l y D o
r e r w
t a i n
a l a e
i n y n s
L M 0 2 1
L L L L
EOF PERFORM UNTIL StudentRecord = HIGH-VALUES READ StudentRecords AT END MOVE HIGH-VALUES TO StudentRecord END-READ END-PERFORM.
M M M M
0 0 0 0
5 6 5 2
1 8 1 1
How the READ works StudentRecord StudentID
Course.
StudentName
HIGH-VALUES 9 9 9 9
3 3 3 3
3 8 4 7
4 3 7 8
5 7 2 8
6 1 9 1
7 5 2 1
F T T B
r h o i
a o n l
n k C u ma s H y O ‘ B l y D o
r e r w
t a i n
a l a e
i n y n s
L L L L
EOF PERFORM UNTIL StudentRecord = HIGH-VALUES READ StudentRecords AT END MOVE HIGH-VALUES TO StudentRecord END-READ END-PERFORM.
M M M M
0 0 0 0
5 6 5 2
1 8 1 1
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 subprogram 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). …
PROGA LINKAGE SECTION. 01 USING-LIST. 10 PART-ID PIC X(4).
PROCEDURE DIVISION.
10 SALES PIC 9(5).
… CALL “PROGA” USING PARM-LIST. …
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.
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 REFERENCE
CALLed Program
Parameter Passing Mechanisms Address of Data Item
CALL .. BY REFERENCE
Direction of Data Flow
CALLed Program
Parameter Passing Mechanisms Address of Data Item
CALL .. BY REFERENCE
Direction of Data Flow
CALL .. BY CONTENT
CALLed Program
CALLed Program
Copy of Data Item
Parameter Passing Mechanisms Address of Data Item
CALL .. BY REFERENCE
Direction of Data Flow
Direction of Data Flow
CALL .. BY CONTENT Data Item
Copy of Data Item
CALLed Program
CALLed Program Address of Copy
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
We can use the variables passed by the calling program, in the procedure division of the called program. The name need not be the same.
Define variables passed by the calling program in the linkage section
Procedure division statement modified to include the passed variables from the calling program in the same sequence as given in the 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 02 StudentName. 03 Surname 03 Initials 02 DateOfBirth. 03 YOBirth 03 MOBirth 03 DOBirth 02 CourseCode 02 Grant 02 Gender
PIC 9(7). PIC X(8). PIC XX. PIC PIC PIC PIC PIC PIC
9(2). 9(2). 9(2). X(4). 9(4). 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 01 SD 01
FD 01
SalesFile. SalesRec. 02 FILLER
PIC X(10).
WorkFile. WorkRec. 02 WSalesmanNum 02 FILLER
PIC 9(5). PIC X(5).
SortedSalesFile. 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 01
WorkFile. WorkRecord. 02 ProvinceCode 02 SalesmanCode 02 FILLER
PIC 9. PIC 9(5). 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 Records
SORT Process
Sorted Records
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 DESCENDING
KEY data-name-1. . . }. . .
{ 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
Unsorted Records
Unsorted Hat Records
SortedSalesFile
SORT Process
Sorted Records
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 01
SalesFile. 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 [ NOT AT END ] [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 DESCENDING
USING file-2 { file-3 } . . . OUTPUT PROCEDURE IS proc-1 GIVING {file-4}.
KEY data -1}
COBOL – Merge Typical Program FILE CONTROL. SELECT IN-FILE1
ASSIGN
TO E-FILE1.
SELECT IN-FILE2
ASSIGN
TO E-FILE2.
SELECT M-FILE SELECT OUT-FILE
ASSIGN ASSIGN
TO WORK. TO E-FILE.
DATA DIVISION. FD
IN-FILE1.
01
IN-REC1
FD
IN-FILE2.
01
IN-REC2
SD
M-FILE.
01
PIC
X(100).
PIC
X(100).
PIC
X(100).
M-REC. 05 KEY-FIELD
PIC
05 REST-OF REC PIC FD
OUT-FILE.
01
OUT-REC
X(5). 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