[USflag] The American Programmer [USflag]


Home
Books on Mainframe Programming
Mainframe Manuals and Tutorials
System Abend codes, Sqlcodes, VSAM/QSAM codes
Everything about the IBM AS/400 Midrange Computer - iSeries
Everything about CICS
Cobol programs, manuals, books
  Sample Cobol code: The Simple, Single File COBOL Program
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Sequence Check COBOL Program
  Sample Cobol code: The Record Selection COBOL Program
  Sample Cobol code: The Edit or Validate COBOL Program
  Sample Cobol code: The The One Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Three Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Sequential File, Batch Update COBOL Program
  Sample Cobol code: The COBOL Sort
  Sample Cobol code: The CASE Structure: COBOL EVALUATE
  Sample Cobol code: Direct Subscripting in COBOL
  Sample Cobol code: The Sequential, or Serial Search in COBOL
  Sample Cobol code: The Binary Search in COBOL
  Sample Cobol code: Loading a Table from a Sequential File in a COBOL program
  Sample Cobol code: The VSAM File Read Sequentially in a COBOL program
  Sample Cobol code: The VSAM KSDS, Read Randomly in a COBOL program
  Sample Cobol code: The VSAM File, Read Randomly in a COBOL program
  Sample Cobol code: VSAM Initial Load in a COBOL program
  Sample Cobol code: VSAM File Maintenance (Add, Change, Delete) in a COBOL program
  Sample Cobol code: VSAM Read Sequentially, with START, in a COBOL program
  Sample Cobol code: Creating a Variable Format File in a COBOL program
  Sample Cobol code: Reading a Variable Format File in a COBOL program
  Sample Cobol code: Creating a Variable Format File with Occurs Depending On in a COBOL program
  Sample Cobol code: COBOL Reading a Variable Format File with Occurs Depending On, in a COBOL program
  Sample Cobol code: COBOL The Table Load with Occurs Depending On
  Sample Cobol/DB2 code: Singleton Select
  Manuals on the COBOL programming language.
  Books on Cobol
  Abend Codes from Cobol programs
Everything about DB2 and SQL
Everything about IMS
Everything about Java and JavaScript
Everything about JCL and JES
Everything about REXX
Everything about zOS, VSAM, Tivoli, Assembler
Everything about TSO, ISPF, Spufi
Site Map and Site Search

           Home   > COBOL   > COBOL program VSAM File Maintenance (Add, Change, Delete)

VSAM File Maintenance (Add, Change, Delete) in a COBOL program


20. VSAM File Maintenance (Add, Change, Delete)

VSAMACD1.
This program will randomly add, change and delete records in a VSAM file. It reads a regular sequential file which contains transaction codes for Add, Change and Delete.

This type of file maintenance could be performed just as this program does, or it could be done based on data keyed in on an on line application.

Here is the program VSAMACD1:

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.



