Deleting Records in an Indexed File

As time passes, you will need to delete records from a file. You might delete vendors with whom you no longer do business. A bills file might retire records for paid bills from the active file by deleting the records from the main file and writing them to a history file.

The command to delete a record is DELETE file-name, and it uses INVALID KEY to trap file-error conditions. Here is the syntax:
DELETE file-name RECORD
INVALID KEY
do something
The following is an example of DELETE file-name:
DELETE VENDOR-FILE RECORD
INVALID KEY
DISPLAY "ERROR DURING DELETE"
You must read a record successfully before you can delete it. As with REWRITE, some large systems require that the record is read WITH LOCK or WITH HOLD before the DELETE command will succeed.

Sample code output
ENTER VENDOR NUMBER OF THE VENDOR
TO DELETE (1-99999)
ENTER 0 TO STOP ENTRY
1
VENDOR NUMBER: 00001
1. VENDOR NAME: JOHNSON AND SON
2. VENDOR ADDRESS-1: 1212 MAIN ST
3. VENDOR ADDRESS-2: # 455
4. VENDOR CITY: LOS ANGELES
5. VENDOR STATE: CA
6. VENDOR ZIP: 91042
7. VENDOR CONTACT: LINDA MARTIN
8. VENDOR PHONE: (213) 555-1234
DELETE THIS RECORD (Y/N)?
N
ENTER VENDOR NUMBER OF THE VENDOR
TO DELETE (1-99999)
ENTER 0 TO STOP ENTRY
1
VENDOR NUMBER: 00001
1. VENDOR NAME: JOHNSON AND SON
2. VENDOR ADDRESS-1: 1212 MAIN ST
3. VENDOR ADDRESS-2: # 455
4. VENDOR CITY: LOS ANGELES
5. VENDOR STATE: CA
6. VENDOR ZIP: 91042
7. VENDOR CONTACT: LINDA MARTIN
8. VENDOR PHONE: (213) 555-1234
DELETE THIS RECORD (Y/N)?
Y
ENTER VENDOR NUMBER OF THE VENDOR
TO DELETE (1-99999)
ENTER 0 TO STOP ENTRY
1
RECORD NOT FOUND
ENTER VENDOR NUMBER OF THE VENDOR
TO DELETE (1-99999)
ENTER 0 TO STOP ENTRY
The primary difference between vnddel01.cbl and vndchg01.cbl is in DELETE-RECORDS at line 008200. At this point in vndchg01.cbl, the user is asked which field to change and data entry is accepted for the changed field. In vnddel01.cbl, instead of asking the user for a field, the program asks the user whether this record should be deleted and then deletes the record if the user answers yes.

This gives the user a chance to see the record to make sure that it is the one to be deleted, rather than just deleting it.

One piece of coding you have not seen before appears at line 008700 as a test: UNTIL OK-TO-DELETE = "Y" OR "N". This is equivalent to OK-TO-DELETE = "Y" OR OK-TO-DELETE ="N".

Code, compile, and test this program carefully. It is important in a deletion program to ensure that the record is deleted only when the user answers yes. It can be disconcerting, to say the least, to have records disappear for no apparent reason.

To test this program, run it and specify a vendor number. When the record is displayed, enter N for no to the OK TO DELETE question. Then request that record number again. If the record has been deleted accidentally, you will receive a RECORD NOT FOUND message.

Enter a vendor number and specify Y for yes to the OK TO DELETE question. Then request that record number again. This time you should receive the RECORD NOT FOUND message.

Displaying Records in an Indexed File

Displaying a single record in an indexed file usually is called an inquiry.

