IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Reading file dynamically


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
suraaj

New User


Joined: 16 Apr 2009
Posts: 69
Location: Canada

PostPosted: Sat Sep 28, 2013 2:02 am
Reply with quote

I have a VSAM file which has a key and alternate key. I need to retrieve some data based on the alternate key from this file. The alternate key is with duplicates. Once I have checked for the value I need to get another value from the record which satisfies the earlier condition.

Code:
SELECT VSAM-FILE  ASSIGN TO  DA-VSAM
                           ORGANIZATION IS INDEXED
                           ACCESS MODE  IS DYNAMIC
                           RECORD KEY   IS VSAM-NUMBER
                                  OF    VSAM-REC
                           ALTERNATE RECORD KEY IS
                                  VSAM-ID
                                  OF    VSAM-REC
                                  WITH DUPLICATES
                                  FILE STATUS IS IO-STATUS-CODE
                                  VSAM-EXTENDED-STATUS-CODE.


Code:
MOVE ID-INPUT TO VSAM-ID OF VSAM-REC
READ VSAM-FILE
PERFORM UNTIL FOUND = 'Y'
  READ VSAM-FILE NEXT   INTO WS-VSAM-REC
    IF TYPE = '1'
      MOVE FIRST-NAME TO WS-FIRST-NAME
      MOVE 'Y' TO FOUND.
    END-IF
  END-READ


Kindly advise if this the correct way of doing it.

Regards Suraaj
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Sat Sep 28, 2013 4:12 am
Reply with quote

When doing a keyed read, I think it best to always code the key on the read. Makes it cleaer for the next person along.

You have FILE STATUS, but you don't check it after your READ,

You need to know you have a record for the key.

When doing the read next, you need to be aware that the key may change. You don't want to find the correct record-type but for the wrong key.

Write down some sample data with a few keys and which cover your main data possibilities. Use this to go through yout code. Use the FILE STATUS values, and find out exactly which statuses are valid and invalid for your situation.
Back to top
View user's profile Send private message
suraaj

New User


Joined: 16 Apr 2009
Posts: 69
Location: Canada

PostPosted: Mon Sep 30, 2013 8:26 pm
Reply with quote

Hi Bill,

Thanks for the reply...

I changed my code to check the file status. I executed the code and now I find that although the open is successful with status = 0 the random read is returning status=23 (even for sequential read I am getting the same status), which means the record is not found with the alternate key. I specified the read statement as below:

Code:
READ VSAM-FILE KEY IS VSAM-ID OF VSAM-REC


When I check manually I find that the record with the alternate key is present in the VSAM. Not sure if I need to specify something other than this.Kindly help

Regards Suraaj
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Mon Sep 30, 2013 8:59 pm
Reply with quote

What do your VSAM and VSAM1 DD statements look like?
Back to top
View user's profile Send private message
suraaj

New User


Joined: 16 Apr 2009
Posts: 69
Location: Canada

PostPosted: Mon Sep 30, 2013 9:27 pm
Reply with quote

Robert,

Code:

VSAM
VSAM1


Regards Suraaj
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1788
Location: Bloomington, IL

PostPosted: Mon Sep 30, 2013 9:45 pm
Reply with quote

suraaj wrote:
Robert,

Code:

VSAM
VSAM1

You recognize that those aren't even approximately DD statements, yes?
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Mon Sep 30, 2013 9:48 pm
Reply with quote

Hello,

If you had been asked for DDNAMEs, those might have been ok . . .

Why did you not post the entire DD statement for both? The longer it takes to drag info from you, the longer it will be for us to help.
Back to top
View user's profile Send private message
suraaj

New User


Joined: 16 Apr 2009
Posts: 69
Location: Canada

PostPosted: Mon Sep 30, 2013 9:56 pm
Reply with quote

Code:
//VSAM   DD DSN=TABB.CBPP.VSAM.A999CIC,DISP=SHR
//VSAM1 DD DSN=TABB.CBPP.VSAM.A999CIC.PATH,
//             DISP=SHR


Apologize for the incomplete answer.

Regards Suraaj
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Mon Sep 30, 2013 10:58 pm
Reply with quote

The Enterprise COBOL 3.4 Programming Guide manual has this to say in 1.10.3.3 Reading records from a VSAM file:
Quote:



When you want to read sequentially, beginning at a specific record, use START before the READ NEXT statement to set the file position indicator to point to a particular record. When you code START followed by READ NEXT, the next record is read and the file position indicator is reset to the next record. You can move the file position indicator randomly by using START, but all reading is done sequentially from that point.

