2019 Nov 26 7:26 AM
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
2019 Nov 26 7:42 AM
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.
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.
2019 Nov 26 7:42 AM
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.
2019 Nov 27 3:17 AM
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.
2019 Nov 27 1:23 PM
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. 🙂
2019 Nov 26 8:12 AM
2019 Nov 27 2:14 AM
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 ).

2019 Nov 27 2:21 PM
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 ).
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |