VSAM FILE HANDLING



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

VSAM FILE HANDLING

Postby Santhya » Thu Oct 20, 2011 8:33 pm

This is my scenario:

I have 2 VSAM files(KSDS).One contains the Policy details and the other contains the customer details, BOTH HAVING THE POLICY NUMBER AS KEY. I need to compare the 2 files and write the matching records into another VSAM file.

How can i achieve it?
I tried this code. but dunno if it would work.

Please help me.

IDENTIFICATION DIVISION
PROGRAM-ID.  POLICY.   
DATE-WRITTEN. OCT 2011.
                                                 
***************END OF IDENTIFICATION DIVISION****
                                                 
 ENVIRONMENT DIVISION.                           
                                         
CONFIGURATION SECTION.                   
INPUT-OUTPUT SECTION.                     
FILE-CONTROL.                             
    SELECT VSAM-INP-FILE1 ASSIGN TO VSAM1.
ORGANIZATION IS INDEXED               
ACCESS IS SEQUENTIAL                 
RECORD KEY IS POL-NUM                 
FILE STATUS IS KEY-STATUS1.           
                                     
SELECT VSAM-INP-FILE2 ASSIGN TO VSAM2.
ORGANIZATION IS INDEXED               
ACCESS IS RANDOM                     
RECORD KEY IS KEY-POL-NUM             
FILE STATUS IS KEY-STATUS2.           
                                     
SELECT OUT-FILE ASSIGN TO VSAM3.     
ORGANIZATION IS INDEXED               
ACCESS IS RANDOM                     
RECORD KEY IS O-KEY-POL-NUM           
FILE STATUS IS KEY-STATUS3.           
************END OF ENVIRONMENT DIVISION***************

DATA DIVISION.                     
FILE SECTION.                       
                                   
FD VSAM-INP-FILE1.                 
01 POLICY-DET-REC.                 
    05 POL-NUM          PIC 9(10). 
    05 START-DATE       PIC 9(6).   
    05 END-DATE         PIC 9(6).   
    05 AMOUNT           PIC 9(6)V99.
                                   
FD VSAM-INP-FILE2.                 
01 CUST-DET-REC.                   
    05 KEY-POL-NUM      PIC 9(10). 
    05 FIRST-NAME       PIC X(20). 
    05 LAST-NAME        PIC X(20). 
    05 CUST-ADDR        PIC X(20). 
                                   
FD OUTPUT-FILE.                     
01 OUTPUT-REC.                     
    05 O-KEY-POL-NUM    PIC 9(10). 
    05 O-FIRST-NAME     PIC X(20). 
    05 O-LAST-NAME      PIC X(20). 
    05 O-START-DATE     PIC 9(6).   
    05 O-END-DATE       PIC 9(6).   
    05 O-AMOUNT         PIC 9(6)V99.
    05 O-CUST-ADDR      PIC  X(20).
                                   
***************END-OF-DATA DIVISION*
                                   
WORKING-STORAGE SECTION.           
                                   
01 WS-VSAM-INP-FILE1.               
    05 WS-POL-NUM       PIC 9(10). 
    05 WS-START-DATE    PIC 9(6).   
    05 WS-END-DATE      PIC 9(6).   
    05 WS-AMOUNT        PIC 9(6)V99.
                                   
01 WS-END-REC.                     
    88 END-REC VALUE HIGH-VALUES. 
                                   
01 WS-VSAM-INP-FILE2.             
    05 WS-KEY-POL-NUM   PIC 9(10).
    05 WS-FIRST-NAME    PIC X(20).
    05 WS-LAST-NAME     PIC X(20).
    05 WS-CUST-ADDR     PIC X(20).
                                   
01 WS-STATUS-KEY.                 
    05 KEY-STATUS1       PIC X(2).
    05 KEY-STATUS2       PIC X(2).
                                   
01 WS-OUTPUT-REC.                 
    05 WS-O-KEY-POL-NUM PIC 9(10).
    05 WS-O-FIRST-NAME  PIC X(20).
    05 WS-O-LAST-NAME   PIC X(20).
    05 WS-O-START-DATE  PIC 9(6). 
    05 WS-O-END-DATE    PIC 9(6). 
    05 WS-O-AMOUNT      PIC 9(6)V99
    05 WS-O-CUST-ADDR   PIC X(20).
**********END-OF WORKING STORAGE SECTION*
                                         
PROCEDURE DIVISION.                     
                                         
*****************************************
 OBTAIN THE INPUT VALUES AND OPEN THE FIL
 CHECK THE POLICY NUMBER AND IF IT MATCHE
 FILE                                   
