Mainframes 360
The one stop destination for System Z professionals

Wednesday, February 23, 2011

Get Next CALL – Sequential Retrieval of Data

Q. What is meant by Hierarchical Sequential Order?
An IMS Database has a sequence of records. The sample Inventory-Database that we've been working with, is loaded with the following data-records.

Scanning all the records in an IMS Database, and traversing through the entire database is done in a peculiar order. This order is called Hierarchical-Sequential Order or Pre-order. The Hierarchical-Sequential method of reading is like reading the Chinese language. You read it in a top-to-bottom, left-to-right fashion. The dotted arrows in blue, show the direction of traversal. The list of the Data-Items in Hierarchical-Sequence in Inventory-Database is shown below..

Q. What happens when you issue an IMS-DB GN(Get Next) CALL?
When you issue a single Get-Next GN Call, how to determine, which segment-occurrence is fetched from IMS Database? To be able to answer this, you need to understand the
Database-Position. At all times, a marker or pointer is maintained in the IMS Database, that is set to your current-position in the Database. This current Database-Position serves as a reference-point, as a base-line for the next IMS-DB Call.

Let's assume, that your position in the Database, is at the STLOCSEG Occurrence [MUM 1000 200 400 230510]. Observe the Marker or Pointer, in the IMS Database in the picture below. 

After the COBOL Program issues an IMS-DB Get-Next GN Call, the marker or Pointer in the IMS Database is incremented, it moves one position forward in the database, from its reference-point. As a result, the Marker or Pointer advances to the next occurrence [00012 METHYL ORANGE 17255.00 3230.00]  in hierarchical sequential order.

This occurrence [00012 METHYL ORANGE 17255.00 3230.00] is the result of the execution of the Get-Next GN Call, and will now be returned back as result to the CALL'er COBOL Program. 

Q. How do I use the GN Calls to scan through the IMS Database? How to detect the end-of-database?
You can use the Get-Next GN Calls to retrieve the Segment-Occurrences sequentially. To begin with, the pointer or marker in the IMS Database points to NULL. 

When you issue the first IMS-DB Get-Next Call, the marker or pointer advances one position ahead to the segment-occurrence [001 INDUS CHEMICALS ...], and this segment-occurrence is fetched.


When you issue the second IMS-DB Get-Next Call, the marker or pointer advances one position ahead to the Segment-Occurrence [00011 SODIUM PALMITATE ...], and this segment-occurrence is fetched.


Likewise, when you issue subsequent IMS-DB Get-Next Calls, each time the marker or pointer in the IMS Database advances one step forward from its previous position, and the corresponding segment-occurrence is fetched. Counting in this fashion, how many IMS-DB Get-Next Calls is it gonna take to reach the end? How to detect the end of the database? The Program should be smart enough to figure out this, and halt processing, and GO BACK. Let's find out, how its done!

Let's fast-forward to the twelfth IMS-DB  Get-Next Call. The Marker or Pointer in the IMS Database, advances from its previous position [KOL 3600 550 ...] one step ahead and gets fixed at [00022 TINCTURE IODINE].


When you issue the thirteenth IMS-DB Get-Next Call, the marker or the pointer in the IMS Database, moves one step ahead. But, there are no more segment-occurrences left to go to. The marker or pointer, has gone past the last segment-occurrence. This special condition is called End-Of-Database. The IMS-DB Software sets a Two-digit Status Code = 'GB' in the PCB Control Block to indicate to the COBOL Program, the end of the Database.

Q. Could you illustrate a simple COBOL Program to retrieve data sequentially from the Inventory Database?
The algorithm to retrieve data sequentially from an IMS Database is quite simple. You run a loop, and keep issuing IMS-DB Get-Next Calls to fetch the segment-occurrences one-by-one, until IMS-DB sets a Status-Code='GB'.


How do you code the above algorithm in COBOL? First of all, on execution of the GN Call, the IMS-DB Software sets a two-digit status code in the PCB Block, indicating the success or failure of the Operation. The IPCB-STATUS-CODE COBOL Field stores this IMS-DB Status Code. I am going to run a Loop to fetch the segment-occurrences one-by-one from the database, until the IPCB-STATUS-CODE Field equals 'GB', and no more segment occurrences are left.

The data-result which is fetched from the IMS-Database, on execution of the GN Call, is stored in the Segment Input-Output Area. I would like to DISPLAY the data-results on the Mainframe Terminal, so I coded a DISPLAY SEGMENT-IO-AREA in my COBOL Source Program.


For those of you, who would like to try out and implement this Program at home, here is the complete Source Code of my COBOL Program, that retrieves the data sequentially from the IMS-Database and displays it on the screen.

To many people who are thrown to work at a mainframe computer on their first job, they feel lost. Mainframe people seem to speak a completely different language and that doesn't make life easy. What's more, the books and manuals are incredibly hard to comprehend.

"What on earth is a Mainframe?" is an absolute beginner's guide to mainframe computers. We'll introduce you to the hardware and peripherals. We'll talk about the operating system, the software installed on a mainframe. We'll also talk about the different people who work on a mainframe. In a nutshell, we'll de-mystify the mainframe.

Readers based in India, can buy the e-book for Rs. 50 only or the print book. International readers based in the US and other countries can click here to purchase the e-book.