 |
» |
|
|
|
|  |  |
The following three examples illustrate KSAM file access from a COBOL program. The file
accessed in each example is called KSAMFILE. It was created previously by the KSAMUTIL
>BUILD command with BYTE type keys: the primary key containing the name of a person and
the alternate key containing his telephone number; the remaining data in each record is his
address. EXAMP1. SEQUENTIAL WRITE |  |
The first example reads data from an input file into working storage and then writes it to a KSAM
file. Access mode is sequential so that as each record is written, the keys are linked in sequential
order although the records are not physically written in sequence. Input-output type is output
only, the only type allowed for the procedure CKWRITE. The following procedures are illustrated: Figure 3-3 Sequential Write Using COBOL
Input to EXAMP1:
NOLAN JACK 923-4975 967 REED AVE. SUNNYVALE CA. 94087
HOSODA JOE 227-8214 1180 SAINT PETER CT. LOS ALTOS CA. 94022
ECKSTEIN LEO 287-5137 5303 STEEVENS CREEK SANTA CLARA CA. 95050
CARDIN RICK 578-7018 11100 WOLFE ROAD CUPERTINO CA. 94053
PASBY LINDA 295-1187 TOWN & CNTRY VILLAGE SAN JOSE CA. 94012
SEELY HENRY 293-4220 1144 LEBERTY ST. EL CERRITO CA. 94053
ROBERT GERRY 258-5535 12345 TELEGRAPH AVE . BERKELEY CA. 90871
TURNEWR IVAN 984-8498 22905 EMERSON ST. OAKLAND CA. 98234
WHITE GORDON 398-0301 4350 ASHBY AVE. BERKELEY CA. 91234
WESTER ELDER 287-4598 1256 KINGFISHER ST. SUNNYVALE CA. 43098
**END OF INPUT FOR EXAMP1**
Program EXAMP1
001000 IDENTIFICATION DIVISION.
001100 PROGRAM-ID. EXAMP1.
001200 ENVIRONMENT DIVISION.
001300 INPUT-OUTPUT SECTIONS
001400 FILE-CONTROL.
001500 SELECT SEQ-DATA ASSIGN TO "SEQDATA".
001600 DATA DIVISION.
001700 FILE SECTION.
001800 FD SEQ-DATA
001900 LABEL RECORDS ARE STANDARD.
002000 01 INPUT-REC.
002100 05 REAL-DATA PIC X(72).
002200 WORKING-STORAGE SECTION.
002300 77 RECSIZE PIC S9(4) COMP VALUE 74.
002400 77 RESULT PIC 9(4) VALUE ZERO.
002500 01 DATA-REC.
002600 05 FILLER PIC XX VALUE SPACES.
002700 05 REAL-DATA PIC X(72). l
002800 01 FILETABLE.
002900 02 FILENUMBER PIC S9(4) COMP VALUE 0.
003000 02 FILENAME PIC X(8) VALUE "GKSAMFIL".
003100 02 I-O-TYPE PIC S9(4) COMP VALUE 1.
003200 02 A-MODE PIC S9(4) COMP VALUE 0.
003300 02 PREV-OP PIC S9(4) COMP VALUE 0.
003400 01 STATUSKEY.
003500 02 STATUS-KEY-1 PIC X.
003600 02 STATUS.KEY-2 PIC X.
003700
003800 PROCEDURE DIVISION.
003900 START.
004000 OPEN INPUT SEQ-DATA@
004100 CALL "CKOPEN" USING FILETABLE, STATUSKEY.
004200 IF STATUS-KEY-1="9" THEN
004300 CALL "CKERROR" USING STATUSKEY, RESULT
004400 DISPLAY "CKOPEN ERROR NO.", RESULT.
004500 IF STATUS-KEY-1 NOT = "0" THEN
004600 DISPLAY "CKOPEN FAILED"
004700 STOP RUN.
004800 LOOP.
004900 READ SEQ-DATA
005000 AT END GO TO FINISH.
005100 MOVE CORP INPUT-REC TO DATA-REC.
005200 CALL "CKWRITE" USING FILETABLE, STATUSKEY, DATA-REC,
005300 RECSIZE.
005400 IF STATUSKEY = "02" THEN
005500 DISPLAY "DUPLICATE KEY".
005600 IF STATUS-KEY-1 = "0" THEN
005700 DISPLAY DATA-REC
005800 GO TO LOOP.
005900 IF STATUS-KEY-1 = "9" THEN
006000 CALL "CKERROR" USING STATUSKEY, RESULT
006100 DISPLAY "CKWRITE ERROR NO.", RESULT
006200 DISPLAY DATA-REC
006300 GO TO LOOP.
006400 FINISH.
006500 CLOSE SEQ-DATA.
006600 CALL "CKCLOSE" USING FILETABLE, STATUSKEY.
006700 IF STATUS-KEY-1 = "9" THEN
006800 CALL "CKERROR" USING STATUSKEY, RESULT
006900 DISPLAY "CKCLOSE ERROR NO. ". RESULT.
007000 STOP RUN.
Output from EXAMP1 Execution:
NOLAN JACK 923-4975 967 REED AVE. SUNNYVALE CA. 94087
HOSODA JOE 227-8214 1180 SAINT PETER CT. LOS ALTOS CA. 94022
ECKSTEIN LEO 287-5137 5303 STEEVENS CREEK SANTA CLARA CA. 95050
CARDIN RICK 578-7018 11100 WOLFE ROAD CUPERTINO CA. 94053
PASBY LINDA 295-1187 TOWN & CNTRY VILLAGE SAN JOSE CA. 94012
SEELY HENRY 293-4220 1144 LEBERTY ST. EL CERRITO CA. 94053
ROBERT GERRY 258-5535 12345 TELEGRAPH AVE . BERKELEY CA. 90871
TURNEWR IVAN 984-8498 22905 EMERSON ST. OAKLAND CA. 98234
WHITE GORDON 398-0301 4350 ASHBY AVE. BERKELEY CA. 91234
WESTER ELDER 287-4598 1256 KINGFISHER ST. SUNNYVALE CA. 43098
END OF PROGRAM
|
EXAMP2. SEQUENTIAL READ |  |
The second example reads the file KSAMFILE in sequential order by primary key (NAME) and
prints each record as it is read. It then repositions the file to the first sequential record according
to the alternate key (PHONE) and prints each of the records as it is read in this order. The file is
opened in sequential mode for input only. The following procedures are illustrated:
CKOPEN
CKREAD
CKSTART
CKCLOSE
|
Figure 3-4 Sequential Read Using COBOL
Program EXAM2:
001000 IDENTIFICATION DIVISION.
001100 PROGRAM-ID. EXAMP2.
001200 ENVIRONMENT DIVISION.
001300 INPUT-OUTPUT SECTION.
001400 FILE-CONTROL.
001500 SELECT SEQ-DATA ASSIGN TO "SEQDATA".
001600 DATA DIVISION.
001700 WORKING-STORAGE SECTION.
001800 77 RECSIZE PIC S9(4) COMP VALUE 74.
001900 77 RESULT PIC 9(4) VALUE ZERO.
002000 77 KEY-LOC PIC S9(4) COMP VALUE 23.
002100 77 RELOP PIC S9(4) COMP VALUE 2.
002200 77 KEYLENGTH PIC S9(4) COMP VALUE 8.
002300 77 KEY-VALUE PIC X(8) VALUE "000-0000".
002400 01 DATA-REC.
002500 05 FILLER PIC XX.
002600 05 NAME PIC X(20).
002700 05 PHONE PIC X(8).
002800 05 OTHER-DATA PIC X(44).
002900 01 FILETABLE.
003000 02 FILENUMBER PIC S9(4) COMP VALUE o.
003100 02 FILENAME PIC X(8) VALUE "GKSAMFIL".
003200 02 I-O-TYPE PIC S9(4) COMP VALUE o.
003300 02 A-MODE PIC S9(4) COMP VALUE o.
003400 02 PREV-OP PIC S9(4) COMP VALUE o.
003500 01 STATUSKEY.
003600 02 STATUS-KEY-l PIC X.
003700 02 STATUS-KEY-2 PIC X.
003800
003900 PROCEDURE DIVISION.
004000 START.
004100 CALL "CKOPEN" USING FILETABLE, STATUSKEY.
004200 IF STATUS-KEY-1 = "9" THEN
004300 CALL "CKERROR" USING STATUSKEY, RESULT
004400 DISPLAY "CKOPEN ERROR NO.", RESULT.
004500 IF STATUS-KEY-1 NOT = "0" THEN
004600 DISPLAY "CKOPEN FAILED"
004700 STOP RUN.
004800 DISPLAY "ALPHABETICAL ORDER"
004900 DISPLAY " ".
005000 L00P1.
005100 CALL "CKREAD" USING FILETABLE, STATUSKEY, DATA-REC,
005200 RESIZED.
005300 IF STATUS-KEY-1= "1" THEN GO TO PART2.
005400 IF STATUS-KEY-1 = "0" THEN
005500 DISPLAY DATA-REC
005600 ELSE
005700 DISPLAY "CKREAD ERROR, STATUS = ", STATUSKEY
005800 IF STATUS-KEY-1 = "9" THEN
005900 CALL "CKERROR" USING STATUSKEY, RESULT
006000 DISPLAY "ERROR NO.", RESULT.
006100 GO TO LOOP.
006200 PART2
006300 DISPLAY " ".
006400 DISPLAY "PHONE NO. ORDER:"
006500 DISPLAY " ".
006600 CALL "CKSTART" USING FILETABLE, STATUSKEY, RELOP,
006700 KEY-VALUE, KEY-LOC, KEYLENGTH.
006800 IF STATUSKEY = "23" THEN GO TO FINISH.
006900 IF STATUS-KEY-1 = "0" THEN GO TO LOOP2.
007000 DISPLAY "CKSTART ERROR, STATUS = ", STATUSKEY.
007100 IF STATUS-KEY-1 = "9" THEN
007200 CALL "CKERROR" USING STATUSKEY, RESULT
007300 DISPLAY "ERROR NO.", RESULT.
007400 GO TO FINISH.
007500 LOOP2
007600 CALL "CKREAD" USING FILETABLE, STATUSKEY, DATA-REC,
007700 RECSIZE.
007800 IF STATUS-KEY-1 = "1" THEN GO TO FINISH.
007900 IF STATUS-KEY-1 = "0" THEN
008000 DISPLAY DATA-REC
008100 ELSE
008200 DISPLAY "CKREAD ERROR, STATUS =", STATUSKEY
008400 IF STATUS-KEY-1 ="9" THEN
008400 CALL "CKERROR" USING STATUSKEY, RESULT
008500 DISPLAY "ERROR NO. ", RESULT.
008600 GO TO LOOP2.
008700 FINISH.
008800 CALL "CKCLOSE" USING FILETABLE, STATUSKEY.
008900 IF STATUS-KEY-1 = "9" THEN
009000 CALL "CKERROR" USING STATUSKEY, RESULT
009100 DISPLAY "CKCLOSE ERROR NO.", RESULT.
009200 STOP RUN.
Output from EXAMP2 Execution:
ALPHABETICAL ORDER:
CARDIN RICK 587-7018 11100 WOLFE ROAD CUPERTINO CA. 94053
ECKSTEIN LEO 287-5137 5303 STEVENS CREEK SANTA CLARA CA. 95050
HOS0DA JOE 227-8214 1180 SAINT PETER CT. LOS ALTOS CA. 94022
NOLAN JACK 923-4975 967 REED AVE. SUNNYVALE CA. 94087
PASBY LINDA 295-1187 TOWN & CNTRY VILLAGE SAN JOSE CA. 94102
ROBERT GERRY 259-5535 12345 TELEGRAPH AVE. BERKELEY CA. 90871
SEELY HENRY 293-4220 1144 LEBERTY ST. EL CERRITO CA. 94053
TURNEWR IVAN 984-8498 22905 EMERSON ST. OAKLAND CA. 98234
WESTER ELDER 287-4598 1256 KINGFISHER ST. SUNNYVALE CA. 43098
WHITE GORDON 398-0301 4350 ASHBY AVE. BERKELEY CA. 91234
PHONE NO. ORDER:
HOSODA JOE 227-8214 1180 SAINT PETER CT. LOS ALTOS CA. 94022
ROBERT GERRY 259-5535 12345 TELEGRAPH AVE. BERKELEY CA. 90871
WESTER ELDER 287-4598 1256 KINGFISHER ST. SUNNYVALE CA. 43098
ECKSTEIN LEO 287-5137 5303 STEVENS CREEK SANTA CLARA CA. 95050
SEELY HENRY 293-4220 1144 LEBERTY ST. EL CERRITO CA. 94053
PASBY LINDA 295-1187 TOWN & CNTRY VILLAGE SAN JOSE CA. 94102
WHITE GORDON 398-0301 4350 ASHBY AVE. BERKELEY CA. 91234
CARDIN RICK 578-7018 11100 WOLFE ROAD CUPERTINO CA. 94053
NOLAN JACK 923-4975 967 REED AVE. SUNNYVALE CA. 94087
TURNEWR IVAN 984-8498 22905 EMERSON ST. OAKLAND CA. 98234
END OF PROGRAM
|
EXAMP3. RANDOM UPDATE |  |
This example reads a set of new data containing update information into the WORKING-STORAGE
SECTION. Each record read is followed by a U for update, a D for delete, or an A for add. Records
to be added are written to the file KSAMFILE using CKWRITE in random mode. Records to be
updated are copied to the appropriate record with CKREWRITE. Records to be deleted are first
read in the WORKING-STORAGE SECTION with CKREADBYKEY and then deleted with
CKDELETE. The file is opened in random mode for input-output. The procedures illustrated by this example are:
CKOPEN
CKREADBYKEY
CKDELETE
CKREWRITE
CKWRITE
CKCLOSE
|
Figure 3-5 Random Update with COBOL
Program EXAMP3:
001000 IDENTIFICATION DIVISION,
001100 PROGRAM-ID. EXAMP3.
001200 ENVIRONMENT DIVISION.
001300 INPUT-OUTPUT SECTION.
001400 FILE-CONTROL.
001500 SELECT NEW-DATA ASSIGN TO "NEWDATA".
001600 DATA DIVISION.
001700 FILE SECTION.
001800 FD NEW-DATA
001900 LABEL RECORDS ARE STANDARD.
002000 01 INPUT-REC PIC X(73),
002100 WORKING-STORAGE SECTION,
002200 77 RECSIZE PIC S9(4) COMP VALUE 74.
002300 77 RESULT PIC 9(4) VALUE ZERO.
002400 77 KEY-LOC PIC S9(4) COMP VALUE 3.
002500 01 MASTER-REC.
002600 05 FILLER PIC XX.
002700 05 NAME PIC X(20).
002800 05 PHONE PIC X(8).
002900 05 OTHER-DATA PIC X(44).
003000 01 DATA-REC.
003100 05 NAME PIC X(20).
003200 05 PHONE PIC X (8).
003300 05 OTHER-DATA PIC X(44).
003400 05 TRANSACTION-CODE PIC X.
003500 01 FILETABLE.
003600 02 FILENUMRER PIC S9(4) COMP VALUE o.
003700 O2 FILENAME PIC X(8) VALUE "GKSAMFIL".
003800 02 I-O-TYPE PIC S9(4) COMP VALUE 2.
003900 02 A-MoDE PIC S9(4) COMP VALUE 1.
004000 02 PHEV-OP PIC S9(4) COMP VALUE 0.
004100 01 STATUSKEY.
004200 02 STATUS-KEY-1 PIC X.
004300 02 STATUS-KEY-2 PIC X.
004400
004500 PROCEDURE DIVISION.
004600 START.
004700 OPEN INPUT NEW-DATA.
004800 CALL "CKOPEN" USING FILETABLE, STATUSKEY.
004900 IF STATUS-KEY-1 = "9" THEN
005000 CALL "CKERROR" USING STATUSKEY, RESULT
005100 DISPLAY "CKOPEN ERROR NO.", RESULT.
005200 IF STATUS-KEY-1 NOT ="0" THEN
005300 DISPLAY "CKOPEN FAILED"
005400 STOP RUN.
005500 LOOP.
005600 READ NEW-DATA INTO DATA-REC;
005700 AT END GO TO FINISH.
005800 IF TRANSACTION-CODE = "A" THEN GO TO ADD.REC,
005900 IF TRANSACTION-CODE NOT = "D" AND "U" THEN
006000 DISPLAY "ILLEGAL TRANSACTION CODE"
006100 DISPLAY DATA-REC
006200 GO TO LOOP.
006300 CALL "CKREADBYKEY" USING FILETABLE, STATUSKEY, MASTER-REC,
006400 NAME OF DATA-REC, KEY-LOC, RECSIZE.
006500 IF STATUS-KEY-1 NOT = "0" THEN
006600 DISPLAY "CKREADBYKEY ERROR, STATUS =", STATUSKEY,
006700 "1 KEY =", NAME OF DATA-REC
006800 IF STATUS-KEY-1 = "9" THEN
006900 CALL "CKERROR" USING STATUSKEY, RESULT
007000 DISPLAY "ERROR NO.", RESULT
007100 GO TO LOOP
007200 ELSE
007300 GO TO LOOP.
007400 IF TRANSACTION-CODE = "D" THEN GO TO DELETE-REC.
007500 MOVE CORR DATA-REC TO MASTER-REC.
007600 CALL "CKREWRITE" USING FILETABLE, STATUSKEY, MASTER-REC,
007700 RECSIZE.
007800 IF STATUS-KEY-1 = "0" THEN
007900 DISPLAY MASTER-REC, "UPDATED"
008000 GO TO LOOP.
008100 DISPLAY "CKREWRITE ERROR, STATUS =", STATUSKEY, "1 KEY ="
008200 NAME OF MASTER-REC.
008300 IF STATUS KEY-1= "9" THEN
008400 CALL "CKERROR" USING STATUSKEY, RESULT
008500 DISPLAY "ERROR NO.", RESULT
008600 GO TO LOOP.
008700 DELETE-REC.
008800 CALL "CKDELETE" USING FILETABLE, STATUSKEY.
008900 IF STATUS-KEY-1 = "0" THEN
009000 DISPLAY MASTER-REC, "DELETED"
009100 GO TO LOOP.
009200 DISPLAY "CKDELETE ERROR, STATUS =" STATUSKEY.
009300 IF STATUS-KEY-1 = "9" THEN
009400 CALL "CKERROR", USING STATUSKEY, RESULT
009500 DISPLAY "ERROR NO.", RESULT.
009600 GO TO LOOP.
009700 ADD-REC.
009800 MOVE CORR DATA-REC TO MASTER-REC.
009900 CALL "CKWRITE" USING FILETABLE, STATUSKEY, MASTER-REC.
010000 RECSIZE.
010100 IF STATUSKEY = "02" THEN
010200 DISPLAY "DUPLICATE KEY",
010300 IF STATUS-KEY-1 = "0" THEN
010400 DISPLAY MASTER-REC, "ADDED"
010500 GO TO LOOP.
010600 DISPLAY "CKWRITE ERROR, STATUS = ", STATUSKEY.
010700 IF STATUS-KEY-1 = "9" THEN
010800 CALL "CKERROR" USING STATUSKEY, RESULT
010900 DISPLAY "ERROR NO. ", RESULT.
011000 DISPLAY MASTER-REC,
011100 GO TO LOOP.
011200 FINISH.
011300 CLOSE NEW-DATA.
011400 CALL "CKCLOSE" USING FILETABLE, STATUSKEY,
011500 IF STATUS-KEY-1 = "9" THEN
011600 CALL "CKERROR" USING STATUSKEY, RESULT
011700 DISPLAY "CKCLOSE ERROR NO.", RESULT
011800 STOP RUN.
Input to EXAMP3:
NOLAN JACK 923-4975 1 ANY STREET. SUNNYVALE CA. 94O87U
SMITH JOHN 555-1212 102 FIRST ST. OUR TOWN CA. 94099A
ECKSTEIN LEO D
CARDIN RICK 257-7000 11100 WOLFE ROAD CUPERTINO CA. 94041U
PASBY LINDAL D
JANE MARY 565-9090 1776 BICENTENNIAL ST. AMAHEIM CA. 91076A
ROBERT GERRY 259-5535 12345 TELEGRAPH AVE. BERKELEY CA. 94704U
TURNEW IVAN D
FORD GERALD 555-1976 1600 PENNSYLVANIS WASHINGTON DC. 20001U
WESTER ELDER 287-4598 1256 KINGFISHER ST. SUNNYVALE CA. 94309A
Output from Execution of EXAMP3:
NOLAN JACK 923-4975 1 ANY STREET. SUNNYVALE CA. 94087 UPDATED
SMITH JOHN 555-1212 102 FIRST ST. OUR TOWN CA. 94099 ADDED
ECKSTEIN LEO 287-5137 5303 STEVENS CREEK SANTA CLARA CA. 95050 DELETED
CARDIN RICK 257-7000 11100 WOLFE ROAD CUPERTINO CA. 94014 UPDATED
PASBY LINDA 295-1187 TOWN & CNTRY VILLAGE SAN JOSE CA. 94102 DELETED
JANE MARY 565-9090 1776 BICENTENNIAL ST. ANAHEIM CA. 91076 ADDED
ROBERT GERRY 259-5535 12345 TELEGRAPH AVE. BERKELEY CA. 94704 UPDATED
CKREADBYKEY ERROR, STATUS = 231 KEY = TURNEW IVAN
CKREADBYKEY ERROR, STATUS = 231 KEY = FORD GERALD
CKWRITE ERROR, STATUS = 22
WESTER ELDER 287-44598 1256 KINGFISHER ST. SUNNYVALE CA. 9430A
|
 |  |  |  |  | NOTE:
Note that the input contains data that results in error messages. The name IVAN TURNEWR is
spelled incorrectly and cannot be found. The name GERALD FORD does not exist in the original
file and also cannot be found. On the other hand, the name ELDER WESTER already exists in the
file and cannot be added since it is a primary key for which duplicates are not allowed. |  |  |  |  |
|