Summary -

In this topic, we described about the SEARCH ALL Statement with detailed example.

The SEARCH ALL statement executes a binary search. The index associated with identifier-1 need not be initialized by SET statements. The table should be in sorted order before the binary search (SEARCH ALL) applies it.

The search index is varied during the search operation. The search value should not less than the value of the first table element nor greater than the value of the last table element.

Before using a binary search on a multidimensional table, SET statements must execute to set the value of the index for each superordinate dimension. To search an entire two-dimensional to seven-dimensional table, SEARCH statement must execute for each dimension.

If the search ends without the WHEN condition satisfied and AT END phrase is specified, imperative-statement-1 is executed. If the AT END phrase is omitted, control passes to the next statement after the SEARCH statement.

The results of a SEARCH ALL operation is as expected when the table data is sorted in ASCENDING KEY or DESCENDING KEY order.

Syntax -

SEARCH ALL Syntax

identifier-1

Identifier-1 specifies the table name to be searched. Identifier-1 references all occurrences within the table. The declaration for identifier-1 must contain an OCCURS clause with the INDEXED BY and KEY IS phrases.

Identifier-1 can reference a data item that is subordinate to a data item with an OCCURS clause. In this case, it must specify an INDEXED BY phrase for each dimension of the table.

Identifier-1 must not be subscripted or reference-modified.

AT END

The condition occurred when the search operation terminates without satisfying the condition specified in any of the WHEN phrases.

WHEN phrase

If the search is unsuccessful, control is passed to imperative-statement-1 of the AT END phrase when specified or to the next statement after the SEARCH statement.

If the WHEN phrase satisfied, control passes to imperative-statement-2 if specified or to the next executable sentence if the NEXT SENTENCE phrase is specified. The index contains the occurrence that has the value satisfied.

After imperative-statement-2 is executed, control passes to the end of the SEARCH statement.

condition-name-1, condition-name-2

Each condition-name should have a single value and must be associated with an ASCENDING KEY or DESCENDING KEY data item for the table element.

data-name-1, data-name-2

Specifies an ASCENDING KEY or DESCENDING KEY data item in the table element. Each data-name can be qualified.

data-name-1 must be a valid operand for comparison with identifier-3, literal-1 or arithmetic-expression-1 according to the rules of comparison.

data-name-2 must be a valid operand for comparison with identifier-4, literal-2 or arithmetic-expression-2 according to the rules of comparison.

data-name-1 and data-name-2 can't be -

  • Floating-point data items
  • Group items containing variable-occurrence data items
  • Date fields

identifier-3, identifier-4

Should not be an ASCENDING KEY or DESCENDING KEY data item or an item subscripted by the first index-name for identifier-1. Identifier-3 and identifier-4 can't be date fields.

literal-1, literal-2

literal-1 or literal-2 must be a valid operand for comparison with data-name-1 or data-name-2 respectively.

Example:

Below example used to search student name from pre-sorted array.

Code:


 IDENTIFICATION DIVISION.                                        
 PROGRAM-ID. PERFTIMI.                                           
 ENVIRONMENT DIVISION.                                           
 DATA DIVISION.                                                  
 WORKING-STORAGE SECTION.                                        
  01 SECTION 
     02 STUDENT-NAME OCCURS PIC X(30) 10 TIMES
              ASCENDING KEY IS STUDENT-NAME
              INDEXED BY STD-IDX. 
  01 I               PIC X(02).
  01 IN-STD-NAME     PIC X(30) VALUE SPACES.

 PROCEDURE DIVISION.                                             
     
	ACCEPT IN-STD-NAME
	
	PERFORM I FROM 1 BY 1 UNTIL I > 10 
	   ACCEPT STUDENT-NAME[I]  
	END-PERFORM.
	
	SET STD-IDX TO 1.
	
	SEARCH ALL STUDENT-NAME 
	       AT END DISPLAY ‘STUDENT DETAILS NOT FOUND' 
	 WHEN STUDENT-NAME (STD-IDX) = IN-STD-NAME 
	      DISPLAY ‘STUDENT DETAILS FOUND'
	END-SEARCH. 
	
	STOP RUN.