Reading n records from a file



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

Reading n records from a file

Postby umeshunni » Wed Jun 02, 2010 12:01 am

Hello,

I was given a case study which is as follows:

There is a KSDS file which has the Employee Name as the primary index (say for example).
There are records with Employee names ranging from A to Z. We have to read only the last 5 records from the file that has the first name starting with say ‘U’.

More precisely: Suppose the records from the KSDS file are as follows:
AEMP1…
AEMP2…
.
.
UEMP1…
UEMP2…
.
.
UEMP15…
.
.
ZEMP1
.
.
Here in the example, we have 15 records having the Primary field starting with ‘U’.
So, as per the requirement, we have to only read the records from “UEMP11” thru “UEMP15”.
Other facts about the file are we do not know the full key…moreover names starting with U are not the last set of records in the file…

As per my answer:
Since the “U” records are not the last set then moving high values and setting the browse operation to the end followed by READPREV 5 times will not serve the purpose.

Using Partial key technique and the START verb, reach out to the first record with initial as “U”. Perform READNEXT until all the records starting with “U” are read (by keeping a tab on the primary field). Now we will be on the next record greater than “U” and so by performing a READPREV we should be on the last record with “U” (UEMP15 in this case). Then performing the READPREV for 5 times we should be able to read the last 5 records as per the requirement.

Although this method sounds a bit tedious, I am pretty sure there should be some other simpler way! I would be glad if anyone could think of a better approach to achieve the desired results and share it here.

Let me know if any kind of clarification/information is required.

Thanks in advance! :)
Umesh
umeshunni
 
Posts: 2
Joined: Wed Nov 04, 2009 8:52 am
Has thanked: 0 time
Been thanked: 0 time

Re: Reading n records from a file

Postby Robert Sample » Wed Jun 02, 2010 12:11 am

I assume you're talking about CICS, although you did not specify:
Move HIGH-VALUES to the key.
Move 'U' to the first character of the key (reference modification works well for this, or an array redefinition will also do it).
Do the START
Do the READNEXT.
Do six READPREV to get the five records.

If you do a STARTBR followed by READPREV, the value in the key variable must match an existing key in the file.
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: Reading n records from a file

Postby umeshunni » Wed Jun 02, 2010 8:40 am

Robert, thanks for your prompt response! :)

So as per my understanding of your approach, since we are first moving HIGH-VALUES to the key and then the 'U' to the first character of the key, am I correct in saying that the START verb using the above formed key will point to the last 'U' record in the file. From the example that i had stated, it means the START will point to the "UEMP15" record!

And proceeding ahead with the READNEXT and READPREV it will fetch me the records:
UEMP15, UEMP14, UEMP13, UEMP12 and UEMP11 in the stated order.

Thanks once again,
Umesh
umeshunni
 
Posts: 2
Joined: Wed Nov 04, 2009 8:52 am
Has thanked: 0 time
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post