START file-name KEY IS EQUAL TO ALTERNATE-RECORD-KEY


When a direct READ is performed for a VSAM indexed file, based on an alternate index for which duplicates exist, only the first record in the data set (base cluster) with that alternate key value is retrieved. You need a series of READ NEXT statements to retrieve each of the data set
records with the same alternate key. A file status code of 02 is returned if there are more records with the same alternate key value to be read; a code of 00 is returned when the last record with that key value has been read.
The code you posted has no START statement in it, and the manual quote does not indicate that a direct READ (especially one without a KEY IS phrase which IIRC means the primary key will be used instead of the alternate index key) would be equivalent to the START.

And if you want to attempt to be funny in the future, let us know in advance so we won't waste our time by asking you for details and getting garbage back from you, or by helping you at all.
Back to top
View user's profile Send private message
suraaj

New User


Joined: 16 Apr 2009
Posts: 69
Location: Canada

PostPosted: Mon Sep 30, 2013 11:22 pm
Reply with quote

Robert,

In my code that I had given earlier I had mentioned two read statements. One for the direct read (I haven't mentioned the alternate key) and then read next in a perform loop so that I can read the subsequent records for the duplicates of the alternate key to check for the type. I had done all these after I had read the manual. I suppose doing a random read first and then a couple of sequential reads is the same as doing start and read next. And when I come to the forum I make sure that I have gone through all the available resources. I tried to do a random read with primary key, just to check if any read actually works, and it worked and fetched the record from the VSAM. Again I apologize for the incomplete details from my side.

Regards Suraaj
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Tue Oct 01, 2013 12:59 am
Reply with quote

Common data:
Code:
       SELECT VSAM-IN             ASSIGN TO DA-VSAM
                                  ORGANIZATION INDEXED
                                  ACCESS MODE DYNAMIC
                                  RECORD KEY IS VI-KEY
                                  ALTERNATE RECORD KEY IS VI-ALT
                                      WITH DUPLICATES
                                  FILE STATUS IS WS-FS.
  DATA DIVISION.
  FILE SECTION.
  FD  VSAM-IN.
  01  VSAM-IN-RECORD.
      05  VI-KEY                  PIC X(08).
      05  VI-ALT                  PIC X(04).
      05  VI-DATA                 PIC X(68).

  WORKING-STORAGE SECTION.
  01  WS-FS                       PIC 9(02) VALUE ZERO.
Code with START:
Code:
      OPEN INPUT VSAM-IN.
      DISPLAY ' OPEN FS = ' WS-FS.
      MOVE 'FFFFFFFA'            TO  VI-KEY.
      START VSAM-IN
          KEY IS NOT LESS THAN VI-KEY.
      DISPLAY 'START FS = ' WS-FS.
      READ VSAM-IN NEXT.
      DISPLAY 'READN FS = ' WS-FS
              ' RECORD = ' VSAM-IN-RECORD
      PERFORM
          UNTIL VI-ALT NOT = 'DDDD'
             OR WS-FS > 00
          READ VSAM-IN NEXT
          DISPLAY 'READN FS = ' WS-FS
                  ' RECORD = ' VSAM-IN-RECORD
      END-PERFORM.
      MOVE 'AAAA'                TO  VI-ALT.
      START VSAM-IN
          KEY IS NOT LESS THAN VI-ALT.
      DISPLAY 'START FS = ' WS-FS.
      READ VSAM-IN NEXT.
      DISPLAY 'READN FS = ' WS-FS
              ' RECORD = ' VSAM-IN-RECORD
      PERFORM
          UNTIL VI-ALT NOT = 'AAAA'
             OR WS-FS > 00
          READ VSAM-IN NEXT
          DISPLAY 'READN FS = ' WS-FS
                  ' RECORD = ' VSAM-IN-RECORD
      END-PERFORM.
      CLOSE VSAM-IN.
      STOP RUN.
produced output of
Code:
  OPEN FS = 00
 START FS = 00
 READN FS = 00 RECORD = FFFFFFFADDDD
 READN FS = 00 RECORD = FFFFFFFBDDDD
 READN FS = 00 RECORD = FFFFFFFCDDDD
 READN FS = 00 RECORD = FFFFFFFDDDDD
 READN FS = 00 RECORD = FFFFFFFEDDDD
 READN FS = 00 RECORD = FFFFFFFFDDDD
 READN FS = 00 RECORD = FFFFFFFGDDDD
 READN FS = 00 RECORD = FFFFFFFHDDDD
 READN FS = 00 RECORD = FFFFFFFIDDDD
 READN FS = 00 RECORD = FFFFFFFJDDDD
 READN FS = 00 RECORD = FFFFFFFKDDDD
 READN FS = 00 RECORD = FFFFFFFLDDDD
 READN FS = 00 RECORD = FFFFFFFMDDDD
 READN FS = 00 RECORD = FFFFFFFNDDDD
 READN FS = 00 RECORD = FFFFFFFODDDD
 READN FS = 10 RECORD = FFFFFFFODDDD
 START FS = 00
 READN FS = 00 RECORD = BBBBBBBBAAAA
 READN FS = 00 RECORD = CCCCCCCCAAAA
 READN FS = 00 RECORD = DDDDDDDDAAAA
 READN FS = 00 RECORD = DDDDDDDEBBBB
whereas code with the READ
Code:
      OPEN INPUT VSAM-IN.
      DISPLAY ' OPEN FS = ' WS-FS.
      MOVE 'FFFFFFFA'            TO  VI-KEY.
      READ VSAM-IN
          KEY IS VI-KEY.
      DISPLAY ' READ FS = ' WS-FS.
      READ VSAM-IN NEXT.
      DISPLAY 'READN FS = ' WS-FS
              ' RECORD = ' VSAM-IN-RECORD
      PERFORM
          UNTIL VI-ALT NOT = 'DDDD'
             OR WS-FS > 00
          READ VSAM-IN NEXT
          DISPLAY 'READN FS = ' WS-FS
                  ' RECORD = ' VSAM-IN-RECORD
      END-PERFORM.
      MOVE 'AAAA'                TO  VI-ALT.
      READ VSAM-IN
          KEY IS VI-ALT.
      DISPLAY ' READ FS = ' WS-FS.
      READ VSAM-IN NEXT.
      DISPLAY 'READN FS = ' WS-FS
              ' RECORD = ' VSAM-IN-RECORD
      PERFORM
          UNTIL VI-ALT NOT = 'AAAA'
             OR WS-FS > 00
          READ VSAM-IN NEXT
          DISPLAY 'READN FS = ' WS-FS
                  ' RECORD = ' VSAM-IN-RECORD
      END-PERFORM.
      CLOSE VSAM-IN.
      STOP RUN.
produces this output:
Code:
  OPEN FS = 00
  READ FS = 00
 READN FS = 00 RECORD = FFFFFFFBDDDD
 READN FS = 00 RECORD = FFFFFFFCDDDD
 READN FS = 00 RECORD = FFFFFFFDDDDD
 READN FS = 00 RECORD = FFFFFFFEDDDD
 READN FS = 00 RECORD = FFFFFFFFDDDD
 READN FS = 00 RECORD = FFFFFFFGDDDD
 READN FS = 00 RECORD = FFFFFFFHDDDD
 READN FS = 00 RECORD = FFFFFFFIDDDD
 READN FS = 00 RECORD = FFFFFFFJDDDD
 READN FS = 00 RECORD = FFFFFFFKDDDD
 READN FS = 00 RECORD = FFFFFFFLDDDD
 READN FS = 00 RECORD = FFFFFFFMDDDD
 READN FS = 00 RECORD = FFFFFFFNDDDD
 READN FS = 00 RECORD = FFFFFFFODDDD
 READN FS = 10 RECORD = FFFFFFFODDDD
  READ FS = 23
 READN FS = 46 RECORD = FFFFFFFOAAAA
So I think you would be wise to change your code to use START instead of attempting a direct READ.
Back to top
View user's profile Send private message
suraaj

New User


Joined: 16 Apr 2009
Posts: 69
Location: Canada

PostPosted: Tue Oct 01, 2013 2:37 am
Reply with quote

Hi Robert,

The code worked. I was all the time using the statement as:
Code:
START VSAM-FILE KEY IS EQUAL TO VSAM-ID


But when I changed it to :

Code:
START VSAM-FILE KEY IS NOT LESS THAN VSAM-ID


It worked.

Thanks a ton Robert.

Regards Suraaj
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Tue Oct 01, 2013 3:11 pm
Reply with quote

Glad to hear you got it working.
Back to top
View user's profile Send private message
suraaj

New User


Joined: 16 Apr 2009
Posts: 69
Location: Canada

PostPosted: Tue Oct 01, 2013 6:00 pm
Reply with quote

Why does it not work with KEY IS EQUAL TO and only work for KEY IS NOT LESS THAN. Please explain.

Regards Suraaj
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts FTP VB File from Mainframe retaining ... JCL & VSAM 4
No new posts Extract the file name from another fi... DFSORT/ICETOOL 6
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
Search our Forums:

Back to Top