06-06-2008 8:28 AM
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
06-06-2008 8:30 AM
Yes,
U need to sort the data first on the key fields you are using.
Hope it helps
06-06-2008 8:33 AM
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
06-06-2008 9:25 AM
Hi,
Thanks for your quick response.
I tried with belnr and action too. but it fails .
Thanks and Regards
06-06-2008 9:28 AM
You may need to change the internal table structure to: -
9000001 BRW name1
9000001 API name1
06-06-2008 10:04 AM
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.
06-06-2008 10:13 AM
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.
06-06-2008 10:22 AM
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.
06-06-2008 10:23 AM
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