This procedure logically deletes a record from a KSAM file.
In order to logically delete records from a KSAM file, you
can use the procedure CKDELETE. If reuse is not specified, then a logically deleted
record is marked for deletion, but is not physically removed from
the file. The deletion mark makes such a record inaccessible but does
not physically reduce the size of the file. The utility program
FCOPY can be used to compact a KSAM file by copying only active
records, excluding deleted records, to a new KSAM file.
Operation Notes |
 |
In order to delete a record, you should first read the record
into the working storage section of your program with a call to CKREAD if in sequential mode, a call to CKREADBYKEY if in random mode, or a call to either if in dynamic
mode. CKDELETE can be called only if the file is currently open
for both input and output (input/output type =2). This allows the
record to be read into your program's data area and then written
back to the file with the delete mark. Following execution of CKDELETE, the deleted record can no longer be accessed.
If the file was opened for shared access with CKOPENSHR, you must lock the file with CKLOCK before you can delete any records with CKDELETE. Because CKDELETE depends on the logical record pointer, the call
to CKLOCK should precede the call that positions the pointer.
The call to CKUNLOCK is then called after the call to CKDELETE. To illustrate, the sequence of calls in shared
access should be:
CKLOCK <--- to lock file CKSTART or CKREADBYKEY <--- to position pointer . . . CKDELETE<--- to delete record at which pointer is positioned CKUNLOCK<--- to unlock file |
Following the call to CKDELETE, the pointer is positioned to the next key following
the key in the deleted record.
The following examples show the use of CKDELETE for sequential access using CKREAD and for random access using CKREADBYKEY. The WORKING-STORAGE SECTION from Figure A-2 “Representation of KSAMFILE Used in COBOL
Examples” and the FINISH procedure from the CKCLOSE example are assumed for these examples.
 |
 |  |
 |
 | NOTE: If access is shared, the file must be opened with a
call to CKOPENSHR and then locked before the call to CKSTART that initially sets the pointer. The file must remain
locked while the records to be deleted are read and then marked
for deletion. If the file is not locked before CKSTART is called, other users can change the file so
that the record pointer points to the wrong record. |
 |
 |  |
 |
In the first example, to delete all records whose primary
key begins with "P", first position the file to the start of these
records with CKSTART and then read each record with CKREAD and delete it with CKDELETE.
WORKING-STORAGE SECTION.
77 RELOP PIC S9(4) COMP.
77 KEYVAL PIC X(20).
77 KEYLOC PIC S9(4) COMP.
77 KEYLENGTH PIC S9(4) COMP.
.
.
.
PROCEDURE DIVISION.
START.
MOVE 2 TO I-O-TYPE.
MOVE 0 TO A-MODE.
CALL "CKOPEN" USING FILETABLE, STAT.
.
.
.
FIND-REC.
MOVE 0 TO RELOP.<--- test for equality between
primary key and KEY
MOVE "P" TO KEYVAL.
MOVE 3 TO KEYLOC.
MOVE 1 TO KEYLENGTH.<--- check first character only
CALL "CKSTART" USING FILETABLE, STAT, RELOP, KEYVAL, KEYLOC,
KEYLENGTH.
IF STATUS-KEY-1 = "0" THEN
GO TO READ-REC.
IF STAT = "23" THEN
DISPLAY "NO RECORD FOUND"
GO TO FINISH.
IF STATUS-KEY-1 = "9" THEN
CALL "CKERROR" USING STAT, RESULT
DISPLAY "CKERROR NO.=", RESULT
GO TO FINISH.
READ-REC.
CALL "CKREAD" USING FILETABLE, STAT, REC, RECSIZE.
IF STATUS-KEY-1 = "1" THEN
DISPLAY "END OF FILE REACHED"
GO TO FINISH.
IF STATUS-KEY-1 = "0" THEN
IF NAME OF REC NOT LESS THAN "Q "THEN
DISPLAY "DELETIONS COMPLETED"
GO TO FINISH;
ELSE GO TO DELETE-REC;
ELSE
DISPLAY "CKREAD ERROR, STATUS =", STAT
IF STATUS-KEY-1 = "9" THEN
CALL "CKERROR" USING STAT, RESULT
DISPLAY "CKERROR NO.", RESULT.
GO TO READ-REC.
DELETE-REC.
CALL "CKDELETE" USING FILETABLE, STAT.
IF STATUS-KEY-1 = "0" THEN
DISPLAY "DELETED"
GO TO READ-REC;
ELSE
DISPLAY "CKDELETE ERROR, STATUS = ", STAT
IF STATUS-KEY-1 = "9" THEN
CALL "CKERROR" USING STAT, RESULT
DISPLAY"CKERROR NO.=", RESULT
GO TO READ-REC.
In the second example, a file containing the primary keys
of those records to be deleted from a KSAM file is read into the
working storage area DAT. These key values are used by CKREADBYKEY to locate and read the items to be deleted by CKDELETE.
PROCEDURE DIVISION.
START.
MOVE 2 TO I-O-TYPE, A-MODE.
CALL "CKOPEN" USING FILETABLE, STAT.
.
.
.
READ-KEY.
READ DATA-FILE INTO DAT;
AT END GO TO FINISH.
CALL "CKREADBYKEY" USING FILETABLE, STAT, REC, NAME OF DAT, KEYLOC,
RECSIZE.
IF STATUS-KEY-1 = "0" THEN
GO TO DELETE-RECORD.
DISPLAY "CKREADBYKEY ERROR, STATUS = ",STAT.
IF STATUS-KEY-1 = "9" THEN
CALL "CKERROR" USING STAT, RESULT
DISPLAY "CKERROR ", RESULT
GO TO READ-KEY.
DELETE-RECORD.
CALL "CKDELETE" USING FILETABLE, STAT.
IF STATUS-KEY-1 = "0" THEN
DISPLAY REC, " DELETED"
GO TO READ-KEY.
DISPLAY "CKDELETE ERROR, STATUS =",STAT.
IF STATUS-KEY-1 = "9" THEN
CALL "CKERROR" USING STAT, RESULT
DISPLAY "CKERROR NO. =", RESULT.
GO TO READ-KEY.
 |
 |  |
 |
 | NOTE: If access is shared, the file must be opened with a
call to CKOPENSHR. A call to CKLOCK must precede the call to CKREADBYKEY. A call to CKUNLOCK must follow the CKDELETE error tests and should precede the return to READ-KEY. |
 |
 |  |
 |