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: 

BINARY SEARCH when readign a table

Former Member
0 Kudos

ABAP experts,

I have a question on READ TABLE statement.

I have an internal table say INT_TAB with say 5 fields already filled and 3 more that need to be filled.

I loop at internal table into WA_TAB.

Then I read table INT_TAB2 INTO WA_TAB2

WITH KEY field1 = WA_TAB-field1

AND field2 = WA_TAB-field2

BINARY SEARCH.

Endloop.

Before reading and before looping I sort INT_TAB2 by both the key fields.

My observation is that when I use BINARY SEARCH, for some recods this field gets populated which is OKAY, but for some records it is blank, I then check in SE16 an find that actually the table-field has a non blank value which should have been read.

However when I remove BINARY SEARCH, it does work, but I feel removing it may slow the processes and hamper performance.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

The problem for this might be with the sort condition,

you need to sort the table with the field, which you are using in the WITH KEY.

I have faced the same situation, when i checked it, i found that the problem is with the sorting thing.

so, better check the sorting condition.

even if the problem persists, then post the same..

Regards,

Sunny.

Edited by: sunny suren chilievru on Nov 21, 2008 7:43 AM

8 REPLIES 8

GauthamV
Active Contributor
0 Kudos

hi,

if you want to use binary search then you should sort your internal table or else not required.

Former Member
0 Kudos

Hi,

The order of the field in sort should be same as in condition of Read Statement.

0 Kudos

Looks like you are doing everything right however what I did miss was what you are doing with SYS-SUBRC after the read with binary search.

The read with binary search will position you to the right row (SYS-TABIX).

However the right row might be between keys (not found).

Read up in the help about the various meanings of SYS-SUBRC after the read with binary search.

The read does not need to bring back any fields at all and may just be used for positioning. (transporting no fields).

Have fun

Former Member
0 Kudos

Hi,

check the order of field in which you are sorting and how you have applied them in WITH KEY condition ,they sould be same,secondly check How you have sorted it,It should always be sorted in Ascending Order not in descending.

Former Member
0 Kudos

Check following. it can give wrong result in both the cases.

Is internal table sorted with both the key fields ?

Are there multiple records with same values for key fields ?

Former Member
0 Kudos

Hi,

The problem for this might be with the sort condition,

you need to sort the table with the field, which you are using in the WITH KEY.

I have faced the same situation, when i checked it, i found that the problem is with the sorting thing.

so, better check the sorting condition.

even if the problem persists, then post the same..

Regards,

Sunny.

Edited by: sunny suren chilievru on Nov 21, 2008 7:43 AM

Former Member
0 Kudos

Hi,

The problem might be that you are sorting by f1,f2,f3.

But when reading you are reading with key f2,f1 and f3 .

the sorting and Read sequence should be same.

Regards

Sandipan

Former Member
0 Kudos

As you said, the sort condition is done. Assuming that

Loop INT_TAB into wa_tab.

....

..

read ...

if sy-subrc eq 0.

...

endif.

endloop.

One question is that, do you have the required data in INT_TAB2? You said, you have the data in SE16. But is that fetched and updated in INT_TAB2?