How to add a new row into a nested internal table?

1.6k views Asked by At
DATA: BEGIN OF line,
        CUOBJ TYPE CUOBJ,
        tab_atinn   TYPE STANDARD TABLE OF ATINN WITH DEFAULT KEY,
   END OF line.

  DATA:
    CUBOBJ_TABLE LIKE STANDARD TABLE OF line WITH DEFAULT KEY.

...

DATA(table) = CUBOBJ_TABLE[ CUOBJ = value-instance ]-tab_atinn.
      IF NOT line_exists( table[ currentatinn ] ).
        INSERT currentatinn INTO table INDEX 1.
      ENDIF.

I'm trying to add a new row into CUOBJ_TABLE [..]-tab_atinn. The table variable will have a new row after executing the code but the CUBOBJ_TABLE[ CUOBJ = value-instance ]-tab_atinn table won't have it.

How can I add it directly into CUBOBJ_TABLE[ CUOBJ = value-instance ]-tab_atinn using a reference or something?

2

There are 2 answers

1
Gert Beukema On

With a reference exactly, the issue is the assignment:

DATA(table) = CUBOBJ_TABLE[ CUOBJ = value-instance ]-tab_atinn.

This creates a new table with the sames values as the tab_atinn field. Then you add the entry to this new table without affecting the deep table in CUOBJ_TABLE. What you need is a reference pointing to the table you want to change so it actually updates the table and not a copy of it, something like the code below should work.

TYPES: atinn_tab TYPE STANDARD TABLE OF atinn WITH DEFAULT KEY.

DATA: BEGIN OF line,
        cuobj     TYPE cuobj,
        tab_atinn TYPE atinn_tab,
      END OF line.

DATA:
  cubobj_table   LIKE STANDARD TABLE OF line WITH DEFAULT KEY,
  value_instance TYPE                   cuobj,
  current_atinn  TYPE                   atinn.

...

data(table_ref) = REF atinn_tab( cubobj_table[ cuobj = value_instance ]-tab_atinn ).
IF NOT line_exists( table_ref->*[ current_atinn ] ).
  INSERT current_atinn INTO table_ref->* INDEX 1.
ENDIF.
0
Suncatcher On

Just for checking the existence you don't need an auxiliary variable

IF NOT line_exists( cubobj_table[ cuobj = 'VAL' ]-tab_atinn[ '0000000020' ] ).

ENDIF.

But for adding a line you need

READ TABLE cubobj_table WITH KEY cuobj = 'VAL' ASSIGNING FIELD-SYMBOL(<fs_cubobj>).
INSERT atinn INTO <fs_cubobj>-tab_atinn INDEX 1.

because INSERT doesn't allow putting table expressions in the itab_position.