[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 the VSAM KSDS, Read Randomly

The VSAM KSDS, Read Randomly in a COBOL program


17. The VSAM KSDS, Read Randomly

VSAMRND1.
This program reads the same file as the previous program, VSAMSEQ1, but it reads it randomly. The program gives a key field (employee name in this case) to VSAM and says “get me the record with this key field.” VSAM will find the record and give the entire record to your program, or perhaps it won’t be able to find it and will give you a status code of 23 which means “not found.” This program will just read two records, and the key field data will be hard coded - it will come from literals coded right in the program. This technique will keep the logic to a minimum and allow you to concentrate on the VSAM coding.

In the Real World, you will probably not be reading a VSAM file randomly based on hard coded information. However, this program will get you started reading VSAM randomly. When faced with a real application, you will be able to use the VSAM coding effectively.

Here is the program VSAMRND1:

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



000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. VSAMRND1.
000300* READ TWO VSAM KSDS RECORDS RANDOMLY
000400* BASED ON HARD CODED DATA
000500* JUST DISPLAY THE VSAM RECORDS
000600* NOTHING COMPLEX IN THIS PROGRAM
000700* THE LOGIC IS SIMPLE STRAIGHT LINE LOGIC
000800 ENVIRONMENT DIVISION.
000900 CONFIGURATION SECTION.
001000 INPUT-OUTPUT SECTION.
001100 FILE-CONTROL.
001200*    INPUT VSAM FILE IS: TSOUSR1.VSAMKSDS.EMPSORTD
001300*    JCL TO CREATE INPUT FILE IS VSAMDEF.JCL
001400*    USES EMPSORTD AS INPUT TO JCL
001500*    OF COURSE THE NAMES MAY BE DIFFERENT AT YOUR COMPANY
001600     SELECT VSAM-KSDS-FILE ASSIGN VSAMKSDS
001700        ORGANIZATION IS INDEXED
001800*    NOTICE THE RANDOM NEXT
001900        ACCESS MODE IS RANDOM
002000        RECORD KEY IS VSAM-KSDS-RECORD-KEY
000600        FILE STATUS  IS VSAM-STATUS-CODE
000700                        VSAM-EXTENDED-STATUS-CODE.
002200 DATA DIVISION.
002300 FILE SECTION.
002400
002500 FD  VSAM-KSDS-FILE.
002600 01  VSAM-KSDS-RECORD.
002700*    THE NEXT FIELD IN THIS PROGRAM IS THE RECORD KEY FIELD
002800*    THE KEY USED FOR SEARCHING IN THE FILE
002900*    YOU WILL BE MOVING A DATA VALUE TO THIS FIELD B4 THE READ
003000*    THE 20 MEANS THE KEY FIELD HAS A LENGTH OF 20
003100*    REFER  BACK TO THE JCL WHICH CREATES THE VSAM FILE
003200*    BECAUSE THAT IS WHERE THE 20 COMES FROM
003300*    THE RECORD KEY IS ACTUALLY EMPLOYEE NAME IN THIS DATA FILE
003400     05  VSAM-KSDS-RECORD-KEY    PIC X(20).
003500     05  VSAM-KSDS-EMP-INFO      PIC X(60).
003700 WORKING-STORAGE SECTION.
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.
004900
005000 PROCEDURE DIVISION.
005100     PERFORM INITIALIZATION
005200     PERFORM PROCESS-ALL
005300**   NOTICE NO UNTIL IN THIS PROGRAM
005400**   THIS PROGRAM WILL JUST BE READING 2 RECORDS
005500*       NO LOOP, NO UNTIL
005600     PERFORM TERMINATION
005700     GOBACK.
005800
005900 INITIALIZATION.
006000     OPEN INPUT VSAM-KSDS-FILE
006100     IF VSAM-STATUS-CODE IS NOT EQUAL TO '00'
006200     THEN GO TO ERROR-EXIT
006300     END-IF.
006400** NO NEED TO DO A READ HERE IN THIS PROGRAM
006500
006600 PROCESS-ALL.
006700*    THIS IS ALL THERE IS IN PROCESS-ALL PARAGRAPH IN THIS EXAMPLE
006800*    IT JUST READS 2 RECORDS RANDOMLY
006900       MOVE 'BUD WEIZER' TO VSAM-KSDS-RECORD-KEY
007000       PERFORM RANDOM-READ
007100       IF VSAM-STATUS-CODE =       '00'
007200       THEN DISPLAY 'RECORD READ' VSAM-KSDS-RECORD
007210       ELSE IF VSAM-STATUS-CODE =       '23'
007230       THEN DISPLAY 'RECORD NOT FOUND' VSAM-KSDS-RECORD-KEY
007300       ELSE GO TO ERROR-EXIT
007400       END-IF
007500
007600       MOVE 'L. A. VATOR' TO VSAM-KSDS-RECORD-KEY
007700       PERFORM RANDOM-READ
007800       IF VSAM-STATUS-CODE =       '00'
007900       THEN DISPLAY 'RECORD READ' VSAM-KSDS-RECORD
007910       ELSE IF VSAM-STATUS-CODE =       '23'
007930       THEN DISPLAY 'RECORD NOT FOUND' VSAM-KSDS-RECORD-KEY
008000       ELSE GO TO ERROR-EXIT
008100       END-IF.
008200
008300 RANDOM-READ.
008400*    PLEASE NOTE THAT THERE IS NO AT END
008500*      (AT END IS FOR SEQUENTIAL READING ONLY, THIS IS RANDOM)
008600       READ VSAM-KSDS-FILE
008700       END-READ.
009000
009100 TERMINATION.
009200     CLOSE VSAM-KSDS-FILE.
009300
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 (check to see what name was actually used) (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 sample JCL: //* vsamrnd1.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) //*OF COURSE, THE PREVIOUS DATASET NAME MAY BE DIFFERENT AT YOUR COMPANY /* //INFILE DD DSN=userid.COBBOOK.DATA(EMPSORTD),DISP=SHR //* //STEP1 EXEC PGM=VSAMRND1 //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 //SYSOUT DD SYSOUT=* //SYSUDUMP DD SYSOUT=* //SYSABOUT DD SYSOUT=* //SYSDBOUT DD SYSOUT=* //*

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 |