6

ABAP 7.40 added the line_exists( ... ) predicate function to analyse internal tables. But is there any way I can check for the presence of a line where a particular column is initial or different from a target value?

For instance, how can I check for a line with an initial Material column like the third line in this table?

Document        Country     Material    
9001287         US          198572111
9001296         FR          160023941       
9001297         EG                      
9001299         DK          873001102   

I could check for Danish entries with line_exists( lt_itab[ Country = 'DK' ] ) and line_exists( lt_itab[ Material = '' ] ) is valid but neither <> nor NE seem to be accepted. There also seems to be no way to check for lines where the country isn't 'FR' for instance?

If there's no way to do this with line_exists, what would be the most condensed alternative approach?

Lilienthal
  • 4,327
  • 13
  • 52
  • 88
  • As Suncatcher replied, `line_exists` works like `READ TABLE` so only `=` is supported. Note that you may use line_exists with several components: `line_exists( lt_itab[ country = 'DK' material = ' ' ] )` – Sandra Rossi Sep 27 '18 at 16:09

4 Answers4

4

LOOP is one way to check, I don't know if there is anything better:

LOOP AT itab
     TRANSPORTING NO FIELDS
     WHERE country NE 'FR'.
  EXIT.
ENDLOOP.
IF sy-subrc EQ 0.
" line exists
ELSE.
" line does not exist
ENDIF.
József Szikszai
  • 4,791
  • 3
  • 14
  • 24
4

No, you cannot.

line_exists is simple predicate function which accepts only table expressions tab[ a = b ]. And, as we know, table expressions is simply a new syntax for READ TABLE, nothing more. All rules and constraints including allowed comparison type are applied to expressions as well.

Check H. Keller's blog for more details.

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
3

It's a little late. But now you can do the following:

xsdbool( line_exists( lt_itab[ Country = 'DK' ] ) ) = abap_false
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Andreas
  • 51
  • 4
1

A little bit later, here's another (shorter) way to do the same thing as in Andreas' answer:

IF NOT line_exists( lt_itab[ country = 'DK' ] ).

However, this tests whether there is no line equal to DK in the table. It does NOT test whether there is any line that is unequal to DK. If you want the second thing, you have to resort to LOOP as József pointed out. Or you could compress it into one line like this:

IF lines( VALUE type( FOR x IN lt_itab WHERE ( country <> 'DK' ) ( x ) ) ) > 0.

Unfortunately, you cannot use VALUE #( ), so you have to put in the type of lt_itab.

If country is the primary key, another possibility is

IF lines( FILTER #( lt_itab WHERE country <> 'DK' ) ) > 0.

and if country is only the secondary key, you could do

IF lines( FILTER #( lt_itab USING KEY country WHERE country <> 'DK' ) ) > 0.
schachmett
  • 19
  • 2