09-21-2011 9:39 PM
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
09-22-2011 2:23 PM
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
09-21-2011 10:06 PM
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
09-21-2011 10:15 PM
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
09-22-2011 4:49 AM
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
09-22-2011 6:22 AM
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.
09-22-2011 6:42 AM
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.
09-22-2011 9:58 AM
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
09-22-2011 10:05 AM
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
09-22-2011 10:48 AM
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
09-22-2011 10:53 AM
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
09-22-2011 10:58 AM
Deleted..not relevant answer..
Regards,
Ravi.
Edited by: rshankar on Sep 23, 2011 10:57 AM
09-22-2011 11:14 AM
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.
09-22-2011 2:23 PM
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
09-22-2011 11:03 PM
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