Search All Example


Scenario - Let us assume we have a employee table for an organization with all active employee numbers (sorted) and we are trying to searching for exmployee E0004 existance. If the employee found, we should display "Employee found". Otherwise, display "Employee not found".

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. TBSRCHAL.
       AUTHOR. MTH.
	   
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-ORG.
          03 WS-EMPLOYEE OCCURS 6 TIMES 
                         INDEXED BY IDX-EMP.   
             05 WS-EMPLOYEE-NUM       PIC X(05).   

       PROCEDURE DIVISION.
      * Initializing table with active employee details
      * every 5 characters represents one employee number.
           MOVE 'E0001E0002E0004E0005E0007'
  		     TO WS-ORG.
      * Initializing index
           SET IDX-EMP         TO 1.
      * Search table using index
           SEARCH ALL WS-EMPLOYEE
               AT END DISPLAY "Employee not found"
             WHEN WS-EMPLOYEE-NUM(IDX-EMP) = 'E0004'
                  DISPLAY "Employee found"
           END-SEARCH.

           STOP RUN.

Output -

Employee found

Explaining Example -

In the above example:

  • It initializes a table named WS-ORG with active employee details represented as five-character strings, each representing one employee number.
  • It then initializes an index and searches the table in binary searching method using the SEARCH ALL statement.
  • If the employee number 'E0004' is found in the table, it displays "Employee found"; otherwise, it displays "Employee not found" at the end of the search.