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 table with key ... binary search - key sequence?!

Clemenss
Active Contributor
0 Kudos

Hi,

consider this scenario:

data:
  lt_mbew type table of mbew.
field-symbols:
  <mbew>  type mbew.
select matnr bwkey stprs into corresponding fields of table  lt_mbew where ...
sort lt_mbew by matnr bwkey.
read table lt_mbew assigning <mbew>
  with key
    bwkey = ...
    matnr = ...

fails sometimes (sy-subrc = 4).

read table lt_mbew assigning <mbew>
  with key
    matnr = ...
    bwkey = ...

works correctly.

Why? Could not explain from documentation.

Please explain, thanks.

Regards

Clemens

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

Clemens,

It is mentioned in the READ Table Documentation, found here: http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb373d358411d1829f0000e829fbfe/frameset.htm

The standard table must be sorted in ascending order by the specified search key. The BINARY SEARCH addition means that you can access an entry in a standard table by its key as quickly as you would be able to in a sorted table.

Since your Search Key is BWKEY and MATNR, your table must be sorted by BWKEY MATNR in this sequence.

Regards,

Naimesh Patel

13 REPLIES 13

former_member182010
Active Participant
0 Kudos

Hello Clemens,

The read of lt_mbew in the first block of code sometimes fails because you do not have the keys in the same order as the sort. You have the internal table sorted by MATNR BWKEY. However, you are reading by BWKEY MATNR.

Kind Regards,

Rae Ellen Woytowiez

krishnendu_laha
Active Contributor
0 Kudos

Hello Clemens,

In my view it does not find the value in sorted array when key are not passed in same sequence as it was used for sorting...