The user asks for the data on a specific vendor, and the information is displayed on the screen for reference. The program does not allow any changes and is used only for lookup actions.
008600*--------------------------------
008700* DISPLAY ALL FIELDS WITH BLANK
008800* LINES ABOVE AND BELOW.
008900*--------------------------------
009000 DISPLAY-ALL-FIELDS.
009100     DISPLAY " ".
009200     PERFORM DISPLAY-VENDOR-NUMBER.
009300     PERFORM DISPLAY-VENDOR-NAME.
009400     PERFORM DISPLAY-VENDOR-ADDRESS-1.
009500     PERFORM DISPLAY-VENDOR-ADDRESS-2.
009600     PERFORM DISPLAY-VENDOR-CITY.
009700     PERFORM DISPLAY-VENDOR-STATE.
009800     PERFORM DISPLAY-VENDOR-ZIP.
009900     PERFORM DISPLAY-VENDOR-CONTACT.
010000     PERFORM DISPLAY-VENDOR-PHONE.
010100     DISPLAY " ".
010200
010300 DISPLAY-VENDOR-NUMBER.
010400     DISPLAY "   VENDOR NUMBER: " VENDOR-NUMBER.
010500
010600 DISPLAY-VENDOR-NAME.
010700     DISPLAY "1. VENDOR NAME: " VENDOR-NAME.
010800
010900 DISPLAY-VENDOR-ADDRESS-1.
011000     DISPLAY "2. VENDOR ADDRESS-1: " VENDOR-ADDRESS-1.
011100
Printing Records in an Indexed File
014000 WRITE-TO-PRINTER.
014100     WRITE PRINTER-RECORD BEFORE ADVANCING 1.
014200     ADD 1 TO LINE-COUNT.
Summary
The basics of keeping indexed files updated include deleting, displaying, adding, and printing records. Today, you learned the following:
  • Records are deleted by using the DELETE command. The DELETE command uses INVALID KEY to trap file-error conditions
  • You must have successfully read a record before you can delete it. Some larger systems might require a READ WITH LOCK or a READ WITH HOLD
  • Programs that allow you to add, change, look up, or delete records in a file usually are called maintenance programs
