András
András

Reputation: 1380

How to add new line to itab with VALUE expression

ABAP 7.40 brought us new syntax, I am still figuring it out.
I want to add a new line to the existing table lt_itab. I found a workaround by adding an empty line and figuring out the current length of the table for an update by index, but is there an easier way?

SELECT spfli~carrid, carrname, connid, cityfrom, cityto
  FROM scarr
  INNER JOIN spfli
  ON scarr~carrid = spfli~carrid
  WHERE scarr~carrid = @carrier
  ORDER BY scarr~carrid
  INTO TABLE @DATA(lt_itab).

"How can I simplify the following code part?" 
DATA(lv_idx) = lines( lt_itab ).
APPEND INITIAL LINE TO lt_itab.
lt_itab[ lv_idx + 1 ] = VALUE #( carrid    = 'UA'
                                 carrname  = 'United Airlines'
                                 connid    = 941
                                 cityfrom  = 'Frankfurt'
                                 cityto    = 'San Francisco' ).

Upvotes: 4

Views: 59430

Answers (2)

vwegert
vwegert

Reputation: 18483

It's all in the documentation:

lt_itab = VALUE #( BASE lt_itab ( carrid = ... ) ).

Upvotes: 6

Gert Beukema
Gert Beukema

Reputation: 2565

The index logic is pretty ugly, you can easily use the ASSIGNING addition to the APPEND command to get a field symbol to the newly added line. You can then use that field symbol to fill the table entry using the same VALUE construct you are using now.

Or you can do it in one statement:

APPEND VALUE #( ... ) TO lt_itab.

Upvotes: 4

Related Questions