Loading a KSDS with WRITE
Indexed files usually start life as a sequential file: an extract, a conversion, yesterday's backup. Loading one is a read-a-record, write-a-record loop. The interesting part is what the file refuses.
WRITE and duplicate keys
WRITE adds a record using whatever is in the RECORD KEY field. If that
key already exists, the write fails with status 22 and the
INVALID KEY phrase runs:
WRITE ACCT-REC FROM NEW-REC
INVALID KEY
DISPLAY "DUPLICATE " NEW-REC(1:6)
NOT INVALID KEY
ADD 1 TO WS-LOADED
END-WRITE
Sequential access wants sorted input
With ACCESS MODE IS SEQUENTIAL and OPEN OUTPUT, records must arrive in
ascending key order. That is the fastest way to load a big file, but an
out-of-order key is rejected with status 21:
IDENTIFICATION DIVISION.
PROGRAM-ID. SEQLOAD.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PART-FILE ASSIGN TO PARTMAST
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL
RECORD KEY IS PART-NO
FILE STATUS IS WS-FS.
DATA DIVISION.
FILE SECTION.
FD PART-FILE.
01 PART-REC.
05 PART-NO PIC X(4).
05 PART-DESC PIC X(12).
WORKING-STORAGE SECTION.
01 WS-FS PIC XX.
PROCEDURE DIVISION.
OPEN OUTPUT PART-FILE
MOVE "B200WASHER" TO PART-REC
WRITE PART-REC
DISPLAY "WROTE B200, STATUS " WS-FS
MOVE "A100BOLT M8" TO PART-REC
WRITE PART-REC
INVALID KEY DISPLAY "A100 REJECTED, STATUS " WS-FS
END-WRITE
CLOSE PART-FILE
STOP RUN.
WROTE B200, STATUS 00
A100 REJECTED, STATUS 21
If your input is not sorted, either sort it first (later in this module)
or use ACCESS MODE IS RANDOM or DYNAMIC, where each WRITE goes
straight to its place by key.
Reading it back in key order
With ACCESS MODE IS DYNAMIC, the same file can be written by key and then
read sequentially. READ file NEXT RECORD returns records in key order,
whatever order they were written in, and hits AT END (status 10) after
the last one:
OPEN INPUT ACCT-FILE
PERFORM UNTIL END-OF-FILE
READ ACCT-FILE NEXT RECORD
AT END SET END-OF-FILE TO TRUE
NOT AT END DISPLAY ACCT-ID " " ACCT-NAME
END-READ
END-PERFORM
Right after OPEN, the file is positioned at the first key, so no START is
needed to read from the beginning.
OPEN modes for indexed files
| OPEN | Use |
|---|---|
OUTPUT |
Create or replace the file, then WRITE |
INPUT |
READ only |
I-O |
READ, WRITE, REWRITE, DELETE on an existing file |
EXTEND |
Append in ascending key order (sequential access) |
On the job
Production KSDS loads are often done without COBOL at all: an IDCAMS
REPRO step copies a sorted sequential file into an empty cluster. You
still write COBOL loaders when records need validation or reformatting
on the way in, and the duplicate-key report is usually the first thing
the business asks to see.
Your task
New accounts arrive in a sequential file in no particular order. Load them into a fresh account master (KSDS), reject duplicates, then prove the load by listing the master.
DD ACCTIN — line sequential, 35-byte records, unsorted:
ACCT-ID X(6), ACCT-NAME X(20), ACCT-BAL 9(7)V99.
DD ACCTMAST — indexed, same layout, record key ACCT-ID. Create it
with OPEN OUTPUT.
- For each input record,
WRITEit to the master. If the key already exists, displayDUPLICATE nnnnnn REJECTED (STATUS 22)using the real file status, and count it as rejected; otherwise count it as loaded. - After the load, display
LOADED: nnn REJECTED: nnn(PIC 9(3)). - Reopen the master for input and display every record in key order as
ACCT-ID, a space,ACCT-NAME.
The starter uses ACCESS MODE IS SEQUENTIAL. Run it first and see what
happens with unsorted input, then choose a better access mode.