4

Let's say I have a table quants and want to find out if any line exists, where the field lenum is not initial. The table is declared inline using a select statement, so I do not have a key available.

Because I don't have a key, the following solution does not work:

line_exists( VALUE #( FOR wa IN quants WHERE ( lenum IS NOT INITIAL ) ( wa ) ) )

Since I want to check for inequality, a table expression does not work:

line_exists( quants[ lenum NE '' ] )

The only solution that I have come up with so far is the following:

abap_true EQ REDUCE abap_bool( INIT bool = abap_false FOR quant IN quants WHERE ( lenum IS NOT INITIAL ) NEXT bool = abap_true )

Obviously there are "old fashioned" solutions, but is there any newer-style?

By "old fashioned" I mean solutions like this:

LOOP AT quants INTO DATA(wa).
  IF wa-lenum IS INITIAL.
    DATA(found) = abap_true.
  ENDIF.
ENDLOOP.
IF found EQ abap_true.
  ...
ENDIF.
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
Kevin Holtkamp
  • 479
  • 4
  • 17
  • 2
    I am not aware of any modern solution for this, but the "old fashioned" can be improved: LOOP AT quants TRANSPORTING NO FIELDS WHERE lenum IS NOT INITIAL. EXIT. ENDLOOP. IF sy-subrc EQ 0. "line exists ELSE. "line does not exist ENDIF. I believe this is also the best from performance point of view. – József Szikszai Oct 26 '22 at 12:33
  • Nothing smart AFAIK. One of the (ugly) workarounds is to declare `TYPES ty_quant LIKE LINE OF quants.` after `SELECT ... INTO TABLE @DATA(quants)` in order to use `VALUE ty_quant( ... )` instead of `VALUE #( ... )`. Note that with ADT, the refactoring tool converts your inline declaration into a static declaration in a single click. – Sandra Rossi Oct 26 '22 at 13:14
  • A Constructor Expression is counter performing because it will loop at all lines (unless a THROW exception is used within a Conditional Expression, but that would make the "new" form really ugly). – Sandra Rossi Oct 26 '22 at 14:18
  • 2
    i am not 100% sure but i think i read somewhere that line_exists function is basically the same as READ TABLE TRANSPORTING NO FIELDS. So you could also use the ```READ TABLE``` statement ---- Found it ```The predicate function line_exists can be considered as a short form of the statement READ TABLE with the addition TRANSPORTING NO FIELDS following by sy-subrc being checked.``` https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abenline_exists_function.htm – Jünge alles Oct 26 '22 at 15:04

1 Answers1

3

The only thin in "new fashion" would be SELECT, FROM @itab.

DATA(lv_exists) = abap_false.
SELECT SINGLE @abap_true FROM @lt_quant AS quant WHERE quant~lenum IS NOT INITIAL INTO @lv_exists.

See documentation link for performance impact (best case is handled like a table in the table buffer) and limitations (e.g. no string column).

The most performant and less restrictions would be this:

LOOP AT lt_quant TRANSPORTING NO FIELDS WHERE lenum IS NOT INITIAL.
  EXIT.
ENDLOOP.
DATA(lv_exists) = xsdbool( sy-subrc = 0 ).
peterulb
  • 2,869
  • 13
  • 20
  • You're right! I forgot about that since it isn't available in out system yet. The big disadvantage of this would be that it needs a variable and cannot be used inline. – Kevin Holtkamp Nov 02 '22 at 02:22