Summary -

In this topic, we described about the below sections -

The DELETE statement removes a record from an indexed or relative file. DELETE not applicable to sequential files as the record deletion is not possible. The file must open in I-O mode, to execute the DELETE statement on it. After successful execution of a DELETE statement, the record is removed from the file and can no longer be accessed. Once the record gets deleted, the below things happen -

  • For indexed files, the space available for any new record and the key can be reused for record addition.
  • For relative files, the space is available for a new record with the same RELATIVE KEY value.

Syntax -

DELETE Statement Syntax

file-name-1 -

Should be defined in an FD entry in the data division. Should be the name of an indexed or relative file. Execution of the DELETE statement does not affect the contents of the record area or data item associated with file-name-1.

If the FILE STATUS clause is specified, the associated file status key is updated with execution status of the DELETE statement. The file position indicator is not affected by execution of the DELETE statement. The records are deleted like below based on the access mode -

Sequential access mode -

The previous READ statement must be a successfully executed before delete statement. When the DELETE statement is executed, the system removes the record that was retrieved by the previous successful READ statement.

The INVALID KEY and NOT INVALID KEY phrases are not applicable for the files accessing sequentially. An EXCEPTION/ERROR procedure can be used for the files accessing sequentially.

Random or Dynamic access mode -

When the DELETE statement is executed on indexed files, the system removes the record identified by the contents of the RECORD KEY data item. When the DELETE statement is executed on relative files, the system removes the record identified by the contents of the RELATIVE KEY data item for relative files.

If the file does not contain record trying to delete, an INVALID KEY condition exists. After the successful execution of DELETE statement and NOT INVALID KEY phase specified, the imperative statements specified with NOT INVALID KEY are executed.

END-DELETE phrase -

The END-DELETE used to end the scope of the DELETE statement. END-DELETE is not required when DELETE statement ended with period.

Note! Opening a sequential file in output mode for second time, all the file data get refreshed (deleted).

Practical Example - DELETE:

Below example covers the matching record DELETE concept from file

Code:


  IDENTIFICATION DIVISION.                                         
  PROGRAM-ID. RECDELET.                                            
  ENVIRONMENT DIVISION.                                            
  INPUT-OUTPUT SECTION.                                            
  FILE-CONTROL.                                                    
      SELECT FILE1 ASSIGN TO DISK1.                                
      ORGANIZATION IS INDEXED.                                     
      ACCESS MODE IS RANDOM.                                       
      RECORD KEY  IS STD-NO.                                       
      FILE STATUS IS WS-FS.                                        
  DATA DIVISION.                                                   
  FILE SECTION.                                                    
  FD  FILE1.                                                       
  01 STD-REC.                                                      
      02 STD-NO          PIC 9(03).                                
      02 STD-NAME        PIC X(20).                                
      02 STD-GENDER      PIC X(07).                                
      02 FILLER          PIC X(50).                                
  WORKING-STORAGE SECTION.                                         
  77 WS-FS               PIC 9(02).                                
  01 WS-EOF-SW           PIC X(01) VALUE 'N'.                      
     88 EOF-SW           VALUE 'Y'.                                
     88 NOT-EOF-SW       VALUE 'N'.                                
  PROCEDURE DIVISION.                                              
      ACCEPT STD-NO.                                               
      DISPLAY 'FILE PROCESSING - MATCHING RECORD DELETE...'.       
      OPEN INPUT FILE1.                                            
      PERFORM UNTIL EOF-SW                                         
         READ FILE1                                                
         KEY IS STD-NO                                             
         AT END MOVE 'Y'  TO WS-EOF-SW                             
         IF WS-FS = 00                                             
            DELETE STD-REC                                         
            MOVE 'Y'  TO WS-EOF-SW                                 
         ELSE                                                      
            DISPLAY ' RECORD NOT FOUND IN FILE...'                 
         END-IF                                                    
      END-PERFORM.                                                 
      CLOSE FILE1.                                                 
      DISPLAY STD-REC.                                             
      STOP RUN.