Application Development and Automation 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 only

ABAP 7.4 (read table)

DKanna
Explorer
63,601
TYPES: BEGIN OF ty_final,
         ebeln TYPE ekko-ebeln,
         bukrs TYPE ekko-bukrs,
         ebelp TYPE ekpo-ebelp,
       END OF ty_final.

DATA: lt_final TYPE TABLE OF ty_final,
      ls_final TYPE ty_final.

SELECT ebeln, bukrs FROM ekko INTO TABLE @DATA(lt_ekko) UP TO 5 ROWS.

IF sy-subrc = 0.
  SELECT ebeln, ebelp FROM ekpo INTO TABLE @DATA(lt_ekpo)
     FOR ALL ENTRIES IN @lt_ekko WHERE ebeln = @lt_ekko-ebeln.
ENDIF.

SORT: lt_ekko BY ebeln,lt_ekpo BY ebeln.

LOOP AT lt_ekpo INTO DATA(ls_ekpo).
  ls_final-ebeln = ls_ekpo-ebeln.
  ls_final-ebelp = ls_ekpo-ebelp.

*  READ TABLE lt_ekko INTO DATA(ls_ekko) WITH KEY ebeln = ls_ekpo-ebeln BINARY SEARCH.

data(ls_ekko) = lt_ekko[ ebeln = ls_ekpo-ebeln ]."Binary search not working

  IF sy-subrc = 0.
    ls_final-bukrs = ls_ekko-bukrs.
  ENDIF.
  APPEND ls_final TO lt_final.

ENDLOOP.

cl_demo_output=>display( lt_final ).

Hi,

How to use binary search in read table for the new ABAP 7.4

1 ACCEPTED SOLUTION
Read only

Former Member
36,543

You cannot use the BINARY SEARCH addition with the new expression. Even if you could, you should not. Use a sorted table type, so the lookup of records will always be accessed via binary search algorithm implicitly.

DATA: lt_ekko TYPE SORTED TABLE OF ekko WITH UNIQUE KEY ebeln.
TYPES: BEGIN OF ty_final,
         ebeln TYPE ekko-ebeln,
         bukrs TYPE ekko-bukrs,
         ebelp TYPE ekpo-ebelp,
       END OF ty_final.

DATA: lt_final TYPE TABLE OF ty_final,
      ls_final TYPE ty_final.

SELECT ebeln, bukrs FROM ekko INTO TABLE @DATA(lt_ekko) UP TO 5 ROWS.

IF sy-subrc = 0.
  SELECT ebeln, ebelp FROM ekpo INTO TABLE @DATA(lt_ekpo)
     FOR ALL ENTRIES IN @lt_ekko WHERE ebeln = @lt_ekko-ebeln.
ENDIF.

SORT: lt_ekko BY ebeln,lt_ekpo BY ebeln.

LOOP AT lt_ekpo INTO DATA(ls_ekpo).
  ls_final-ebeln = ls_ekpo-ebeln.
  ls_final-ebelp = ls_ekpo-ebelp.

*  READ TABLE lt_ekko INTO DATA(ls_ekko) WITH KEY ebeln = ls_ekpo-ebeln BINARY SEARCH.

data(ls_ekko) = lt_ekko[ ebeln = ls_ekpo-ebeln ]."Binary search not working

  IF sy-subrc = 0.
    ls_final-bukrs = ls_ekko-bukrs.
  ENDIF.
  APPEND ls_final TO lt_final.

ENDLOOP.

cl_demo_output=>display( lt_final ).

Hi,

How to use binary search in read table for the new ABAP 7.4

6 REPLIES 6
Read only

Former Member
36,544

You cannot use the BINARY SEARCH addition with the new expression. Even if you could, you should not. Use a sorted table type, so the lookup of records will always be accessed via binary search algorithm implicitly.

DATA: lt_ekko TYPE SORTED TABLE OF ekko WITH UNIQUE KEY ebeln.
Read only

0 Likes
36,543

NB: the answer is ambiguous ("cannot use [the binary search] with the new expression" (table expression)) because in fact when the table is of type SORTED a table expression uses the binary search algorithm implicitly (if the components match the key), so you mean that the two words "BINARY SEARCH" cannot be used explicitly in a table expression as they are with READ TABLE.

Read only

36,543

sandra.rossi

Thanks, reading the answer after a day was in fact ambiguous. I was trying to say that whenever you have to type BINARY SEARCH it is a certain thing that you are using the wrong table type. 🙂

Read only

FredericGirod
Active Contributor
36,543

You need to declare the internal table as sorted table.

Read only

mlizcano1
Explorer
36,542

Hi, I don´t know what is the error I execute the next code and work fine.

TYPES: BEGIN OF ty_final,
  ebeln TYPE ekko-ebeln,
  bukrs TYPE ekko-bukrs,
  ebelp TYPE ekpo-ebelp,
END OF ty_final.

DATA: lt_final TYPE TABLE OF ty_final,
      ls_final TYPE ty_final.

SELECT ebeln, bukrs FROM ekko INTO TABLE @DATA(lt_ekko) UP TO 5 ROWS.

IF sy-subrc = 0.
  SELECT ebeln, ebelp FROM ekpo INTO TABLE @DATA(lt_ekpo)
        FOR ALL ENTRIES IN @lt_ekko WHERE ebeln = @lt_ekko-ebeln.
ENDIF.

SORT: lt_ekko BY ebeln,lt_ekpo BY ebeln.


LOOP AT lt_ekpo INTO DATA(ls_ekpo).
  ls_final-ebeln = ls_ekpo-ebeln.
  ls_final-ebelp = ls_ekpo-ebelp.
  READ TABLE lt_ekko INTO DATA(ls_ekko) WITH KEY ebeln = ls_ekpo-ebeln BINARY SEARCH.

  IF sy-subrc = 0.
    ls_final-bukrs = ls_ekko-bukrs.
  ENDIF.
  APPEND ls_final TO lt_final.
  CLEAR ls_ekko.
ENDLOOP.

cl_demo_output=>display( lt_final ).

Read only

raphael_almeida
Active Contributor
36,542

Hi dhanushkanna

About your statement:

data(ls_ekko) = lt_ekko[ ebeln = ls_ekpo-ebeln ]."Binary search not working

Beware with this type of table read... Sometimes when this internal table was initial or the line don't exists you'll get a dump...

Use Try Catch or this statement: DATA(ls_ekko) = VALUE #( lt_ekko[ ebeln = ls_ekpo-ebeln ] OPTIONAL ).