In [binary search algorithm|http://en.wikipedia.org/wiki/Binary_search_algorithm] attempt is made to search a value within the array which was prepared internally by sorting key combination...

Thanks

Krish

kesavadas_thekkillath
Active Contributor
0 Kudos

Hi Clemens,

Interesting one !!!

May be a linear search should have the fields in order( in itab ) while doing a read

In my view it does not find the value in sorted array when key are not passed in same sequence as it was used for sorting...

To check this may be you can do the same thing without sorting the itab and check it.

Keshav

Former Member
0 Kudos

Hi Clemens,

I have also faced such a scenario many times wherein the problem of Sy-subrc = 4 occurs when the order of key fields while reading an internal table is shuffled.

Did you try by using the key word "Binary Search" since you are sorting the data ?

Regards,

Danish.

Former Member
0 Kudos

Hi,

Check the code as follows...

data:

lt_mbew type table of mbew,

wa_mbew type mbew.

select matnr bwkey stprs into corresponding fields of table lt_mbew where ...

sort lt_mbew by matnr bwkey.

read table lt_mbew into wa_mbew

with key

matnr = ...

bwkey = ...

Ram.

raymond_giuseppi
Active Contributor
0 Kudos

This is written in the documentation

For the binary search, the table must be sorted by the specified search key in ascending order. Otherwise the search will not find the correct row.

Else the algorithm will fail.

sorted table

A1

A2

B1

C2

D3

Read with key 'C' '2'

first look at middle of table (record (1+5) / 2 = 3)

record 3 is less than 'C2' so

look at next half (record = (3 + 5) / 2 = 4)

record is 'C2' -> found

not sorted table - actual key value (expected key value)

1C (C1)

2C (C2)

2B (B2)

3D (D3)

3E (E3)

Read with key 'C' '2' and not ''2' 'C'

first look at middle of table

record 3 is less than 'C2' so

look at next half

record is 'D3'

look for half between 3 and 4, no more record -> not found

Regards,

Raymond

0 Kudos

Hi Raymond,

For the binary search, the table must be sorted by the specified search key in ascending order. Otherwise the search will not find the correct row.

But Clemens is not using a binary search in his code,its just a linear search.

Kesav

0 Kudos

I think he is, judging by the topic name

read table with key ... binary search - key sequence?!

possibly he left out the BINARY SEARCH in his examples.

And like raymond stated: you need to stick to the order of the key fields in order to have binary search working.

Edited by: Maen Anachronos on Sep 22, 2011 11:49 AM

0 Kudos

Yes i was referring to the thread header, I never get this error when using a linear search, else that would be an Abap "Big Bug", no ?

Regards,

Raymond

Former Member
0 Kudos

Deleted..not relevant answer..

Regards,

Ravi.

Edited by: rshankar on Sep 23, 2011 10:57 AM

Former Member
0 Kudos

If you look at Raymond's description on hwo BINARY SEARCH works then you should note that


  with key
    bwkey = ...
    matnr = ...

causes the split first on BWKEY. Depending on the the contents of your internal table the split may occur incorrect.

I tried it out with a simple ABAP and maybe it will help to understand what happens.


types:  begin of ty_struct,
          field1 type C length 1,
          field2 type i,
        end of ty_struct.

data: it_struct type TABLE OF ty_struct.
data: wa_struct type ty_struct.
data: g_field1  type c length 1.
data: g_field2  type i.

wa_struct-field1 = 'A'. wa_struct-field2 = 0. append wa_struct to it_struct.
wa_struct-field1 = 'A'. wa_struct-field2 = 1. append wa_struct to it_struct.
wa_struct-field1 = 'A'. wa_struct-field2 = 6. append wa_struct to it_struct.

wa_struct-field1 = 'B'. wa_struct-field2 = 1. append wa_struct to it_struct.
wa_struct-field1 = 'B'. wa_struct-field2 = 4. append wa_struct to it_struct.
wa_struct-field1 = 'B'. wa_struct-field2 = 3. append wa_struct to it_struct.

wa_struct-field1 = 'C'. wa_struct-field2 = 7. append wa_struct to it_struct.
wa_struct-field1 = 'C'. wa_struct-field2 = 5. append wa_struct to it_struct.

wa_struct-field1 = 'D'. wa_struct-field2 = 1. append wa_struct to it_struct.
wa_struct-field1 = 'D'. wa_struct-field2 = 2. append wa_struct to it_struct.

sort it_struct by field1 field2.

g_field1 = 'C'.
g_field2 = 5.

loop at it_struct into wa_struct.
  write:/ wa_struct-field1, wa_struct-field2.
endloop.

skip 2.



write: /'Bin search FIELD1 FIELD2', g_field1, g_field2.
read table it_struct into wa_struct with key field1 = g_field1
                                             field2 = g_field2
                                             binary search.
if sy-subrc eq 0.
    write: / 'FOUND:', wa_struct-field1, wa_struct-field2.
else.
  write: / 'Not found'.
endif.

skip.

write: /'Bin search FIELD2 FIELD1', g_field2, g_field1.
read table it_struct into wa_struct with key field2 = g_field2
                                             field1 = g_field1
                                             binary search.
if sy-subrc eq 0.
    write: / 'FOUND:',  wa_struct-field1, wa_struct-field2.
else.
  write: / 'Not found'.
endif.

If you run this example...both binary search gives you a hit. Now change the value of B 4 into B 6 and run it again.

naimesh_patel
Active Contributor
0 Kudos

Clemens,

It is mentioned in the READ Table Documentation, found here: http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb373d358411d1829f0000e829fbfe/frameset.htm

The standard table must be sorted in ascending order by the specified search key. The BINARY SEARCH addition means that you can access an entry in a standard table by its key as quickly as you would be able to in a sorted table.

Since your Search Key is BWKEY and MATNR, your table must be sorted by BWKEY MATNR in this sequence.

Regards,

Naimesh Patel

0 Kudos

Thank you all!

Yes, it is in the documentation - the key sequence must be the same as the sort sequence.

Anyway, I changed it to a hashed table and read it WITH TABLE KEY. Still the best and fastest way.

Regards,

Clemens