Professional Documents
Culture Documents
BUSINESS ORIENTED
LANGUAGE)
Overview
COBOL Fundamentals
DAY5
FILES
1-------1011-----------------25-28------35--38
1000000345RAJESH KRISHNAMOORTHY03308900.50030
SALESIDSNAME QTY1 LOC1 PRICE1 QTY2 LOC2 PRICE2 QTY3 LOC3 PRICE3 QTY4 LOC4 PRICE4
STUDENTS
StudId StudName DateOfBirth
9723456 COUGHLAN 10091961
9724567 RYAN 31121976
9534118 COFFEY 23061964 occurrences
9423458 O'BRIEN 03111979
9312876 SMITH 12121976
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.
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
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
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
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
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
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
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
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
REWRITE is used to update an existing record in the file which has been
read in the program
Syntax
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.
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
Value clause can not be specified for items other than level-88 items
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
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
1) By REFERENCE
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
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 )
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
Overview
COBOL –Sorting
USING File-name-2
GIVING File-name-3.
SORT SORT-FILE
ON ASCENDING KEY EMP-NO
ON ASCENDING KEY E-NAME
ON ASCENDING KEY E-LEVEL
USING INPUT-FILE
GIVING OUTPUT-FILE.
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.
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.
SalesFile SortedSalesFile
Unsorted Sorted
Records Records
SORT
Process
WorkFile
Overview
COBOL – SORT
Input Procedure – Syntax
SORT file-name-1
{ ON ASCENDING KEY data-name-1. . . }. . .
DESCENDING
{ 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
RELEASE Sort-record-name-1
[ FROM Identifier-1 ]
COBOL – SORT Release (cont’d)
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.
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.
After the records have been sorted but before they are written into
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
Overview
COBOL – Sort & Merge Summary