Combine conditions in table expression?

248 Views Asked by At

I am using the line_index function and would like to search for two values, not only for carrid but also for connid. Is it possible? If so, in what way?

Because right now, this works:

lv_index = line_index( lt[ carrid = 'LH' ] ).

But after adding the code [ connid = '2407' ] like this:

lv_index = line_index( lt[ carrid = 'LH' ][ connid = '2407' ] ).

I get a syntax error:

LT[ ] is not an internal table

2

There are 2 best solutions below

0
József Szikszai On BEST ANSWER

All fields (conditions) just one after the other inside one bracket:

lv_index = line_index( lt[ carrid = 'LH'
                           connid = '2407' ] ).
2
Sandra Rossi On

I'd like to comment about the chaining of Table Expressions.

So the answer corresponding to the OP example is that a single Table Expression must be used (itab[...]) with as many components as needed, and not a chain of table expressions as was done (itab[...][...]).

lt[ carrid = 'LH' ][ connid = '2407' ] can never be valid (because connid = '2407' would imply that each line of LT is itself an internal table but carrid = 'LH' is contradictory as it means that each line of LT is a structure).

But other syntaxes of chained table expressions can be valid, like e.g. (provided that the internal tables are defined adequately)

itab[ 1 ][ comp1 = 'A' ]
itab[ comp1 = 'A' ][ 1 ]
itab[ comp1 = 'A' ]-itabx[ compx = 42 ]

Here is an example that you can play with:

TYPES: BEGIN OF ty_structure,
         connid TYPE c LENGTH 4,
       END OF ty_structure,
       ty_table TYPE STANDARD TABLE OF ty_structure WITH EMPTY KEY,
       BEGIN OF ty_structure_2,
         carrid TYPE c LENGTH 2,
         table  TYPE ty_table,
       END OF ty_structure_2,
       ty_table_2 TYPE STANDARD TABLE OF ty_structure_2 WITH EMPTY KEY,
       ty_table_3 TYPE STANDARD TABLE OF ty_table_2 WITH EMPTY KEY.

DATA(lt) = VALUE ty_table_3( ( VALUE #( ( carrid = 'LH' table = VALUE #( ( connid = '2407' ) ) ) ) ) ).

DATA(structure) = lt[ 1 ][ carrid = 'LH' ]-table[ connid = '2407' ].