000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. VSAMACD1.
000300* ADD, CHANGE OR DELETE VSAM RECORDS
000400* BASED ON TRANSACTION CODES IN A REGULAR FILE READ SEQUENTIALLY
000500* THE LOGIC IS BASED ON THAT OF THE REGULAR FILE READ SEQUENTIALL
000600* IMPORTANT CLAUSES ARE ORGANIZATION IS INDEXED
000700*    ACCESS MODE IS RANDOM OPEN I-O    USE WRITE TO ADD
000900*     CHANGE: DO READ (NO AT END) (23 IS NOT FOUND)
000910*             THEN REWRITE
000920*    ON DELETE, 23 IS NOT FOUND
001000 ENVIRONMENT DIVISION.
001100 CONFIGURATION SECTION.
001200 INPUT-OUTPUT SECTION.
001300 FILE-CONTROL.
001400*    THIS IS AN ORDINARY SEQUENTIAL FILE: EMPTRANS
001500     SELECT IN-FILE  ASSIGN EMPTRANS.
001600*    INPUT VSAM FILE IS: TSOUSR1.VSAMKSDS.EMPSORTD
001700*    JCL TO CREATE INPUT FILE IS VSAMDEF
001800*    USES EMPSORTD AS INPUT TO JCL
001900*    OF COURSE THE NAMES MAY BE DIFFERENT AT YOUR COMPANY
002000     SELECT VSAM-KSDS-FILE ASSIGN VSAMKSDS
002100        ORGANIZATION IS INDEXED
002200*    NOTICE THE RANDOM NEXT
002300        ACCESS MODE IS RANDOM
002400        RECORD KEY IS VSAM-KSDS-RECORD-KEY
000600        FILE STATUS  IS VSAM-STATUS-CODE
000700                        VSAM-EXTENDED-STATUS-CODE.
002600 DATA DIVISION.
002700 FILE SECTION.
002800 FD  IN-FILE
002900     RECORDING MODE IS F
003200     RECORD CONTAINS 80 CHARACTERS.
003210 01  IN-RECORD.
003220     05  TRANS-CODE                    PIC X.
003230     05  FILLER                        PIC X(02).
003230*     THE NEXT WILL BE USED AS THE KEY TO SEARCH IN THE VSAM FILE
003240*     IT HAS TO HAVE THE SAME PICTURE AS THE VSAM RECORD KEY
003250     05  EMPLOYEE-NAME            PIC X(20).
003260     05  EMPLOYEE-INFO                        PIC X(57).
003270
003280 FD  VSAM-KSDS-FILE.
003290 01  VSAM-KSDS-RECORD.
003300     05 VSAM-KSDS-RECORD-KEY    PIC X(20).
003310     05 VSAM-KSDS-EMP-INFO      PIC X(57).
003320     05 FILLER                  PIC X(03).004000 WORKING-STORAGE SECTION.
004100 01 SWITCHES.
004200      05  FILE-AT-END     PIC X  VALUE 'N'.
001800
001900 01  VSAM-STATUS-CODE.
002000     05 VSAM-STATUS-CODE-BYTE1   PIC X.
002100     05 VSAM-STATUS-CODE-BYTE2   PIC X.
002200
002300 01  VSAM-EXTENDED-STATUS-CODE.
002400     05 VSAM-EXTENDED-RETURN-CODE   PIC S9(4) COMP.
002500     05 VSAM-EXTENDED-FUNCTION-CODE PIC S9(4) COMP.
002600     05 VSAM-EXTENDED-FEEDBACK-CODE PIC S9(4) COMP.
005300
006100 PROCEDURE DIVISION.
006200     PERFORM INITIALIZATION
006300     PERFORM PROCESS-ALL
006400         UNTIL FILE-AT-END = 'Y'
006500     PERFORM TERMINATION
006600     GOBACK.
006700
006800 INITIALIZATION.
006900     OPEN INPUT IN-FILE
007000*     NOTE THE OPEN I-O, THIS ALLOWS READ, WRITE, REWRITE, DELETE
007100     OPEN I-O VSAM-KSDS-FILE
007200     IF VSAM-STATUS-CODE IS NOT EQUAL TO '00'
007300     THEN GO TO ERROR-EXIT
007400     END-IF
007500     PERFORM READ-REGULAR-FILE.
007600
007700 PROCESS-ALL.
007800     PERFORM DO-MAINT
007900     PERFORM READ-REGULAR-FILE.
008000
008100 DO-MAINT.
008200     EVALUATE TRANS-CODE
008300      WHEN 'A' PERFORM ADD-TRANS
008400      WHEN 'C' PERFORM CHANGE-TRANS
008500      WHEN 'D' PERFORM DELETE-TRANS
008600       WHEN OTHER
008700                DISPLAY 'UNKNOWN TRANS CODE'
008800                DISPLAY IN-RECORD
008900       END-EVALUATE.
009000
009100 ADD-TRANS.
009200*     MOVE ALL FIELDS
009300      MOVE EMPLOYEE-NAME TO VSAM-KSDS-RECORD-KEY
009400      MOVE EMPLOYEE-INFO    TO VSAM-KSDS-EMP-INFO
009500      WRITE VSAM-KSDS-RECORD
009600      EVALUATE VSAM-STATUS-CODE
009700       WHEN '22'
009800             DISPLAY 'DUPLICATE RECORD ON FILE'
009900             VSAM-KSDS-RECORD
010000       WHEN '00'
010100             DISPLAY 'ADDED - AOK '
010200             VSAM-KSDS-RECORD
010300       WHEN OTHER GO TO ERROR-EXIT
010400
010500       END-EVALUATE.
010700 CHANGE-TRANS.
010800*     MOVE KEY FIELD (DONE IN RANDOM-READ), READ THE RECORD
010900      PERFORM RANDOM-READ
011000      EVALUATE VSAM-STATUS-CODE
011100       WHEN '23'
011200             DISPLAY 'CANNOT CHANGE, NOT FOUND'
011300             VSAM-KSDS-RECORD
011400       WHEN '00'
011500             DISPLAY 'READ - AOK '
011600             VSAM-KSDS-RECORD
011700**      NOW DO THE ACTUAL UPDATE - NOTE REWRITE, NOT WRITE
011800             MOVE EMPLOYEE-INFO    TO VSAM-KSDS-EMP-INFO
011900             REWRITE VSAM-KSDS-RECORD
      *            SHOULD CHECK STATUS CODE FOR REWRITE HERE