Sample Code
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. VNDDEL01.
000300*--------------------------------
000400* Delete records in Vendor File.
000500*--------------------------------
000600 ENVIRONMENT DIVISION.
000700 INPUT-OUTPUT SECTION.
000800 FILE-CONTROL.
000900
001000     COPY "SLVND01.CBL".
001100
001200 DATA DIVISION.
001300 FILE SECTION.
001400
001500     COPY "FDVND02.CBL".
001600
001700 WORKING-STORAGE SECTION.
001800
001900 77  OK-TO-DELETE        PIC X.
002000 77  RECORD-FOUND        PIC X.
002100
002200 77  VENDOR-NUMBER-FIELD PIC Z(5).
002300
002400 PROCEDURE DIVISION.
002500 PROGRAM-BEGIN.
002600     OPEN I-O VENDOR-FILE.
002700     PERFORM GET-VENDOR-RECORD.
002800     PERFORM DELETE-RECORDS
002900        UNTIL VENDOR-NUMBER = ZEROES.
003000     CLOSE VENDOR-FILE.
003100
003200 PROGRAM-DONE.
003300     STOP RUN.
003400
003500*--------------------------------
003600* TO GET A VENDOR RECORD, ASK FOR
003700* VENDOR NUMBER, AND THEN TRY TO
003800* READ THE RECORD.
003900*--------------------------------
004000 GET-VENDOR-RECORD.
004100     PERFORM INIT-VENDOR-RECORD.
004200     PERFORM ENTER-VENDOR-NUMBER.
004300     MOVE "N" TO RECORD-FOUND.
004400     PERFORM FIND-VENDOR-RECORD
004500         UNTIL RECORD-FOUND = "Y" OR
004600               VENDOR-NUMBER = ZEROES.
004700
004800 INIT-VENDOR-RECORD.
004900     MOVE SPACE TO VENDOR-RECORD.
005000     MOVE ZEROES TO VENDOR-NUMBER.
005100
005200 ENTER-VENDOR-NUMBER.
005300     DISPLAY " ".
005400     DISPLAY "ENTER VENDOR NUMBER OF THE VENDOR" .
005500     DISPLAY "TO DELETE (1-99999)".
005600     DISPLAY "ENTER 0 TO STOP ENTRY".
005700     ACCEPT VENDOR-NUMBER-FIELD.
005800*OR  ACCEPT VENDOR-NUMBER-FIELD WITH CONVERSION.
005900
006000     MOVE VENDOR-NUMBER-FIELD TO VENDOR-NUMBER.
006100
006200 FIND-VENDOR-RECORD.
006300     PERFORM READ-VENDOR-RECORD.
006400     IF RECORD-FOUND = "N"
006500         DISPLAY "RECORD NOT FOUND"
006600         PERFORM ENTER-VENDOR-NUMBER.
006700
006800 READ-VENDOR-RECORD.
006900     MOVE "Y" TO RECORD-FOUND.
007000     READ VENDOR-FILE RECORD
007100       INVALID KEY
007200          MOVE "N" TO RECORD-FOUND.
007300
007400*or  READ VENDOR-FILE RECORD WITH LOCK
007500*      INVALID KEY
007600*         MOVE "N" TO RECORD-FOUND.
007700
007800*or  READ VENDOR-FILE RECORD WITH HOLD
007900*      INVALID KEY
008000*         MOVE "N" TO RECORD-FOUND.
008100
008200 DELETE-RECORDS.
008300     PERFORM DISPLAY-ALL-FIELDS.
008400     MOVE "X" TO OK-TO-DELETE.
008500
008600     PERFORM ASK-TO-DELETE
008700        UNTIL OK-TO-DELETE = "Y" OR "N".
008800
008900     IF OK-TO-DELETE = "Y"
009000         PERFORM DELETE-VENDOR-RECORD.
009100
009200     PERFORM GET-VENDOR-RECORD.
009300
009400*--------------------------------
009500* DISPLAY ALL FIELDS WITH BLANK
009600* LINES ABOVE AND BELOW.
009700*--------------------------------
009800 DISPLAY-ALL-FIELDS.
009900     DISPLAY " ".
010000     PERFORM DISPLAY-VENDOR-NUMBER.
010100     PERFORM DISPLAY-VENDOR-NAME.
010200     PERFORM DISPLAY-VENDOR-ADDRESS-1.
010300     PERFORM DISPLAY-VENDOR-ADDRESS-2.
010400     PERFORM DISPLAY-VENDOR-CITY.
010500     PERFORM DISPLAY-VENDOR-STATE.
010600     PERFORM DISPLAY-VENDOR-ZIP.
010700     PERFORM DISPLAY-VENDOR-CONTACT.
010800     PERFORM DISPLAY-VENDOR-PHONE.
010900     DISPLAY " ".
011000
011100 DISPLAY-VENDOR-NUMBER.
011200     DISPLAY "   VENDOR NUMBER: " VENDOR-NUMBER.
011300
011400 DISPLAY-VENDOR-NAME.
011500     DISPLAY "1. VENDOR NAME: " VENDOR-NAME.
011600
011700 DISPLAY-VENDOR-ADDRESS-1.
011800     DISPLAY "2. VENDOR ADDRESS-1: " VENDOR-ADDRESS-1.
011900
012000 DISPLAY-VENDOR-ADDRESS-2.
012100     DISPLAY "3. VENDOR ADDRESS-2: " VENDOR-ADDRESS-2.
012200
012300 DISPLAY-VENDOR-CITY.
012400     DISPLAY "4. VENDOR CITY: " VENDOR-CITY.
012500
012600 DISPLAY-VENDOR-STATE.
012700     DISPLAY "5. VENDOR STATE: " VENDOR-STATE.
012800
012900 DISPLAY-VENDOR-ZIP.
013000     DISPLAY "6. VENDOR ZIP: " VENDOR-ZIP.
013100
013200 DISPLAY-VENDOR-CONTACT.
013300     DISPLAY "7. VENDOR CONTACT: " VENDOR-CONTACT.
013400
013500 DISPLAY-VENDOR-PHONE.
013600     DISPLAY "8. VENDOR PHONE: " VENDOR-PHONE.
013700
013800 ASK-TO-DELETE.
013900     DISPLAY "DELETE THIS RECORD (Y/N)?".
014000     ACCEPT OK-TO-DELETE.
014100     IF OK-TO-DELETE= "y"
014200         MOVE "Y" TO OK-TO-DELETE.
014300     IF OK-TO-DELETE= "n"
014400         MOVE "N" TO OK-TO-DELETE.
014500     IF OK-TO-DELETE NOT = "Y" AND
014600        OK-TO-DELETE NOT = "N"
014700         DISPLAY "YOU MUST ENTER YES OR NO".
014800
014900 DELETE-VENDOR-RECORD.
015000     DELETE VENDOR-FILE RECORD
015100         INVALID KEY
015200         DISPLAY "ERROR DELETING VENDOR RECORD".
015300