2015 Jul 07 12:01 PM
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,
2015 Jul 07 12:37 PM
hi Rojer,
1) You are right.
2) Binary search will work only for keys mentioned in the table key.
3) Yes.
2015 Jul 07 12:57 PM
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.
2020 Jan 07 10:30 AM
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.
2020 Jan 07 2:14 PM
uba_heraeus You know, it's not so important to reply to old answers of someone is wrong on the internet 😉