Created On:  24 August 2012

Problem:

Program is executing a COBOL SEARCH statement but it appears to be failing on a data match that is definately in the table.

What could be wrong?

The program looks like:

 01  ERROR-MSG                    PIC  X(28) VALUE SPACES.       
 01  ERR-IO-STATUS              PIC  X(02) VALUE SPACES.        
                                                                  
 01  STATUS-CODE-TABLE.                                           
       03  STATUS-CODE-TBL.                                        
             05  STCODE-00            PIC X(02) VALUE '00'.          
             05  STCODE-00-DESC       PIC X(28) VALUE   'NO ERROR.                   '.                      
             05  STCODE-10            PIC X(02) VALUE '10'.          
             05  STCODE-10-DESC       PIC X(28) VALUE   'END OF FILE REACHED.        '.                      
             05  STCODE-22            PIC X(02) VALUE '22'.           
             05  STCODE-22-DESC       PIC X(28) VALUE   'DUPLICATE KEY.              '.                      
             05  STCODE-23            PIC X(02) VALUE '23'.          
             05  STCODE-23-DESC       PIC X(28) VALUE   'RECORD NOT FOUND.           '.                      
             05  STCODE-35            PIC X(02) VALUE '35'.           
             05  STCODE-35-DESC       PIC X(28) VALUE   'EMPTY FILE.                 '.                      
       03  FILLER  REDEFINES  STATUS-CODE-TBL.                     
             05  ST-CD-ENTRIES  OCCURS  5  TIMES                     
                   INDEXED BY ST-CD-IDX.                               
                   07  ST-CD-NUM        PIC X(02).                     
                   07  ST-CD-DESC       PIC X(28).                      

 PROCEDURE DIVISION.                                             
                                                                 
      MOVE SPACES          TO ERROR-MSG.                         
      MOVE '35'            TO ERR-IO-STATUS.                      
      SEARCH ST-CD-ENTRIES                                       
          AT END                                                 
             MOVE 'NOT FOUND'  TO ERROR-MSG                      
          WHEN ERR-IO-STATUS = ST-CD-NUM(ST-CD-IDX)              
             MOVE ST-CD-DESC(ST-CD-IDX) TO ERROR-MSG             
          END-SEARCH                                              
                                                                 

Resolution:

The problem is that you are not initializing the index before executing the SEARCH.

If you add the following line before the SEARCH starts it works correctly:

SET ST-CD-IDX TO 1
-------------------------

01 ERROR-MSG PIC X(28) VALUE SPACES.
01 ERR-IO-STATUS PIC X(02) VALUE SPACES.

01 STATUS-CODE-TABLE.
     03 STATUS-CODE-TBL.
          05 STCODE-00 PIC X(02) VALUE '00'.
          05 STCODE-00-DESC PIC X(28) VALUE 'NO ERROR. '.
          05 STCODE-10 PIC X(02) VALUE '10'.
          05 STCODE-10-DESC PIC X(28) VALUE 'END OF FILE REACHED. '.
          05 STCODE-22 PIC X(02) VALUE '22'.
          05 STCODE-22-DESC PIC X(28) VALUE 'DUPLICATE KEY. '.
          05 STCODE-23 PIC X(02) VALUE '23'.
          05 STCODE-23-DESC PIC X(28) VALUE 'RECORD NOT FOUND. '.
          05 STCODE-35 PIC X(02) VALUE '35'.
          05 STCODE-35-DESC PIC X(28) VALUE 'EMPTY FILE. '.
     03 FILLER REDEFINES STATUS-CODE-TBL.
          05 ST-CD-ENTRIES OCCURS 5 TIMES
               INDEXED BY ST-CD-IDX.
               07 ST-CD-NUM PIC X(02).
               07 ST-CD-DESC PIC X(28).

PROCEDURE DIVISION.

          MOVE SPACES TO ERROR-MSG.
          MOVE '35' TO ERR-IO-STATUS. 

          SET ST-CD-IDX TO 1 
         
          SEARCH ST-CD-ENTRIES
              AT END
                   MOVE 'NOT FOUND' TO ERROR-MSG
              WHEN ERR-IO-STATUS = ST-CD-NUM(ST-CD-IDX)
                  MOVE ST-CD-DESC(ST-CD-IDX) TO ERROR-MSG 
          END-SEARCH

Incident #2588915