Logic to Delete record from VSAM file

3.9k Views Asked by At

I have Input file as VSAM file and Parm value passed from JCL. if these two match the record needs to be deleted from the input VSAM file. Please could you give me the logic in Cobol code for this.

1

There are 1 best solutions below

0
Srinivasan JV On

I've asssumed that the PARM value passed from the JCL is the Primary key to the VSAM file. Please go through the following code and check if it meets your requirement.

In the VSAM file you would have a 6 byte field for Employee ID (acting as the key) and 20 byte field for Employee name. The PARM value passed from the JCL is the Employee ID. The Access mode of VSAM file is random and the PARM value is being used to delete the record in the VSAM file, if present.

ID DIVISION.                             
PROGRAM-ID. VSAM01.                      
ENVIRONMENT DIVISION.                    
INPUT-OUTPUT SECTION.                    
FILE-CONTROL.                            
    SELECT VSAMKSDS ASSIGN TO VSAMKSDS   
    ORGANIZATION IS INDEXED              
    ACCESS MODE IS RANDOM               
    RECORD KEY IS ASSOC-ID               
    FILE STATUS IS WS-VSAM-ST.           
DATA DIVISION.                           
FILE SECTION.                            
FD VSAMKSDS.                             
01 IN-RECORD.                            
   05 ASSOC-ID          PIC X(6).        
   05 FILLER            PIC X(1).        
   05 ASSOC-NAME        PIC X(20).       
WORKING-STORAGE SECTION.                 
01 WS-SWITCHES.                          
   05 WS-EOF            PIC X(1).        
      88 EOF            VALUE 'Y'.       
      88 NOT-EOF        VALUE 'N'.       
01 WS-DELETE            PIC 9(2) VALUE 0.
01 WS-VSAM-ST           PIC X(2).        
LINKAGE SECTION.                       
01 LS-PARM-INFO.                       
   05 WS-PARM-LENGTH    PIC S9(4) COMP.
   05 WS-PARM           PIC X(6).      
PROCEDURE DIVISION USING LS-PARM-INFO. 
    PERFORM OPEN-PARA.                 
    PERFORM PROCESS-PARA.              
    PERFORM CLOSE-PARA.                

OPEN-PARA.                             
    OPEN I-O VSAMKSDS                  
    IF WS-VSAM-ST NOT EQUAL '00'       
    PERFORM ERROR-PARA.                

PROCESS-PARA.                          
    MOVE WS-PARM TO ASSOC-ID.          
    DELETE VSAMKSDS.                   
    EVALUATE WS-VSAM-ST                
    WHEN '00'                          
    DISPLAY 'RECORD DELETED'           
    ADD 1 TO WS-DELETE                 
    WHEN '23'                          
    DISPLAY 'NOT FOUND:' WS-PARM       
    WHEN OTHER                         
    PERFORM ERROR-PARA                 
    END-EVALUATE.                      

CLOSE-PARA.                            
    CLOSE VSAMKSDS. 
    STOP RUN.                   

ERROR-PARA.                            
    DISPLAY 'VSAM STATUS:' WS-VSAM-ST. 
    CLOSE VSAMKSDS.
    STOP RUN.