'MODIFY TABLE of type ANY TABLE from field-symbol of type ANY

I have a generic table of type "any table" and I'm trying to modify some entries in there, in this way:

  LOOP AT ct_data INTO <fs_data>.
    ASSIGN COMPONENT 'KEY' OF STRUCTURE <fs_data> TO <fs_feld_fu_key>.
    IF <fs_feld_fu_key> IS ASSIGNED.
      IF <fs_feld_fu_key> = <fs_mci_items>-parent_key.
        ASSIGN COMPONENT 'ZZ_CHANGED_FIELD' OF STRUCTURE <fs_data>  TO <fs_feld_walzzyk>.
        IF <fs_feld_walzzyk> IS ASSIGNED.
          <fs_feld_walzzyk> = <fs_mci_items>-zz_changed_field.
          MODIFY TABLE ct_data FROM <fs_data>.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDLOOP.

Field-Symbols are all having the type "any".

Everything in this code woks just fine, BUT after this line:

MODIFY TABLE ct_data FROM <fs_data>.

I get SY_SUBRC = 4 and the table will not be modified. Does anybody have some ideas or tips? What am I doing wrong in this case?

Full code:

    DATA: rt_data TYPE REF TO data,
      ls_key  TYPE /bobf/s_frw_key,
      lt_key  TYPE /bobf/t_frw_key.
FIELD-SYMBOLS: <fs_data>         TYPE any,
               <fs_feld_fu_key>  TYPE any,
               <fs_feld_walzzyk> TYPE any.


CREATE DATA rt_data LIKE LINE OF ct_data.
ASSIGN rt_data->* TO <fs_data>.

IF <fs_data> IS ASSIGNED.
  LOOP AT ct_data INTO <fs_data>.
    ASSIGN COMPONENT 'fu_key' OF STRUCTURE <fs_data> TO <fs_feld_fu_key>.
    IF <fs_feld_fu_key> IS ASSIGNED.
      ls_key-key = <fs_feld_fu_key>.
      APPEND ls_key TO lt_key.
    ENDIF.
  ENDLOOP.
ENDIF.


/scmtms/cl_tor_helper_read=>get_tor_data(
  EXPORTING
    it_root_key          = lt_key
  IMPORTING
    et_mci_items         = DATA(lt_mci_items)
).


LOOP AT lt_mci_items ASSIGNING FIELD-SYMBOL(<fs_mci_items>).
  LOOP AT ct_data INTO <fs_data>.
    ASSIGN COMPONENT 'FU_KEY' OF STRUCTURE <fs_data> TO <fs_feld_fu_key>.
    IF <fs_feld_fu_key> IS ASSIGNED.
      IF <fs_feld_fu_key> = <fs_mci_items>-parent_key.
        ASSIGN COMPONENT 'ZZ_CHANGED_FIELD' OF STRUCTURE <fs_data>  TO <fs_feld_walzzyk>.
        IF <fs_feld_walzzyk> IS ASSIGNED.
          <fs_feld_walzzyk> = <fs_mci_items>-zz_walzzyklus.
          MODIFY TABLE ct_data FROM <fs_data>.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDLOOP.
ENDLOOP.


Solution 1:[1]

<fs_data> doesn't point to ct_data. It points to a new memory area that you create in the very beginning. Remove this and declare the field symbol inline to get a pointer to the actual output:

DATA: rt_data TYPE REF TO data,
      ls_key  TYPE /bobf/s_frw_key,
      lt_key  TYPE /bobf/t_frw_key.
FIELD-SYMBOLS: <fs_feld_fu_key>  TYPE any,
               <fs_feld_walzzyk> TYPE any.


IF <fs_data> IS ASSIGNED.
  LOOP AT ct_data ASSIGNING FIELD-SYMBOL(<fs_data>).
    ASSIGN COMPONENT 'fu_key' OF STRUCTURE <fs_data> TO <fs_feld_fu_key>.
    IF <fs_feld_fu_key> IS ASSIGNED.
      ls_key-key = <fs_feld_fu_key>.
      APPEND ls_key TO lt_key.
    ENDIF.
  ENDLOOP.
ENDIF.


/scmtms/cl_tor_helper_read=>get_tor_data(
  EXPORTING
    it_root_key          = lt_key
  IMPORTING
    et_mci_items         = DATA(lt_mci_items)
).


LOOP AT lt_mci_items ASSIGNING FIELD-SYMBOL(<fs_mci_items>).
  LOOP AT ct_data ASSIGNING <fs_data>.
    ASSIGN COMPONENT 'FU_KEY' OF STRUCTURE <fs_data> TO <fs_feld_fu_key>.
    IF <fs_feld_fu_key> IS ASSIGNED.
      IF <fs_feld_fu_key> = <fs_mci_items>-parent_key.
        ASSIGN COMPONENT 'ZZ_CHANGED_FIELD' OF STRUCTURE <fs_data>  TO <fs_feld_walzzyk>.
        IF <fs_feld_walzzyk> IS ASSIGNED.
          <fs_feld_walzzyk> = <fs_mci_items>-zz_walzzyklus.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDLOOP.
ENDLOOP.

Solution 2:[2]

<fs_data> is a pointer to a row in ct_data. You are trying to modify the table from itself. The sy-subrc = 4 simply states that there is nothing to change.

As JozsefSzikszai points out in his comment, you don't need the MODIFY TABLE at all. Simply change the values in <fs_data> directly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Florian
Solution 2