*****************************************
                                         
    PERFORM 100-INITIALIZATION           
       THRU 100-EXIT.                   
                                         
    PERFORM 200-PROCESS                 
       THRU 200-EXIT UNTIL END-REC.     
                                         
    PERFORM 300-TERMINATE               
       THRU 300-EXIT.                   
      STOP RUN.                                 
                                               
 100-INITIALIZATION.                           
                                               
     OPEN INPUT VSAM-INP-FILE1.                 
     OPEN INPUT VSAM-INP-FILE2.                 
     OPEN OUTPUT OUTPUT-FILE.                   
                                               
 100-EXIT.                                     
     EXIT.                                     
                                               
 200-PROCESS.                                   
     READ VSAM-INP-FILE1                       
       AT END MOVE END-REC TO TRUE.             
                                               
     IF KEY-STATUS1 IS '00' OR '97'             
                                               
     MOVE POL-NUM         TO WS-POL-NUM         
     MOVE START-DATE      TO WS-START-DATE     
MOVE END-DATE        TO WS-END-DATE   
MOVE AMOUNT          TO WS-AMOUNT     
                                       
MOVE WS-POL-NUM      TO WS-KEY-POL-NUM
                                       
READ VSAM-INP-FILE2                   
                                       
MOVE KEY-POL-NUM     TO WS-KEY-POL-NUM
MOVE FIRST-NAME      TO WS-FIRST-NAME 
MOVE LAST-NAME       TO WS-LAST-NAME   
MOVE CUST-ADDR       TO WS-CUST-ADDR   
                                       
IF KEY-STATUS2 IS '00' OR '97'         
DISPLAY "POLICY IS PRESENT"           
                                       
PERFORM 210-POLICY-PRESENT             
   THRU 210-POLICY-EXIT               
                                       
ELSE                                   
DISPLAY "POLICY IS NOT PRESENT"       
     PERFORM 220-POLICY-NOT-PRESENT           
        THRU 220-POLICY-EXIT                 
     END-IF.                                 
                                             
     WRITE OUTPUT-FILE FROM WS-OUT-REC.       
                                             
     ELSE                                     
     DISPLAY "ERROR WHILE READING INPUT FILE1"
                                             
     END-IF.                                 
                                             
 200-EXIT.                                   
     EXIT.                                   
                                             
 300-TERMINATE.                               
                                             
     CLOSE VSAM-INP-FILE1.                   
     CLOSE VSAM-INP-FILE2.                   
     CLOSE OUTPUT-FILE.                       
 300-EXIT.                                           
     EXIT.                                           
                                                     
 210-POLICY-PRESENT.                                 
         MOVE WS-KEY-POL-NUM     TO WS-O-KEY-POL-NUM.
         MOVE WS-FIRST-NAME      TO WS-O-FIRST-NAME. 
         MOVE WS-LAST-NAME       TO WS-O-LAST-NAME.   
         MOVE WS-START-DATE      TO WS-O-START-DATE. 
         MOVE WS-END-DATE        TO WS-O-END-DATE.   
         MOVE WS-AMOUNT          TO WS-O-AMOUNT.     
         MOVE WS-CUST-ADDR       TO WS-O-CUST-ADDR.   
                                                     
 210-POLICY-EXIT.                                     
     EXIT.                                           
                                                     
 220-POLICY-NOT-PRESENT.                             
         MOVE WS-KEY-POL-NUM     TO WS-O-KEY-POL-NUM.
         MOVE SPACES             TO WS-O-FIRST-NAME. 
         MOVE SPACES             TO WS-O-LAST-NAME.   
        MOVE WS-START-DATE      TO WS-O-START-DATE.
        MOVE WS-END-DATE        TO WS-O-END-DATE..
        MOVE WS-AMOUNT          TO WS-O-AMOUNT.   
        MOVE SPACES             TO WS-O-CUST-ADDR.
                                                   
220-POLICY-EXIT.                                   
    EXIT.     
Regards,
Santhya
Santhya
 
Posts: 7
Joined: Tue Oct 04, 2011 12:20 pm
Has thanked: 0 time
Been thanked: 0 time

Re: VSAM FILE HANDLING

Postby Robert Sample » Thu Oct 20, 2011 8:51 pm

How can i achieve it?
I tried this code. but dunno if it would work.
1) If YOU don't know if the code will work, why not run it to find out? Why ask us?

2) You've got a simple two file match process here -- well understood in the industry and there are lots of solutions (COBOL ones, even) available if you exert yourself a little and look.

