Вы находитесь на странице: 1из 46

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 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 numeric or non numeric


Simplified Sort Syntax.
  A S C E N D I N G  
S O R T W o r k F ile N a m e  O N   K E Y S o r tK e y I d e n tifie r    
  D E S C E N D I N G  
U S I N G  I n F ile N a m e  
G I V I N G O u tF ile N a m e  

 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 salesmanno pic x(5)
02 FILLER PIC X(5).
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.
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
How the INPUT PROCEDURE
works.

SalesFile SortedSalesFile

Unsorted Sorted
Records Records
Unsorted
Hat SORT
Records Process

SelectHatSale
s
WorkFile

SORT WorkFile ON ASCENDING KEY WSalesmanNum


INPUT PROCEDURE IS SelectHatSales
GIVING SortedSalesFile.
INPUT PROCEDURE
Template

OPEN
OPENINPUT
INPUTInFileName
InFileName
READ
READInFileName
InFileNameRECORD
RECORD
PERFORM
PERFORMUNTIL
UNTILCondition
Condition
RELEASE
RELEASESDWorkRec
SDWorkRec
READ
READ InFileNameRECORD
InFileName RECORD
END-PERFORM
END-PERFORM
CLOSE
CLOSEInFile
InFile
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 ]
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 – 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
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

Вам также может понравиться