13 Replies Latest reply: Sep 23, 2011 12:03 AM by Clemens Li RSS

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

Clemens Li
Currently Being Moderated

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

  • Re: read table with key ... binary search - key sequence?!
    Rae Ellen Woytowiez
    Currently Being Moderated

    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

  • Re: read table with key ... binary search - key sequence?!
    Krishnendu Laha
    Currently Being Moderated

    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

  • Re: read table with key ... binary search - key sequence?!
    Kesavadas Thekkillath
    Currently Being Moderated

    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

  • Re: read table with key ... binary search - key sequence?!
    Danish Kazi
    Currently Being Moderated

    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.

  • Re: read table with key ... binary search - key sequence?!
    ram prasad
    Currently Being Moderated

    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.

  • Re: read table with key ... binary search - key sequence?!
    Raymond Giuseppi
    Currently Being Moderated

    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

  • Re: read table with key ... binary search - key sequence?!
    Ravi Shankar
    Currently Being Moderated

    Deleted..not relevant answer..

     

    Regards,

    Ravi.

     

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

  • Re: read table with key ... binary search - key sequence?!
    Maen Anachronos
    Currently Being Moderated

    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.

  • Re: read table with key ... binary search - key sequence?!
    Naimesh Patel
    Currently Being Moderated

    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

Actions