Reputation: 25413
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?
Upvotes: 3
Views: 32391
Reputation: 521
Using INSERT <ls_table> INTO TABLE <lt_table>.
is correct but the reason for the syntax errors is that you defined your field symbol as:
<lt_table> TYPE ANY TABLE,
since your program is hard coded to use a hashed table you should rather define your field symbols as
<lt_table> TYPE HASHED TABLE,
By doing this the syntax checker can give you better syntax checks since it knows the basic type of the table.
What the syntax error is telling you is that using append will only work with tables that are accessible via an index (standard and sorted) and since ANY TABLE
could possibly be hash table (only accessible by key) you cannot append to it.
Upvotes: 10
Reputation: 18483
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.
Upvotes: 7