012000       WHEN OTHER GO TO ERROR-EXIT
012100       END-EVALUATE.
012200
012400 DELETE-TRANS.
012500*     JUST MOVE THE KEY FIELD
012600      MOVE EMPLOYEE-NAME TO VSAM-KSDS-RECORD-KEY
012700      DELETE VSAM-KSDS-FILE
012800      EVALUATE VSAM-STATUS-CODE
012900       WHEN '23'
013000             DISPLAY 'CANNOT DELETE, NOT ON FILE'
013100             VSAM-KSDS-RECORD
013200        WHEN '00'
013300             DISPLAY 'DELETED - AOK '
013400             VSAM-KSDS-RECORD
013500       WHEN OTHER GO TO ERROR-EXIT
013600       END-EVALUATE.
013700
013800  TERMINATION.
013900     CLOSE IN-FILE VSAM-KSDS-FILE.
014000
014100 READ-REGULAR-FILE.
014200     READ IN-FILE
014300         AT END MOVE 'Y' TO FILE-AT-END
014400     END-READ.
014500
014600 RANDOM-READ.
014700*   EMPLOYEE-NAME COMES FROM THE REGULAR INPUT FILE
014800*   IT MUST BE MOVED TO THE RECORD KEY OF THE VSAM FILE
014900*   THE RECORD KEY IS WHAT VSAM SEARCHES FOR IN THE FILE
015000*    IF IT FINDS IT, IT RETRIEVES THAT ENTIRE RECORD,
015100*    IF NOT, THEN STATUS CODE 23
015200     MOVE EMPLOYEE-NAME TO VSAM-KSDS-RECORD-KEY
015300     READ VSAM-KSDS-FILE
015400*    NOTE: THERE IS NO AT END
015500     END-READ
015600
015700*   YOU DON'T HAVE TO DO THIS EVALUATE HERE, BECAUSE IT IS DONE
015800*    IN CHANGE-TRANS ABOVE. BUT LEAVE IT HERE ANYWAY.
015900     EVALUATE VSAM-STATUS-CODE
016000       WHEN '23'
016100                  DISPLAY 'NOT FOUND' EMPLOYEE-NAME
016200       WHEN '00'
016300                  DISPLAY 'FOUND' VSAM-KSDS-RECORD
016400       WHEN OTHER GO TO ERROR-EXIT
016500       END-EVALUATE.
016600
017400 ERROR-EXIT.
017500*   SEE THE PROGRAM VSAMSEQ2 FOR VSAM STATUS CODES
017510*   AND EXTENDED STATUS CODES
017520*   AND THE REMAINDER OF THIS PARAGRAPH

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.
Here is the input data file userid.VSAMKSDS.EMPSORTD: (the next two lines are a column ruler) 1 2 3 4 5 6 123456789.123456789.123456789.123456789.123456789.123456789.12345678 BUD WEIZER 05000 9 0001 0000001 000000 HERB GARDNER 03000 1 0040 0000055 000022 HUGH MUNGUS 06000 1 0200 0000020 000020 L. A. VATOR 07000 5 0020 0000033 000033 PAT A. GONIA 04000 0 0002 0000022 000011 PEARLE E. GATES 01000 2 0010 0000020 000300 PHIL HARMONIC 02000 3 0030 0000050 000020 Here is the input data file EMPTRANS: (the next two lines are a column ruler) 1 2 3 4 5 6 123456789.123456789.123456789.123456789.123456789.123456789.12345678 A ALLEN ROENTCH 05000 9 0001 0000001 000000 C HERB GARDNER 03000 1 0040 0000055 000022 D HUGH MUNGUS 06000 1 0200 0000020 000020 A L. A. VATOR 07000 5 0020 0000033 000033 C LISA CARR 04000 0 0002 0000022 000011 D MAL A. MUTE 01000 2 0010 0000020 000300 X PHIL HARMONIC 02000 3 0030 0000050 000020 Here is sample JCL: //* vsamacd1.jcl //DEFKSDS EXEC PGM=IDCAMS //SYSPRINT DD SYSOUT=* //SYSIN DD * DELETE (userid.VSAMKSDS.EMPSORTD) CLUSTER DEFINE CLUSTER - (NAME(userid.VSAMKSDS.EMPSORTD) - CYLINDERS(1,1) - KEYS(20,0) - RECORDSIZE(80,80)- INDEXED ) REPRO INFILE(INFILE) OUTDATASET(userid.VSAMKSDS.EMPSORTD) PRINT INDATASET(userid.VSAMKSDS.EMPSORTD) CHARACTER /* //INFILE DD DSN=userid.COBBOOK.DATA(EMPSORTD),DISP=SHR //* //STEP1 EXEC PGM=VSAMACD1 //STEPLIB DD DSN=your.executable.program.library.goes.here,DISP=SHR //*OF COURSE, THE NEXT DATASET NAME MAY BE DIFFERENT AT YOUR COMPANY //VSAMKSDS DD DSN=userid.VSAMKSDS.EMPSORTD,DISP=SHR //EMPTRANS DD DSN=userid.COBBOOK.DATA(EMPTRANS),DISP=SHR //SYSOUT DD SYSOUT=* //SYSUDUMP DD SYSOUT=* //SYSABOUT DD SYSOUT=* //SYSDBOUT DD SYSOUT=* //* //PRINTVSM EXEC PGM=IDCAMS //SYSPRINT DD SYSOUT=* //SYSIN DD * PRINT INDATASET(userid.VSAMKSDS.EMPSORTD) CHARACTER /*

Top of Page

Your email and other personal information will not be given to anyone
[Books Computer]

Home Books for Computer Professionals Privacy Terms |
Site Map and Site Search Programming Manuals and Tutorials The REXX Files Top of Page |