[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 File, Read Randomly, Based on Records in a Regular Sequential File

The VSAM File, Read Randomly, Based on Records in a Regular Sequential File, in a COBOL program


18. The VSAM File, Read Randomly, Based on Records in a Regular Sequential File

VSAMRND2.
This is more Real World. There are at least two situations in which you might read a VSAM file randomly:

You are reading an ordinary sequential file in which the records are not in the same order as the VSAM file. You donít want to sort the sequential file, so you read them in the order they are in and then randomly read the corresponding record from the VSAM file. The model program VSAMRND2 works like this.

You have an on-line, interactive application. Data is keyed in at the terminal in no special order. You have to read the VSAM file randomly.

Here is the program VSAMRND2:

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


000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. VSAMRND2.
000300* READ VSAM KSDS RECORDS RANDOMLY
000400* BASED ON A REGULAR FILE READ SEQUENTIALLY
000500* JUST DISPLAY THE VSAM RECORDS
000600* NOTHING COMPLEX IN THIS PROGRAM
000700* THE LOGIC IS SAME AS REGULAR FILE READ SEQUENTIALLY
000800* IMPORTANT CLAUSES ARE  ORGANIZATION IS INDEXED
000900* ACCESS MODE IS RANDOM. OPEN INPUT
001000 ENVIRONMENT DIVISION.
001100 CONFIGURATION SECTION.
001200 INPUT-OUTPUT SECTION.
001300 FILE-CONTROL.
001400*    THIS IS AN ORDINARY SEQUENTIAL FILE: EMPNAMES
001500     SELECT IN-FILE  ASSIGN EMPNAMES.
001600*    INPUT VSAM FILE IS: TSOUSR1.VSAMKSDS.EMPSORTD
001700*    USES EMPSORTD AS INPUT TO JCL
001800*    OF COURSE THE NAMES MAY BE DIFFERENT AT YOUR COMPANY
001900     SELECT VSAM-KSDS-FILE ASSIGN VSAMKSDS
002000        ORGANIZATION IS INDEXED
002100*    NOTICE THE RANDOM NEXT
002200        ACCESS MODE IS RANDOM
002300        RECORD KEY IS VSAM-KSDS-RECORD-KEY
002400        FILE STATUS  IS VSAM-STATUS-CODE
002500                        VSAM-EXTENDED-STATUS-CODE.
002600 DATA DIVISION.
002700 FILE SECTION.
002800 FD  IN-FILE
002900     RECORDING MODE IS F
003000     RECORD CONTAINS 80 CHARACTERS.
003100 01  IN-RECORD.
003200*     THE NEXT WILL BE USED AS THE KEY TO SEARCH IN THE VSAM FILE
003300*     IT HAS TO HAVE THE SAME PICTURE AS THE VSAM RECORD KEY
003400     05  EMPLOYEE-NAME            PIC X(20).
003500     05  FILLER                   PIC X(60).
003600
003700 FD  VSAM-KSDS-FILE
003800     RECORD CONTAINS 80 CHARACTERS.
003900 01  VSAM-KSDS-RECORD.
004000     05  VSAM-KSDS-RECORD-KEY    PIC X(20).
004100     05  VSAM-KSDS-EMP-INFO     PIC X(60).
004300 WORKING-STORAGE SECTION.
004400 01 SWITCHES.
004500      05  FILE-AT-END     PIC X  VALUE 'N'.
004600
004700 01  VSAM-STATUS-CODE.
004800     05 VSAM-STATUS-CODE-BYTE1   PIC X.
004900     05 VSAM-STATUS-CODE-BYTE2   PIC X.
005000
005100 01  VSAM-EXTENDED-STATUS-CODE.
005200     05 VSAM-EXTENDED-RETURN-CODE   PIC S9(4) COMP.
005300     05 VSAM-EXTENDED-FUNCTION-CODE PIC S9(4) COMP.
005400     05 VSAM-EXTENDED-FEEDBACK-CODE PIC S9(4) COMP.
005500
005600 PROCEDURE DIVISION.
005700     PERFORM INITIALIZATION
005800     PERFORM PROCESS-ALL
005900         UNTIL FILE-AT-END = 'Y'
006000     PERFORM TERMINATION
006100     GOBACK.
006200
006300 INITIALIZATION.
006400     OPEN INPUT IN-FILE
006500     OPEN INPUT VSAM-KSDS-FILE
006600     IF VSAM-STATUS-CODE IS NOT EQUAL TO '00'
006700     THEN GO TO ERROR-EXIT
006800     END-IF
006900     PERFORM READ-REGULAR-FILE.
007000
007100 PROCESS-ALL.
007200     PERFORM RANDOM-READ
007300     PERFORM READ-REGULAR-FILE.
007400
007500 TERMINATION.
007600     CLOSE IN-FILE VSAM-KSDS-FILE.
007700
007800 READ-REGULAR-FILE.
007900     READ IN-FILE
008000         AT END MOVE 'Y' TO FILE-AT-END
008100     END-READ.
008300 RANDOM-READ.
008400*   EMPLOYEE-NAME COMES FROM THE REGULAR INPUT FILE
008500*   IT MUST BE MOVED TO THE RECORD KEY OF THE VSAM FILE
008600*   THE RECORD KEY IS WHAT VSAM SEARCHES FOR IN THE FILE
008700*    IF IT FINDS IT, IT RETRIEVES THAT ENTIRE RECORD,
008800*    IF NOT, THEN STATUS CODE 23
008900     MOVE EMPLOYEE-NAME TO VSAM-KSDS-RECORD-KEY
009000     READ VSAM-KSDS-FILE
009100*    NOTE: THERE IS NO AT END
009200     END-READ
009400     EVALUATE VSAM-STATUS-CODE
009500       WHEN '23'
009600                  DISPLAY 'NOT FOUND' EMPLOYEE-NAME
009700       WHEN '00'
009800*              ON A FOUND SITUATION IN REAL LIFE
009900*                  YOU WOULD DO MORE THAN JUST DISPLAY THE RECORD
010000                  DISPLAY 'FOUND' VSAM-KSDS-RECORD
010100       WHEN OTHER GO TO ERROR-EXIT
010200       END-EVALUATE.
010400 ERROR-EXIT.
010500*   SEE THE PROGRAM VSAMSEQ2 FOR VSAM STATUS CODES
010600*   AND EXTENDED STATUS CODES
010700*   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 EMPNAMES: (the next two lines are a column ruler) 1 2 3 4 5 6 123456789.123456789.123456789.123456789.123456789.123456789.12345678 BUD WEIZER yup, heís there PHIL HARMONIC ok L. A. VATOR ok too HERB GARDNER calling all green thumbs HAL A. PENO this isnít there (try Taco Bell) Here is sample JCL: //* vsamrnd2.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) /* //INFILE DD DSN=userid.COBBOOK.DATA(EMPSORTD),DISP=SHR //* //STEP1 EXEC PGM=VSAMRND2 //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 //EMPNAMES DD DSN=userid.COBBOOK.DATA(EMPNAMES),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 |