在cobol中自带实现对输入输出文件的排序和合并,cobol中的合并主要是将不同来源的数据整合在一起。
普通文件使用FD定义,排序的中间文件是一个临时文件,使用SD定义。
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT TEST-SORT-FILE
ASSIGN TO S-SORT.
S-SORT 是系统中所指定的一个文件名。在环境部指定文件后,便可以使用SD语句在数据部的文件节中相对应了。
DATA DIVISION.
FILE SECTION.
SD TEST-SORT-FILE.
RECOED CONTAINS 80 CHARACTERS
DATA RECIRD IS SORT-RECORD.
01 SORT-RECORD.
05 EMP-CODE PIC 9(4).
05 FILLER PIC X(4) VALUE SPACES.
05 EMP-NAME PIC X(17).
05 FILLER PIC XXX VALUE SPACES.
05 ADDR-CORD PIC 9(6).
05 FILLER PIC X(30) VALUE SPACES.
即是原始的输入文件。
....
FILE SECTION.
FD TEST-INPUT-FILE.
01 INPUT-RECORD.
05 EMP-CODE PIC 9(4).
05 FILLER PIC X(4) VALUE SPACES.
05 EMP-NAME PIC X(17).
05 FILLER PIC XXX VALUE SPACES.
05 ADDR-CORD PIC 9(6).
05 FILLER PIC X(30) VALUE SPACES.
...
PROCEDURE DIVISION.
SORT TEST-SORT-FILE
ON ASCENDING KEY EMP-CODE OF SORT-RECORD
USING TEST-INPUT-FILE /*指定排序的输入文件*/
GIVING TEST-OUTPUT-FILE.
1.打开与文件名一致的排序输入文件
2.读取该排序的输入文件的每条记录
3.将读取到的数据释放到SORT排序中
4.关闭输入文件
FILE SECTION.
FD TEST-OUTPUT-FILE.
01 OUTPUT-RECORD.
05 EMP-CODE PIC 9(4).
05 FILLER PIC X(4) VALUE SPACES.
05 EMP-NAME PIC X(17).
05 FILLER PIC XXX VALUE SPACES.
05 ADDR-CORD PIC 9(6).
05 FILLER PIC X(30) VALUE SPACES.
...
PROCEDURE DIVISION.
SORT TEST-SORT-FILE
ON ASCENDING KEY EMP-CODE OF SORT-RECORD
USING TEST-INPUT-FILE
GIVING TEST-OUTPUT-FILE./*指定排序的输出文件*/
1.打开输出文件
2.将排序中间的每条记录顺次写入
3.关闭输出文件
SORT格式
SORT sort-file-name
ON ASCENDING KEY sort-filed
USING input-file-name
GIVING output-file-name.
ON ASCENDING KEY 升序 ON DESCENDING KEY 降序
sort-filed 指定排序的关键字
DATA DIVISION.
FILE SECTION.
SD SORT-PRPC-FILE.
01 SORT-PROC-REC.
05 S-KEY PIC X(5).
05 S-DATA PIC X(75).
FD OUT-FILE.
01 OUT-FILE.
05 OUT-KEY PIC X(5).
05 OUT-DATA PIC X(75).
*
WORKING-STORAGE SECTION.
01 WS-REC.
05 WS-KEY PIC X(5).
05 WS-DATA PIC X(75).
*
PROCEDURE DIVISION.
SORT TEST-SORT-FILE
ON ASCENDING KEY S-KEY
INPUT PROCEDURE SORT-INPUT-PROC/*输出处理过程*/
GIVING TEST-OUTPUT-FILE.
STOP RUN.
*
SORT-INPUT-PROC.
PERFORM UNTIL WS-KEY = '00000'
ACCEPT WS-REC
RELEASE SORT-PROC-REC FROM WS-REC
END-PERFORM.
RELEASE SORT-PROC-REC FROM WS-REC 就是将工作储存区中的数据转到输出文件
DATA DIVISION.
FILE SECTION.
SD SORT-PRPC-FILE.
01 SORT-PROC-REC.
05 S-KEY PIC X(5).
05 S-DATA PIC X(75).
FD IN-FILE.
01 IN-FILE.
05 IN-KEY PIC X(5).
05 IN-DATA PIC X(75).
*
WORKING-STORAGE SECTION.
01 WS-REC.
05 WS-KEY PIC X(5).
05 WS-DATA PIC X(75).
01 END-OF-RECORDS PIC X VALUE 'N'.
*
PROCEDURE DIVISION.
SORT TEST-SORT-FILE
ON ASCENDING KEY S-KEY
USING IN-FILE
OUTPUT PROCEDURE IS SORT-OUTPUT-PROC./*输出处理过程*/
STOP RUN.
*
SORT-OUTPUT-PROC.
RETURN SORT-PROC-FILE
AT END
SET END-PROC-FILE TO TURE.
PERFORM UNTIL END-OF-RECORDS
DISPLAY 'SOREEN-REC :' SORT-RPOC-REC
RETURN SORT-PROC-FILE
AT END
SET END-PROC-FILE TO TURE.
END-PERFORM.
RETURN 处理之后的数据能够输出,AT END是终止标志。
MERGE merge-file-name
ON ASCENDING kEY key-name
USING in-file-one in-file-two ...
GIVING out-file
USING 指定输入文件,输入文件应为有序数据
GIVING 指定输出文件
IN-FILE-ONE
001000 WANG IDM 2001/10/10
001004 WANG1 IDM 2004/10/10
IN-FILE-TWO
001007 WANG3 IDM 2011/10/10
001008 WANG3 IDM 2014/10/10
FILE-CONTROL.
SELECT IN-FILE-ONE ASSIGN TO S-SYSIN-1.
SELECT IN-FILE-TWO ASSIGN TO S-SYSIN-2.
SELECT OUT-FILE ASSIGN TO S-SYSIN.
SELECT TEST-MERGE-FILE ASSIGN TO S-MERGE.
*
DATA DIVISION.
FILE SECTION.
SD TEST-MERGE-FILE.
RECORD CONTAINS 80 CHARACTERS
DATA RECORD IS SORT-REC.
01 SORT-REC.
05 MEM-CODE PIC 9(6).
05 FILLER PIC X(4) VALUE SPACES.
05 MEM-NAME PIC X(17).
05 FILLER PIC XXX VALUE SPACES.
05 ON-BOARD PIC X(10).
05 FILLER PIC X(20) VALUE SPACES.
FD IN-FILE-ONE.
01 IN-REC-ONE.
05 MEM-CODE PIC 9(6).
05 FILLER PIC X(4) VALUE SPACES.
05 MEM-NAME PIC X(17).
05 FILLER PIC XXX VALUE SPACES.
05 ON-BOARD PIC X(10).
05 FILLER PIC X(20) VALUE SPACES.
FD IN-FILE-TWO.
01 IN-REC-TWO.
05 MEM-CODE PIC 9(6).
05 FILLER PIC X(4) VALUE SPACES.
05 MEM-NAME PIC X(17).
05 FILLER PIC XXX VALUE SPACES.
05 ON-BOARD PIC X(10).
05 FILLER PIC X(20) VALUE SPACES.
FD OUT-FILE.
01 OUT-REC.
05 MEM-CODE PIC 9(6).
05 FILLER PIC X(4) VALUE SPACES.
05 MEM-NAME PIC X(17).
05 FILLER PIC XXX VALUE SPACES.
05 ON-BOARD PIC X(10).
05 FILLER PIC X(20) VALUE SPACES.
*
PROCEDURE DIVISION.
MERGE TEST-MERGE-FILE
ON ASCENDING KEY MEM-CODE OF SORT-REC
USING IN-FILE-ONE IN-FILE-TWO
GIVING OUT-FILE.
STOP RUN.