3) Posting that something did not work is useless -- tell us if it had a system abend, or a user abend, or did not abend but did not produce expected results, or whatever. And if you are expecting certain results you didn't get, post what you expected and what you actually got.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: VSAM FILE HANDLING

Postby BillyBoyo » Fri Oct 21, 2011 4:06 am

I don't think you've tried to compile your program.

It doesn't have "two-file-matching" logic in it, so, no, it won't work.

If you look in the Cobol forum here, at the top, you'll find an example program that should help you out a lot.

You should look at how to define your VSAM files for serial processing.

If you get stuck, come back and explain how you are stuck. Do you have access to a system to compile/run on?
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: VSAM FILE HANDLING

Postby Santhya » Fri Oct 21, 2011 11:21 am

Robet/BillyBoyo: i have applied for COMPILE/RUN access and waiting for it to get approved.So i was not sure if it would work. thanx for the reply.
Regards,
Santhya
Santhya
 
Posts: 7
Joined: Tue Oct 04, 2011 12:20 pm
Has thanked: 0 time
Been thanked: 0 time

Re: VSAM FILE HANDLING

Postby BillyBoyo » Fri Oct 21, 2011 1:06 pm

Check your SELECT statements, they have an extra full-stop/period each. You have an ELSE without an IF.

You should check your file status for every IO operation, so for OPEN and CLOSE, READ and WRITE. What is file status 97 on a READ?

Get Dick's example while you are waiting, understand the logic of it and then apply the matching logic to your program.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: VSAM FILE HANDLING

Postby Santhya » Tue Oct 25, 2011 12:26 pm

Hi Billy,
I corrected the errors for the period and the ELSE. I included the 97 status to check if proper opeing has taken place.
I checked Dick's example. I tried working it with 2-ps files.
I have an idea to establish the same in VSAM to check if the records match. Please correct if am wrong.
-> Declare one with sequential access and another with Random access.
-> Read the 1st file (seq)
-> As both have policy number as key, move the key to the key variable of 2nd (random) and then try to read it. if it had the matching policy, read will be successful else it will not be able to read it. As we have declared the 2nd file as random, it will directly hit the respective policy and as 1st file is seq, it will perform seq read till EOF. Thereby, all records will be read and matching records will be found.
Regards,
Santhya
Santhya
 
Posts: 7
Joined: Tue Oct 04, 2011 12:20 pm
Has thanked: 0 time
Been thanked: 0 time

Re: VSAM FILE HANDLING

Postby BillyBoyo » Tue Oct 25, 2011 1:43 pm

A qualified "yes". If your requirement is to find mismatches off either file, then it will not work.

The other thing is the "performance". It is generally slower to read records on a key than to read them serially. Depending on how many records you have on the driving file, you will notice this or not. Not many records, won't make much difference. A lot of records, a noticeable difference.

If either of these two things would present you with a problem fulfilling the requirement, then definitely go for the two-file-match.

Since both you files are keyed, you don't have duplicate keys for your match. If you are experimenting/learning then do a follow-on sometime with duplicate keys, first on one file, then the other, then both.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: VSAM FILE HANDLING

Postby Santhya » Fri Oct 28, 2011 6:58 pm

Hi Billy,
Got compile and run access.
I tried to work with the above logic. I got an error while opening the 3rd file(which is also VSAM) and thus could'nt proceed further with the execution. The return code is 37. How to overcome this?
Regards,
Santhya
Santhya
 
Posts: 7
Joined: Tue Oct 04, 2011 12:20 pm
Has thanked: 0 time
Been thanked: 0 time

Re: VSAM FILE HANDLING

Postby BillyBoyo » Fri Oct 28, 2011 7:30 pm

Well, it is manual time for you. Try this, http://publib.boulder.ibm.com/cgi-bin/bookmgr/BOOKS/igy3lr00/6.1.8.9.1?SHELF=igy3sh00&DT=20011206182158#HDRSTATKEY, first. Look down the left column for your 3, then the next column for your 7. Review you type of OPEN and the SELECT. You might also find more messages in the job output relating to your problem, look those up as well. Usually poking the message number into google will give you a start with that. There is also IBM's LOOKAT facility, if you google for that.

If you are still stuck, post your Select, FD, Open and the associated messages and we can give some help. Even if I could tell you exactly what is wrong from what you have given, it is still better that you do the searching yourself as you'll find it much easier to remember/know how to do it in the future.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: VSAM FILE HANDLING

Postby dick scherrer » Fri Oct 28, 2011 8:49 pm

Hi Bill,

Well, this user (even after several replies) decided to post this exact same question on another forum :(

If there was not such a dialog here, i'd have deleted this topic.

d
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post