Previous Topic: Accessing a Record by Its db-keyNext Topic: Saving db-key, Page Information, and Bind Addresses


Accessing Indexed Records

Indexes provide an efficient means of accessing member record occurrences. You can retrieve member records in indexed sets as if they were member records in nonindexed sets.

The table below lists the retrieval statements that you can use with indexed records.

Retrieval statement

Restrictions

FIND/OBTAIN CURRENT

WITHIN SET

FIND/OBTAIN RECORD

WITHIN SET

FIND/OBTAIN USING SORT KEY

Sorted indexed sets only

FIND/OBTAIN OWNER

OBTAIN not allowed for system-owned indexes

RETURN

Db-key and symbolic key only

Example of Accessing an Indexed Record

The program excerpt below shows retrieval of all records in the EMP-NAME-NDX (a system-owned indexed set).

The EMP-NAME-NDX set is sorted in ascending order on EMP-LAST-NAME and EMP-FIRST-NAME; this program produces an alphabetical list of all employees.

 PROCEDURE DIVISION.
 A000-MAIN-LINE.
 .
 .
 .
        MOVE 'Y' to FIRST-TIME-SW.
        PERFORM A000-GET-NDX-SET THRU A000-EXIT
             UNTIL DB-END-OF-SET.
        PERFORM END-PROCESSING.
        GOBACK.

 A000-GET-NDX-SET.
*** SEQUENTIALLY RETRIEVE EMPLOYEES INDEXED BY LAST NAME ***
     IF FIRST-TIME-SW = 'Y'
        MOVE 'N' TO FIRST-TIME-SW
        OBTAIN FIRST EMPLOYEE WITHIN EMP-NAME-NDX
     ELSE
        OBTAIN NEXT EMPLOYEE WITHIN EMP-NAME-NDX.
*** CHECK FOR ERROR-STATUS = 0307 ***
     IF DB-END-OF-SET
        GO TO A000-EXIT
*** CHECK FOR ERROR-STATUS = 0000 ***
     ELSE IF DB-STATUS-OK
        NEXT SENTENCE
     ELSE
        PERFORM IDMS-STATUS.
     DISPLAY EMP-ID-0415
             EMP-LAST-NAME-0415
             EMP-FIRST-NAME-0415.
 A000-EXIT.
     EXIT.

Retrieving the Key without the Record

To retrieve the db-key and symbolic key of an indexed record without retrieving the record itself, perform the following steps:

  1. Initialize variable storage fields, as required.
  2. Issue the RETURN statement.
  3. If you are issuing the RETURN statement iteratively, check for an ERROR-STATUS of 1707; if you are doing a keyed search, check for an ERROR-STATUS of 1726.
  4. Perform the IDMS-STATUS routine if 1726, 1707, or 0000 is not returned.

Using the RETURN Statement

The RETURN statement establishes currency in the indexed set and moves the record's symbolic key into the data fields within the record in program variable storage. Alternatively, you can move the record's symbolic key into some other specified variable-storage location.

Example of Using RETURN

The program excerpt below uses the RETURN statement to establish indexed set currency.

This program establishes currency in the EMP-NAME-NDX set by using the RETURN statement to perform a generic-key search. It checks for the ERROR-STATUS 1726 (record not found), and retrieves all employees whose last name begins with the letter N or greater.

 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01  INDEX-ITEMS.
     03 DB-KEY-V          PIC S9(8) COMP SYNC.
     03 INDEX-START-POINT PIC X(30)   VALUE 'N'.
 PROCEDURE DIVISION.
 A000-MAIN-LINE.
 .
 .
 .
     MOVE INDEX-START-POINT TO INDEX-KEY-VALUE.
     RETURN DB-KEY-V FROM EMP-NAME-NDX
           USING INDEX-START-POINT.
*** IF NO MATCHING EMPLOYEE, OBTAIN NEXT IN SET ***
     IF ERROR-STATUS = '1726' THEN
        OBTAIN NEXT EMPLOYEE WITHIN EMP-NAME-NDX.
     IF DB-STATUS-OK
        NEXT SENTENCE
     ELSE
        PERFORM IDMS-STATUS.
     PERFORM A000-GET-NDX-SET THRU A000-EXIT
          UNTIL DB-END-OF-SET.
     FINISH.
     GOBACK.
 A000-GET-NDX-SET.
     DISPLAY  EMP-ID-0415
              EMP-LAST-NAME-0415
              EMP-FIRST-NAME-0415.
     OBTAIN NEXT EMPLOYEE WITHIN EMP-NAME-NDX.
*** CHECK FOR ERROR-STATUS = 0307 ***
     IF DB-END-OF-SET
        GO TO A000-EXIT
*** CHECK FOR ERROR-STATUS = 0000 ***
     ELSE IF DB-STATUS-OK
        NEXT SENTENCE
     ELSE
        PERFORM IDMS-STATUS.
 A000-EXIT.
     EXIT.