Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Reading file dynamically

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
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    Post subject: Reading file dynamically
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

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7223

PostPosted: Sat Sep 28, 2013 4:12 am    Post subject: Reply to: Reading file dynamically
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    Post subject: Reply to: Reading file dynamically
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: 7905
Location: Bellevue, IA

PostPosted: Mon Sep 30, 2013 8:59 pm    Post subject:
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    Post subject: Reply to: Reading file dynamically
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: 1738
Location: Bloomington, IL

PostPosted: Mon Sep 30, 2013 9:45 pm    Post subject: Re: Reply to: Reading file dynamically
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

Site Director


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

PostPosted: Mon Sep 30, 2013 9:48 pm    Post subject:
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    Post subject: Reply to: Reading file dynamically
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: 7905
Location: Bellevue, IA

PostPosted: Mon Sep 30, 2013 10:58 pm    Post subject:
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    Post subject: Reply to: Reading file dynamically
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: 7905
Location: Bellevue, IA

PostPosted: Tue Oct 01, 2013 12:59 am    Post subject:
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    Post subject: Reply to: Reading file dynamically
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: 7905
Location: Bellevue, IA

PostPosted: Tue Oct 01, 2013 3:11 pm    Post subject:
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    Post subject: Reply to: Reading file dynamically
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Add PD field from 2nd file to PD in 1st Sushant Garje DFSORT/ICETOOL 6 Thu Dec 01, 2016 4:32 pm
No new posts File Aid to File Manager conversion murali3955 IBM Tools 4 Thu Nov 24, 2016 3:41 pm
No new posts How to convert the VBM file to VB or... Sulabh Agrawal JCL & VSAM 4 Fri Nov 18, 2016 1:04 pm
No new posts CICS Roll back partially - Need to re... dwijadas CICS 4 Wed Nov 16, 2016 4:30 pm
No new posts Problem in writing Output file vickey_dw COBOL Programming 5 Mon Nov 14, 2016 11:14 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us