Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Problem with Binary search.

Former Member
0 Likes
4,242

I am looping and internal table, and reading the same internal table inside the loop when certain if conditions are satisfied.
The issue is the Read statement is not successful with the addition Binary search, even though is satifies all the if conditions.
It returns a sy-subrc value as 8.

When i remove the Binary search addition, the Read statement is successful with sy-subrc = 0. I  have sorted the internal table before the loop.


Can anyone explain why its happeing so?

9 REPLIES 9
Read only

matt
Active Contributor
0 Likes
2,333

Define your tables as SORTED and a) you'll be using the more recent technology, introduced just 13 years ago... and b) you won't have BINARY SEARCH to add, so your problem will go away.

Read only

ronaldo_aparecido
Contributor
2,333

The problem is probably why u are not using the SORT command before giving the command read.

Note: The sort command should be done for the condition of the field used in the read

Example:

Puts the sort before

it_mara sort by ascending MATNR.

Read table with key it_mara into wa_mara lsmara-MATNR MATNR = binary search.

Read only

0 Likes
2,333

Yes, you are right. It is better to use SORT command exactly one line before the READ <ITAB> INTO <WA> WITH KEY <CONDITION> BINARY SEARCH

Reason: If the READ statement is used inside the loop, there may be chances that <ITAB> values gets added or removed and the sorting order of <ITAB> gets changed. So every time the READ statement is accessed inside the loop, the SORT statement should be accessed every time to get the successful results(SY-SUBRC = 0). Otherwise, We will get error while accessing READ statement with BINARY SEARCH(SY-SUBRC = 😎

Read only

kamesh_g
Contributor
0 Likes
2,333

Option 1.

define your internal table as sorted table

ex : lt_table type sorted table of <structure/table ref> with key  -latest

option 2 :

Make sure you sorted your internal   table with all the fields which you are using in read statement other wise read will fail .

Read only

Former Member
0 Likes
2,333

You have to sort your internal table by the fields that you're comparing in READ statement. Place the SORT statement just before the READ TABLE.

Read only

RaymondGiuseppi
Active Contributor
0 Likes
2,333

Just read the documentation of the BINARY SEARCH option, to understand.

Regards,

Raymond

Read only

Former Member
0 Likes
2,333

Thanks experts, I got the solution.

The issue with my code was that, i had sorted the internal table with the specified keys in descending.

But Binary Search in the read statement works, only if the internal table is sorted with the same keys in the Read statement in Ascending order.

Thanks Raymond for your link.

Read only

0 Likes
2,333

Text removed by moderator

(And he couldn't give points as the post was unmarked as a question)

Message was edited by: Matthew Billingham

Read only

Former Member
0 Likes
2,333

Hi Deepti,

Please SORT your table with KEY before you use it in READ with Binary Search.

This is the prerequisite with BINARY SEARCH, that your table must be SORTED before READ.

For example:

SORT itab by fld_xyz.

Read table itab with KEY fld_xyz = 'X'.

Hope this'll solve your issue.