Search code examples
abapinternal-tablesrtts

Add line to dynamic internal table


I want to add a dynamic table line to a dynamic internal table.

DATA: lo_structdescr    TYPE REF TO cl_abap_structdescr,
      lo_tabledescr     TYPE REF TO cl_abap_tabledescr,
      lt_components     TYPE abap_component_tab,
      ls_component      TYPE LINE OF abap_component_tab,
      lt_keys           TYPE abap_keydescr_tab,
      lt_table          TYPE REF TO data,
      ls_table          TYPE REF TO data.

FIELD-SYMBOLS: <ls_table>  TYPE any,
               <lt_table>  TYPE ANY TABLE,
               <lv_value> TYPE any.


MOVE 'COMP1' TO ls_component-name.
ls_component-type ?= cl_abap_elemdescr=>get_string( ).
INSERT ls_component INTO TABLE lt_components.

MOVE 'COMP2' TO ls_component-name.
ls_component-type ?= cl_abap_elemdescr=>get_i( ).
INSERT ls_component INTO TABLE lt_components.

lo_structdescr ?= cl_abap_structdescr=>create( lt_components ).

CREATE DATA ls_table TYPE HANDLE lo_structdescr.
ASSIGN ls_table->* TO <ls_table>.

lo_tabledescr ?= cl_abap_tabledescr=>create( p_line_type  = lo_structdescr
                                             p_table_kind = cl_abap_tabledescr=>tablekind_hashed
                                             p_unique     = abap_true
                                             p_key        = lt_keys
                                             p_key_kind   = cl_abap_tabledescr=>keydefkind_default ).

CREATE DATA lt_table TYPE HANDLE lo_tabledescr.
ASSIGN lt_table->* TO <lt_table>.


ASSIGN COMPONENT 'COMP1' OF STRUCTURE <ls_table> TO <lv_value>.
<lv_value> = 'test'.

APPEND <ls_table> TO <lt_table>.

The last line is the problem. I get this syntax error:

You cannot use explicit or implicit index operations on tables with types "HASHED TABLE" or "ANY TABLE". "<LT_TABLE>" has the type "ANY TABLE". It is possible that the "TABLE" addition was not specified before "<LT_TABLE>".

How am I supposed to add a line to the table?


Solution

  • Use the INSERT operation:

    INSERT <ls_table> INTO TABLE <lt_table>.
    

    I hope you know what you're doing. With all that generic data handling, I doubt anyone will be able to understand what problem you're trying to solve at all.