---
title: "Search Vs Search all in COBOL: Top Differences"
description: "The content explains \"SEARCH\" and \"SEARCH ALL\" in COBOL, highlighting their usage and differences."
url: https://srinimf.com/2015/10/02/cobol-search-and-search-all-differences/
date: 2015-10-02
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2020/07/pexels-photo-2331014.jpeg?fit=1880%2C1253&ssl=1
categories: ["COBOL"]
tags: ["Search and Search All in COBOL"]
type: post
lang: en
---

# Search Vs Search all in COBOL: Top Differences

Here are the [differences between](https://srinimf.com/2020/08/22/linux-find-vs-locate-quick-differences/) Search and [Search All](https://srinimf.com/2016/01/07/5-top-limitations-of-cobol-search-all/) in COBOL. Search uses a linear search algorithm, while Search All uses a binary search algorithm.

![Search and Search All in COBOL](https://i0.wp.com/srinimf.com/wp-content/uploads/2015/10/pexels-photo-1358900.jpeg?resize=1880%2C1253&ssl=1)*Photo by Rene Asmussen on [Pexels.com](https://www.pexels.com/photo/green-leafed-plants-on-kitchen-island-1358900/)*

## **SEARCH Statement**

[Search in](https://srinimf.com/2018/01/17/ai-breadth-first-method-to-solve-complex-problems/) COBOL simplifies serial search. Moreover, consider these rules [to use](https://srinimf.com/2018/05/25/cobol-real-time-example-to-write-if-logic-in-search-statement/) the “SEARCH” verb.

- Search uses linear search
- When searching a table, use the SEARCH statement with the AT END clause before any WHEN clauses. 
- It is not necessary to include NOT-WHEN or WHEN OTHER clauses in the SEARCH statement since the AT END clause will handle any unmatched cases. 
- If you have declared a table with multiple indexes, you can use the SEARCH statement with the VARYING keyword and an index name. In this case, the specified index will take priority, regardless of the order of the indexes.

```
SET DIV-IX TO 1.
SEARCH DIVISION-ENTRY
AT END
   MOVE 8 TO ORG-SUB
   WHEN ORD-RECIPIENT = DIVISION-CODE (DIV-IX)
        SET ORG-SUB TO DIV-IX.
   ADD 1 TO DIVISION-COUNT (ORG-SUB).
      COMPUTE DIVISION-AMOUNT (ORG-SUB)
      = DIVISION-AMOUNT (ORG-SUB) + (ORD-ITEM-COUNT * ORD-ITEM-RATE)
END-SEARCH
```

Related[Why do We need to Sort Table Prior to COBOL SEARCH ALL](https://srinimf.com/2016/11/08/why-sorting-is-needed-for-search-all/)

**PERFORM VARYING **

```
PERFORM VARYING DIV-IX FROM 1 BY 1 
UNTIL ORG-RECIPIENT = DIVISION-CODE(DIV-IX) OR DIV-IX > 7
CONTINUE
END-PERFORM.
SET ORG-SUB TO DIV-IX.
ADD 1 TO DIVISION-COUNT (ORG-SUB).
COMPUTE DIVISION-AMOUNT (ORG-SUB) = DIVISION-AMOUNT (ORG-SUB) + (ORD-ITEM-COUNT * ORD-ITEM-RATE).
```

When to use Search?

If there are more than ’50’ items in a Table, then, SEARCH is good.

## **SEARCH ALL Statement**

When using the “SEARCH ALL” operation, keep in mind:

- Search All uses binary search concept.
- The table must be sorted in ascending or descending order.
- Use the “WHEN” clause with the “=” operator to specify the order, like “WHEN test-field (DIV-IX) = receiver-field.”
- You cannot use the “VARYING” clause with the “SEARCH ALL” operation.
- Only the “AND” operator is allowed, and the “OR” operator is not allowed.
- This operation works best when you have 25 rows in a table.

```
01  TABLE-A.
    05 TABLE-ENTRY OCCURS 90 TIMES
           ASCENDING KEY-1, KEY-2
           DESCENDING KEY-3
           INDEXED BY INDX-1.
       10 PART-1       PIC 99.
       10 KEY-1        PIC 9(5).
       10 PART-2       PIC 9(6).
       10 KEY-2        PIC 9(4).
       10 PART-3       PIC 9(18).
       10 KEY-3        PIC 9(5).
```

Related[Best Example for COBOL SEARCH and WHEN Condition](https://srinimf.com/2016/01/06/best-example-for-cobol-search-and-when-condition/)

```
SEARCH ALL TABLE-ENTRY
  AT END
    PERFORM NOENTRY
  WHEN KEY-1 (INDX-1) = VALUE-1 AND
       KEY-2 (INDX-1) = VALUE-2 AND
       KEY-3 (INDX-1) = VALUE-3
    MOVE PART-1 (INDX-1) TO OUTPUT-AREA
END-SEARCH
```

Related Posts

- [Why do We need to Sort Table Prior to COBOL SEARCH ALL](https://srinimf.com/2016/11/08/why-sorting-is-needed-for-search-all/)DateNovember 8, 2016
- [Best Example for COBOL SEARCH and WHEN Condition](https://srinimf.com/2016/01/06/best-example-for-cobol-search-and-when-condition/)DateJanuary 6, 2016
- [COBOL SEARCH: How to Use IF Logic Explained](https://srinimf.com/2018/05/25/cobol-real-time-example-to-write-if-logic-in-search-statement/)DateMay 25, 2018

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2015%2F10%2F02%2Fcobol-search-and-search-all-differences%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2020%2F07%2Fpexels-photo-2331014.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=Search%20Vs%20Search%20all%20in%20COBOL%3A%20Top%20Differences)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2015/10/02/cobol-search-and-search-all-differences/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2015/10/02/cobol-search-and-search-all-differences/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2015/10/02/cobol-search-and-search-all-differences/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2015/10/02/cobol-search-and-search-all-differences/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Search%20Vs%20Search%20all%20in%20COBOL%3A%20Top%20Differences&body=https%3A%2F%2Fsrinimf.com%2F2015%2F10%2F02%2Fcobol-search-and-search-all-differences%2F&share=email)
-

### Like this:

Like Loading…
