abapinternal-tables

Merge two internal tables using comprehensions?


I'm trying to create an internal table assembled from the lines of two internal tables of the same type using table comprehension syntax. Seems simple but I can't do it:

data(lt_fcat1) = VALUE lvc_t_fcat( ).
data(lt_fcat2) = VALUE lvc_t_fcat( ).

data(lt_fcat) = VALUE lvc_t_fcat( for i in lt_fcat1 ( i )
                                  for j in lt_fcat2 ( j ) ).

Gives a syntax error:

Unknown component "for" (on the second for).


Solution

  • You need to prefix your internal table with LINES OF:

    data(lt_fcat) = VALUE lvc_t_fcat( ( LINES OF t_fcat1 )
                                      ( LINES OF t_fcat2 ) ).
    

    If you want to use a for iteration (for ... with or without where ...), you still need LINES OF:

    ...
    ( LINES OF VALUE #( FOR <line> IN itab WHERE ( AnyConditionYouWant ) ( <line> ) ) )
    ...
    

    Of course, there are many other possibilities by nesting other constructor expressions (REDUCE, CORRESPONDING, etc.)