COBOL File Delete

COBOL File Rewrite COBOL File Start

Advertisements


DELETE is used to delete the record which is read in the latest READ of the file.

To delete the record, file should be opened in I-O mode.

Specific record deletion is not possible in sequential files.

Whenever we open a sequential file in output mode for second time, all the previous data get refreshed (deleted).


Syntax:

DELETE File-name RECORD
	[INVALID KEY Statements]
	[NOT INVALID KEY Statements]
END-DELETE.

INVALID KEY and NOT INVALID KEY is used to validate the passed key is valid or not when the file is reading randomly.

INVALID KEY and NOT INVALID KEY option is available for other than indexed files.


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.                                                    


COBOL File Rewrite COBOL File Start

Advertisements