COBOL SEARCH and SEARCH ALL

Suppose we are required to search the table and find out whether a particular record is present in the table or not?

What can we do in that case?

The answer to this is – usage of either SEARCH or SEARCH ALL

SEARCH​

SEARCH is a linear search to find a record or records in the internal table (array). SEARCH is also called as Sequential Search.

For SEARCH, the table needs not be in sorted order and the record can be present in any order. It is not required for the records to be in ascending or descending order.

Search Syntax in COBOL

SEARCH TABLE-NAME [VARYING {identifier1/index1}]    [AT END Statement]

    {WHEN ConditionPass
          {statement/NEXT SENTENCE/CONTINUE}…}
END-SEARCH.

SEARCH ALL​

SEARCH ALL is a Binary search to find a record or records in the internal table (array).

For SEARCH ALL, the table needs to be in sorted order and the records must be present in either ascending or descending order.

When using SEARCH ALL, Ascending or Descending key must be defined while defining the table in Working storage section

Search All Syntax in COBOL

SEARCH ALL TABLE-NAME
   [AT END Statement]

   {WHEN ConditionPass
       {statement/NEXT SENTENCE/CONTINUE}…}
END-SEARCH.

Sample COBOL Program to show SEARCH ALL

TutorialBrain-SEARCH ALL in Internal Table(Arrays) of COBOL
TutorialBrain-SEARCH ALL in Internal Table(Arrays) of COBOL

Output

TutorialBrain-Output of SEARCH ALL in Internal Table(Array)
TutorialBrain-Output of SEARCH ALL in Internal Table(Array)

Difference between SEARCH and SEARCH ALL

TutorialBrain-Difference between SEARCH and SEARCH ALL in Internal Table(Array)
TutorialBrain-Difference between SEARCH and SEARCH ALL in Internal Table(Array)

Tutorials for all brains!