DELETE Statement
The DELETE
statement logically removes a record from a relative, or indexed file.
General Format:
Format 1
DELETE file-1 RECORD
[ INVALID KEY statement-1 ]
[ NOT INVALID KEY statement-2 ]
[ END-DELETE ]
Syntax:
file-n
is a file described in the File Section with an FD.
General Rules:
file-1
must be described withORGANIZATION IS RELATIVE
orORGANIZATION IS INDEXED
.file-1
must beOPEN I-O
when theDELETE file-1 RECORD
statement is executed.- For files declared with
ACCESS IS SEQUENTIAL
:- The
INVALID KEY
/NOT INVALID KEY
clauses cannot be used. - The previous
I/O
statement must be a successfulREAD
. That record isDELETE
’d from the file.
- The
- For files declared with
ACCESS IS DYNAMIC
orACCESS IS RANDOM
:- The
INVALID KEY
/NOT INVALID KEY
clauses can be used
- The
- For relative files, the
RELATIVE KEY
is used to identify the record to beDELETE
’d. If theRELATIVE KEY
data item does not identify a record, then theINVALID KEY
condition is raised. - For indexed files, the
RECORD KEY
is used to identify the record to beDELETE
’d. If theRECORD KEY
data item does not identify a record, then theINVALID KEY
condition is raised. - The
INVALID KEY
condition can be handled programmatically using theINVALID KEY
clause.Statement-1
executes when theINVALID KEY
condition is detected.Statement-2
executes when theINVALID KEY
condition isNOT
detected. - In the absence of the
INVALID KEY
clause, theINVALID KEY
condition causes aFile I/O error
condition, which can be handled inDECLARATIVES
. If it is not handled inDECLARATIVES
, or if there are noDECLARATIVES
, then theINVALID KEY
condition updates the file status variable and aborts the program. - If successful, the
DELETE file-1 RECORD
statement removes the identified record from the file. - The
DELETE file-1 RECORD
statement updates theFILE-STATUS
variable.
Format 2
DELETE FILE <filename>
erases the file and the optional index.
DELETE FILE file-1
[ END-DELETE ]
Syntax:
file-n
is a file described in the File Section with an FD.
General Rules:
When executing a DELETE FILE file-1
statement, file-1
must be CLOSE
'd.
Code Sample:
IDENTIFICATION DIVISION.
PROGRAM-ID. DELETE-1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT RESWORDS ASSIGN TO "RESWORDS"
ORGANIZATION IS INDEXED
ACCESS IS DYNAMIC
RECORD KEY IS RESERVED-WORD
FILE STATUS IS RESWORDS-STAT.
DATA DIVISION.
FILE SECTION.
FD RESWORDS.
01 RESWORDS-RECORD.
03 RESERVED-WORD PIC X(30).
WORKING-STORAGE SECTION.
77 RESWORDS-STAT PIC XX.
88 END-OF-RESWORDS VALUE "10".
77 DUMMY PIC X.
PROCEDURE DIVISION.
MAIN.
OPEN OUTPUT RESWORDS.
MOVE "ACCEPT" TO RESERVED-WORD.
WRITE RESWORDS-RECORD.
MOVE "ADD" TO RESERVED-WORD.
WRITE RESWORDS-RECORD.
CLOSE RESWORDS.
OPEN I-O RESWORDS.
MOVE "ACCEPT" TO RESERVED-WORD.
READ RESWORDS.
* DELETE FILE-NAME RECORD
* [ INVALID KEY STATEMENT-1 ]
* [ NOT INVALID KEY STATEMENT-2 ]
* [ END-DELETE ]
DELETE RESWORDS RECORD.
IF RESWORDS-STAT = "00"
DISPLAY"RECORD DELETED!"LINE 5 COL 10
ELSE
DISPLAY "ERROR! FILE STATUS: " LINE 7 COL 10
DISPLAY RESWORDS-STAT LINE 7 COL 31
END-IF.
DISPLAY "DELETE-1 FINISHED!" LINE 10 COL 10.
ACCEPT DUMMY LINE 10 COL 30.
STOP RUN.