Application Development 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: 

Reading internal tables Using Binary Search

ainjaka_narivelo
Explorer
0 Kudos

Hi all,

i have a doubt when using BINARY SEARCH ...

As SAP says in the link "http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb373d358411d1829f0000e829fbfe/content.htm" :

- "The standard table must be sorted in ascending order by the specified search key."

So my questions are :

1. Binary search do not work if we sort the table in Descending order ?

2. In the sort case below: SORT itab BY matnr vtweg.

     Can i directly Read the table with only the key matnr like below ? I mean, i don't need to sort again itab BY matnr (only)

     READ TABLE itab

       ASSIGNING <fs_itab>

       WITH KEY matnr = lv_matnr

       BINARY SEARCH.

My question is very basic but i think it is important to understand this concept ... hope to have an answer

Thanks in advance

Ainjaka

6 REPLIES 6

Former Member
0 Kudos

Hi,

1) No. Binary Search won't work in case of Descending Sort.

2) No. No need to sort again. This would work fine.

Thanks and Regards,

Chirag

hendrik_brandes
Contributor
0 Kudos

Hello Ainjaka,

as you mentioned it before, the BINARY SEARCH Option works only within the specified parameters.

You cannot use a READ TABLE on a table where the sorting is not ascending by the specified key and you will get a SY-SUBRC = 8 if a violation of the table has been found.

In your case, you can only use "MATNR" as key if your table is sorted ascending because the binary search algorithm follows the key fields of the READ TABLE command. E.g. first search with MATNR - if there are no more fields, it takes the current result.

Regard: You can get multiple results when you use an incomplete key.

Better use table types with "SORTED" or "HASHED" Option.

Kind regards,

Hendrik

Former Member
0 Kudos

1. BINARY SEARCH doesn't work in descending.

2. Usually your SORT BY fields must be the same as your READ KEY fields. On your example it would be ok as you use only the first key fields. It would only dangerous if you use more READ KEY fields than what it is actually sorted.

0 Kudos

Hi C Z,

Statement 2. is only true if you are using binary search. If you are not then it does not matter whether it is sorted or not.

Regards,

Shravan

former_member227141
Active Participant
0 Kudos

Hi Ainjaka, although your doubt is already resolved above, I would like just to add an interesting link, hoping it helps you to clarify the concept around read table with binary search.

http://friendlyabaper.blogspot.mx/2006/10/pure-and-simple-truth-about-binary.html

Best Regards!

Former Member
0 Kudos

Hi

Forget ABAP - just search for what BINARY SEARCH is in programming and you would be able to understand the logic behind the answers to your question.

Regards,

Shravan