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

Loop on sorted table and binary search

Former Member
0 Likes
11,596

Hello Gurus,

I have few queries about sorted tables

i have a sorted internal table,

itab_sortd type sorted table with non-unique key MATNR WERKS.

Loop at itab_sortd into wa_srtd where matnr = 'xxxx'.

     .....

Endloop.

If I use both the keys in where statement i can achieve a binary search.

1. but what if i am not using the whole key? Will it still do a binary search as i use leftmost part of the key.

I have read that if you use the leftmost part of the key, we can achieve a binary search on sorted tables.

2. Also how about loop with all key fields + additional fields?

     e.g

itab_sortd type sorted table with non-unique key MATNR WERKS.

Loop at itab_sortd into wa_srtd where MATNR = 'xxxx' WERKS= 'yyyy' PSTNK = 'zzzz'.

     .....

Endloop

3.

itab_sortd type sorted table with non-unique key MATNR WERKS.

READ TABLE itab_sortd WITH KEY matnr = 'XXXX'.

Will the above statement result in a binary search ?

Thanks in advance,

4 REPLIES 4
Read only

Former Member
4,242

hi Rojer,

1) You are right.

2) Binary search will work only for  keys mentioned in the table key.

3) Yes.

Read only

Former Member
0 Likes
4,242

Hi Rojar ,

     Please find my answers to your question

1) Yes . As alread the table is sorted with MATNR and WERKS , if you use just MATNR it will act as binary serach only.

2) Please find the below syntax stmt for your requirement (Loop with all key fields + additional fields)

itab_sortd type sorted table with non-unique key MATNR WERKS.

Loop at itab_sortd into wa_srtd where MATNR = 'xxxx' and WERKS= 'yyyy' and PSTNK = 'zzzz'.

     .....

Endloop

3) No.

     For the statements mentioned by you above , only the Linear Search will happen since you have not mentioned the complier to use the BINARY SEARCH technique for reading.You need to explicity mention 'BINARY SEARCH' in the read statement even though its already sorted.BINARY search will decrease the search time , thats the purpose of it.

itab_sortd type sorted table with non-unique key MATNR WERKS.

READ TABLE itab_sortd WITH KEY matnr = 'XXXX' BINARY SEARCH.

Read only

4,242

Hi all,

sorry, but although this has been posted ages ago, I must disagree on 3):

When itab_sortd is declared as SORTED TABLE any access (LOOP, READ,...) will be a binary as soon as an initial part of a key is used.

READ TABLE itab_sortd WITH TABLE KEY matnr = 'XXXX'

READ TABLE itab_sortd WITH KEY matnr = 'XXXX'

will both be executed as binary search, because matnr is the first field of primary key. (same applies for secondary keys...)

It is ONLY the STANDARD TABLE that needs explicit addition BINARY SEARCH.

Read only

0 Likes
4,242

uba_heraeus You know, it's not so important to reply to old answers of someone is wrong on the internet 😉