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: 

READ statement with binary search

Former Member
0 Kudos

Hi friends,

I know that while using the READ statement that we have to sort data and use BINARY SEARCH for faster search.

I have a situation

following are internal table contents

belnr agent action

9000001 name1 BRW

9000001 name1 API

when i use READ statement with where condition ( ( belnr - 9000001 ) and ( action = 'BRW' ) ) with binary search then the SY_SUBRC value is 4.

if i remove the binary search then its giving SY-SUBRC value 0.

Can anybody explain why BINARY SEARCH fails.

Points will be rewarded for correct answers.

Thanks and regards,

Murthy

8 REPLIES 8

Former Member
0 Kudos

Yes,

U need to sort the data first on the key fields you are using.

Hope it helps

Former Member
0 Kudos

Hi,

check on which field u r sorting

u have to sort the internal table by belnr and action.

sort itab by belnr action.

regards,

lavanya

Former Member
0 Kudos

Hi,

Thanks for your quick response.

I tried with belnr and action too. but it fails .

Thanks and Regards

0 Kudos

You may need to change the internal table structure to: -

9000001 BRW name1

9000001 API name1

Former Member
0 Kudos

try this i am not getting sy-subrc 4

TYPES:BEGIN OF TY_ITAB,

BELNR TYPE BELNR,

AGENT(30),

ACTION(5),

END OF TY_ITAB.

DATA:IT_TAB TYPE TABLE OF TY_ITAB,

WA_TAB TYPE TY_ITAB.

WA_TAB-BELNR = 9000001.

WA_TAB-AGENT = 'name1'.

WA_TAB-ACTION = 'BRW'.

APPEND WA_TAB TO IT_TAB.

CLEAR WA_TAB.

WA_TAB-BELNR = 9000002.

WA_TAB-AGENT = 'name 2'.

WA_TAB-ACTION = 'API'.

APPEND WA_TAB TO IT_TAB.

loop at it_tab into wa_tab.

read table it_tab into wa_tab with key belnr = wa_tab-belnr binary search .

write: sy-subrc, wa_tab-agent.

endloop.

Former Member
0 Kudos

You have sorted with 3 fields..

field1, field2, field3.

then u can use any of these combinations for reading with binary search

field1

field1 and field2

field1 and field2 and field3.

other than this u cannot use any combination.

solution is to change the internal table strcture or sort using the required fields only.. in ur case field1 and field3.

Former Member
0 Kudos

hi,

if you are using binary search in your read statement then you need to sort the internal table in ascending order i.e. by default the sort statement does . But in between you have sorted the table in descending order then your read statement with binary search will not work.

Or if you want that your search should works fast, declare the internal table as sorted type and then you can use the binary search statement but in that case you can not put any sort statement on the table.

Or

In case you have to do sorting in descending order then you need to provide all the possible key fields to the read statement read statement with key addition. So it will find the unique record with possible combination of key fields.

I hope this will solve your query.

Former Member
0 Kudos

Hey its simple...

Internal table - i_tab (say)

belnr agent action

9000001 name1 BRW

9000001 name1 API

you need to sort itab:

sort i_tab by belnr action.

Now you can read it:

Read table i_tab into wa_tab with key belnr = '9000001' action = 'BRW' binary search.

Regards, Tapas