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: 

How to read an internal table using binary search.

Former Member
0 Kudos
19,059

Hi all,

when I was reading an internal table with binary search I was not getting proper results but when I was reading internable without binary search means I was getting perfect results .What might be the reason.

Thanks,

Balaji.

11 REPLIES 11

Former Member
7,789

A pre-requisite to use binary search is that you should have sorted the table by the field with which you are searching.

Have you done that.

Regards,

Ravi

Note - Please mark all the helpful answers

Former Member
0 Kudos
7,789

Hi Balaji,

Sort the internal table, before you use the binary search.

sort itab by key1.
read table itab with key key1 = value1 binary search.
       

Hope this helps.

Former Member
0 Kudos
7,789

Hi Balaji,

Before read the internal table you have to sort the internal table what you are reading for example

SORT IT_EKKO BY EBELN.

READ TABLE IT_EKKO WITH KEY EBELN = IT_EKPO-EBELN BINARY SEARCH.

Thanks

Vikranth Khimavath

Message was edited by:

Khimavath Vikranth

Former Member

Sandeep_Kumar
Product and Topic Expert
Product and Topic Expert
0 Kudos
7,789

HI,

Sort the internal table on the key always before using binary search.

Sort itab by key1.

read table itab with key1 = value binary earch.

Hope this helps.

Former Member
0 Kudos
7,789

Hi Balaji,

You need to SORT the internal table with the field you are using in BINARY SEARCH.

SORT ITAB BY MATNR.

READ TABLE ITAB WITH KEY MATNTR = <VALUE> BINARY SEARCH.

Thanks,

Vinay

Former Member
0 Kudos
7,789

Hi

You internal table should be sorted by the key (all the fields in WITH KEY) what you are using for READ. Use the fields in <b>Order</b> in both the places.

Regards

Surya.

Former Member
0 Kudos
7,789

Hi,

It is required to sort the internal table by the fields that you are using to read the table.

If you want to read table t_itab with vbeln and posnr and use binary search then you need to sort

t_itab by vbeln posnr.

read table t_itab into wa_itab with key vbeln = <sales order>

posnr = <item >

binary search.

If the table is sorted then only binary search works else not..

Hope this makes it clear.

Regards,

Shahu

Former Member
0 Kudos
7,789

Hi all,

thanks for all your replies.I sorted the internal table as u all said.Suppose I have three fields field1,field2,field3 in the internal table itab.I am sorting using only field1 and field2 but not field3.I am reading the internal table with all the three fields.In this scenarion I am using binary search .But the results are not perfect .The results are perfect in the absence of binary search in this scenario.

So do I need to sort all the three fields field1,field2,field3 to get correct results.

Thanks,

Balaji

0 Kudos
7,789

Yes.

Rob

Muthyal_Reddy
Newcomer
0 Kudos
5,819

Hello,

sort your internal table.

SORT  itab BY EBELN.

LOOP AT itab2  ASSIGNING FIELD-SYMBOL(<WA_ITM>).

 READ TABLE itab INTO DATA(WA)  WITH  KEY  EBELN = <WA_ITM>-ebeln  BINARY SEARCH.

endloop.