Best Example for COBOL SEARCH and WHEN Condition

The below flow chart shows how Search in COBOL works. So, SEARCH is a serial search. SEARCH ALL is a binary search.

Search in COBOL

Search

Key points

  • Multiple WHEN conditions you can give
  • AND , ‘=’ , ‘>’, ‘<‘ operators you can give in WHEN condition
  • For ‘OR’ operator, the below way you can do coding in COBOL program
Search 
. . 
 WHEN A 
 WHEN B
 imperative sentence . . 
End-search

Read more: Best tutorial for COBOL serial search

  • COBOL Search
  • How to Tune COBOL Program
  • COBOL Examples

Example for SEARCH:

01  TABLE-ONE.     
 05 TABLE-ENTRY1 OCCURS 10 TIMES INDEXED BY TE1-INDEX.        
  10 TABLE-ENTRY2 OCCURS 10 TIMES INDEXED BY TE2-INDEX.
   15 TABLE-ENTRY3 OCCURS 5 TIMES  ASCENDING KEY IS KEY1 INDEXED BY TE3-INDEX.   
    20 KEY1                 PIC X(5).              
    20 KEY2                 PIC X(10)
. . . . 
PROCEDURE DIVISION.     
. . .     
SET TE1-INDEX TO 1     
SET TE2-INDEX TO 4     
SET TE3-INDEX TO 1     

MOVE “A1234” TO KEY1 (TE1-INDEX, TE2-INDEX, TE3-INDEX + 2)     
MOVE “AAAAAAAA00” TO KEY2 (TE1-INDEX, TE2-INDEX, TE3-INDEX + 2)    
 . . .
SEARCH TABLE-ENTRY3       
AT END         
 MOVE 4 TO RETURN-CODE.       
  WHEN TABLE-ENTRY3(TE1-INDEX, TE2-INDEX, TE3-INDEX) = “A1234AAAAAAAA00”         
 MOVE 0 TO RETURN-CODE     
END-SEARCH

Related

Author: Srini

Experienced Data Engineer, having skills in PySpark, Databricks, Python SQL, AWS, Linux, and Mainframe