Checking for Set Membership
When accessing the database, you may find it necessary to obtain information about an owner or member record's set-membership status. To obtain set-specific information for a record occurrence, use the IF statement. By using the IF statement, you can determine:
idmscu19
When accessing the database, you may find it necessary to obtain information about an owner or member record's set-membership status. To obtain set-specific information for a record occurrence, use the IF statement. By using the IF statement, you can determine:
- If the current occurrence of a specified set contains any member record occurrences (Is it empty?)
- If the current record of run unit participates as a member in a specified set defined with either the optional or the manual set membership option (Is it currently connected to an occurrence of the specified set?)
Each IF statement contains a conditional phrase and an imperative statement that specifies further action based on the outcome of the evaluation.
'Native VSAM users'. The IF statement is not allowed for sets defined with member records that are stored in native VSAM data sets.
Use of the IF EMPTY statement and the IF MEMBER statement are discussed below.
This article describes the following information:
2
2
Using the IF EMPTY Statement
After you have retrieved the owner record in a set, you can issue the IF EMPTY statement to determine if the set owns any member record occurrences. This allows you to control processing based on whether the set is empty.
Steps in Determining if a Set Is Empty
To determine if a set is empty, perform the following steps:
- Establish currency for the set.
- Issue the IF EMPTY statement.
- Perform further processing as specified.
If the set contains a member record, the first record in the set is always accessed during the processing of an IF EMPTY DML statement, in order to determine whether or not it is logically deleted. This can result in additional I/Os particularly if the member records are not stored VIA the set being tested.
How to Avoid an OBTAIN
You can also use the IF EMPTY statement to eliminate the need for using an OBTAIN FIRST WITHIN SET statement to walk a set, as illustrated in the program excerpt below.
Because the IF EMPTY statement determines that the set is not empty, you can be assured that the program can read at least one EMPLOYEE record before an ERROR-STATUS of 0307 (DB-END-OF-SET) is returned.
. . . IF DEPT-EMPLOYEE IS EMPTY MOVE NO-EMP-MESSAGE TO TITLE-OUT ELSE PERFORM A100-DEPT-EMP-WALK THRU A100-EXIT UNTIL DB-END-OF-SET. A100-DEPT-EMP-WALK. OBTAIN NEXT WITHIN DEPT-EMPLOYEE. . . .
Using the IF MEMBER Statement
If the current record of run unit participates as a member in a set defined with either the optional or the manual set membership option, you cannot assume that that record occurrence is also current of set. For example, an optional record may never have been connected to the set, or a manual record may have been disconnected from the set.
For more information about optional and manual set membership, see Set Membership Options in the Sets topic.
Failure to Test for Set Membership
The figure below shows the invalid conclusion that can result from not testing for set membership.
Since EMPLOYEE 480 is not currently connected to an occurrence of the OFFICE-EMPLOYEE set, the OBTAIN OWNER statement retrieves the owner of the current record of set (OFFICE 3). This leads to the invalid assumption that EMPLOYEE 480 works in OFFICE 3.
R. U. Record Set Areacurr. currencies currencies currencies┌─────┐┌─────┬─────┬─────┐┌─────┬─────┐┌─────┬─────┐ │ R ││ D │ E │ O ││ D │ O ││ O │ E │ │ U ││ E │ M │ F ││ E │ F ││ R │ M │ │ N ││ P │ P │ F ││ P │ F ││ G │ P │ │ ││ A │ L │ I ││ T │ I ││ - │ - │ │ U ││ R │ O │ C ││ - │ C ││ D │ D │ │ N ││ T │ Y │ E ││ E │ E ││ E │ E │ │ I ││ M │ E │ ││ M │ - ││ M │ M │ │ T ││ E │ E │ ││ P │ E ││ O │ O │ │ ││ N │ │ ││ L │ M ││ - │ - │ │ ││ T │ │ ││ O │ P ││ R │ R │ │ ││ │ │ ││ Y │ L ││ E │ E │ │ ││ │ │ ││ E │ O ││ G │ G │ │ ││ │ │ ││ E │ Y ││ I │ I │ │ ││ │ │ ││ │ E ││ O │ O │ │ ││ │ │ ││ │ E ││ N │ N │ │ ││ │ │ ││ │ ││ │ │ ┌────────────────────────┼─────┼┼─────┼─────┼─────┼┼─────┼─────┼┼─────┼─────┤ │PREVIOUSLY ESTABLISHED │5200 ││5200 │ 477 │ 3 ││5200 │ 477 ││ 477 │ 5200│ │CURRENCIES │ ││ │ │ ││ │ ││ │ │ ├────────────────────────┼─────┼┼─────┼─────┼─────┼┼─────┼─────┼┼─────┼─────┤ │OBTAIN FIRST WITHIN │ 480 ││5200 │ 480 │ 3 ││ 480 │ 477 ││ 480 │ 5200│ │DEPT-EMPLOYEE. │ ││ │ │ ││ │ ││ │ │ ├────────────────────────┼─────┼┼─────┼─────┼─────┼┼─────┼─────┼┼─────┼─────┤ │OBTAIN OWNER WITHIN │ 3 ││5200 │ 480 │ 3 ││ 480 │ 3 ││ 480 │ 3 │ │OFFICE-EMPLOYEE. │ ││ │ │ ││ │ ││ │ │ └────────────────────────┴─────┴┴─────┴─────┴─────┴┴─────┴─────┴┴─────┴─────┘
Steps in Testing for Set Membership
You can issue the IF MEMBER statement to ensure that a record occurrence currently participates as a member of a specified set.
To determine if a record participates as a member in a set, perform the following steps:
- Establish run unit currency for the specified member record.
- Issue the IF MEMBER statement.
- Perform further processing, as specified
The program excerpt below uses the IF EMPTY and the IF MEMBER statements to facilitate database navigation.
The IF EMPTY statement determines if the DEPT-EMPLOYEE set is empty; the IF MEMBER statement determines whether the current of run unit (EMPLOYEE) participates as a member in the OFFICE-EMPLOYEE set.
PROCEDURE DIVISION. . . . A100-EMP-DEPT-OFF. MOVE DEPT-ID-IN TO DEPT-ID-0410. OBTAIN CALC DEPARTMENT IF DB-REC-NOT-FOUND GO TO GET-NEXT ELSE IF DB-STATUS-OK NEXT SENTENCE ELSE PERFORM IDMS-STATUS. *** TEST TO SEE IF SET IS EMPTY *** IF DEPT-EMPLOYEE IS EMPTY MOVE NO-EMP-MESSAGE TO TITLE-OUT ELSE PERFORM A100-WALK THRU A100-EXIT UNTIL DB-END-OF-SET. A100-WALK. OBTAIN NEXT WITHIN DEPT-EMPLOYEE. *** CHECK FOR ERROR-STATUS = 0307 *** IF DB-END-OF-SET GO TO A100-EXIT ELSE IF DB-STATUS-OK NEXT SENTENCE ELSE PERFORM IDMS-STATUS. *** TEST TO SEE IF EMPLOYEE IS CURRENTLY CONNECTED TO THE SET *** IF NOT OFFICE-EMPLOYEE MEMBER MOVE NO-OFF-MESSAGE TO TITLE-OUT ELSE OBTAIN OWNER WITHIN OFFICE-EMPLOYEE PERFORM IDMS-STATUS MOVE OFFICE-ADDRESS-0450 TO ADDRESS-OUT. PERFORM U100-PRINT. A100-EXIT. EXIT.