This is a serial code samples of how COBOL access dataset
Write an Index Dataset
IDENTIFICATION DIVISION. PROGRAM-ID. WRITEINDEXV14. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FILE-V14 ASSIGN TO IDXFILE ORGANIZATION IS INDEXED ACCESS IS DYNAMIC RECORD KEY IS S-ID FILE STATUS IS IO-STATUS. DATA DIVISION. FILE SECTION. FD FILE-V14 LABEL RECORD STANDARD RECORDING MODE IS V RECORD IS VARYING IN SIZE FROM 2 TO 14 DEPENDING ON FILE-REC-LEN DATA RECORD FILE-REC. 01 FILE-REC. 02 DATAV14. 04 S-ID PIC X(02). 04 S-NAME PIC X(04). 04 S-VALUE PIC X(08). WORKING-STORAGE SECTION. 01 FILE-REC-LEN PIC X(4) COMP-X. 01 IO-STATUS PIC XX. 01 NB-RECS PIC 9(9) VALUE 0. PROCEDURE DIVISION. P-START. OPEN OUTPUT FILE-V14 IF IO-STATUS NOT = "00" DISPLAY "FILELD-0202: OUTPUT FAILED" DISPLAY "ASSIGN: SORTIE" DISPLAY "IO-STATUS =" IO-STATUS GO TO FIN-ERREUR END-IF. WRITE-1. MOVE SPACES TO FILE-REC. MOVE "11" TO S-ID. MOVE "AAAA" TO S-NAME. MOVE "aaaaaaaa" TO S-VALUE. MOVE 14 TO FILE-REC-LEN. WRITE FILE-REC INVALID KEY DISPLAY "ERROR:" Error DISPLAY "FILELD-0203: WRITE FAILED" Error DISPLAY "ASSIGN: SORTIE" DISPLAY "IO-STATUS =" IO-STATUS GO TO FIN-ERREUR END-WRITE. ADD 1 TO NB-RECS. WRITE-2. MOVE SPACES TO FILE-REC. MOVE "22" TO S-ID. MOVE "BBBB" TO S-NAME. MOVE "bbbbbbbb" TO S-VALUE. MOVE 14 TO FILE-REC-LEN. WRITE FILE-REC INVALID KEY DISPLAY "ERROR:" Error DISPLAY "FILELD-0203: WRITE FAILED" Error DISPLAY "ASSIGN: SORTIE" DISPLAY "IO-STATUS =" IO-STATUS GO TO FIN-ERREUR END-WRITE. ADD 1 TO NB-RECS. WRITE-3. MOVE SPACES TO FILE-REC. MOVE "33" TO S-ID. MOVE "CCCC" TO S-NAME. MOVE "cccccccc" TO S-VALUE. MOVE 14 TO FILE-REC-LEN. WRITE FILE-REC INVALID KEY DISPLAY "ERROR:" Error DISPLAY "FILELD-0203: WRITE FAILED" Error DISPLAY "ASSIGN: SORTIE" DISPLAY "IO-STATUS =" IO-STATUS GO TO FIN-ERREUR END-WRITE. ADD 1 TO NB-RECS. FIN-REL. DISPLAY "WRITE DONE, ITEM: " NB-RECS. PERFORM END-COMMON-DISPLAY. EXIT PROGRAM. STOP RUN. FIN-ERREUR. DISPLAY "WRITE FAILED". PERFORM END-COMMON-DISPLAY. EXIT PROGRAM. STOP RUN RETURNING 1. END-COMMON-DISPLAY. CLOSE FILE-V14.
Read an Index Dataset
IDENTIFICATION DIVISION. PROGRAM-ID. READINDEXV14. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INDEX-FILE ASSIGN TO IDXFILE ORGANIZATION IS INDEXED * ACCESS MODE IS RANDOM ACCESS MODE IS DYNAMIC RECORD KEY IS S-ID FILE STATUS IS IO-STATUS. DATA DIVISION. FILE SECTION. FD INDEX-FILE LABEL RECORD STANDARD RECORDING MODE IS V RECORD is VARYING in SIZE from 2 to 14 DEPENDING ON REC-LEN DATA RECORD DATAV14-REC. 01 DATAV14-REC. 03 S-ID PIC X(02). 03 S-NAME PIC X(04). 03 S-VALUE PIC X(08). WORKING-STORAGE SECTION. 01 REC-LEN PIC 9(4) COMP. 01 IO-STATUS PIC XX. PROCEDURE DIVISION. OPEN INPUT INDEX-FILE. IF IO-STATUS NOT = "00" DISPLAY "OPEN INPUT FAILED" DISPLAY "IO-STATUS =" IO-STATUS GO TO FIN-REL END-IF. DISPLAY "------------READ SEQUENTIAL------------". READ-SEQUENTIAL. MOVE SPACES TO DATAV14-REC. READ INDEX-FILE NEXT AT END GO TO READ-KEY END-READ. PERFORM DISPLAY-RECORD THRU E-DISPLAY-RECORD. GO TO READ-SEQUENTIAL. READ-KEY. MOVE "22" TO S-ID DISPLAY "------------READ KEY(" S-ID ")------------". READ INDEX-FILE IF IO-STATUS NOT = "00" DISPLAY "READ FAILED" DISPLAY "IO-STATUS =" IO-STATUS GO TO FIN-REL END-IF. PERFORM DISPLAY-RECORD THRU E-DISPLAY-RECORD. FIN-REL. * DISPLAY "Done". CLOSE INDEX-FILE. EXIT PROGRAM. STOP RUN. DISPLAY-RECORD. DISPLAY "RECORD" ": S-ID=" S-ID ", S-NAME=" S-NAME ", S-VALUE=" S-VALUE. E-DISPLAY-RECORD. EXIT.
Execute Result:
$ cobrun READIDXV14 ------------READ SEQUENTIAL------------ RECORD: S-ID=11, S-NAME=AAAA, S-VALUE=aaaaaaaa RECORD: S-ID=22, S-NAME=BBBB, S-VALUE=bbbbbbbb RECORD: S-ID=33, S-NAME=CCCC, S-VALUE=cccccccc ------------READ KEY(22)------------ RECORD: S-ID=22, S-NAME=BBBB, S-VALUE=bbbbbbbb