1

In SAP there is a table T552A. There are several fields like TPR, TTP, FTK, VAR, KNF as per day of a month such as TPR01, TPR02, etc.

In a loop I would like to access the said fields by determining the table field dynamically instead of hard coding of field name, like below:

  DATA: ld_begda LIKE sy-datum,
        ld_endda LIKE sy-datum.
  DATA: lc_day(2) TYPE c.
  DATA: lc_field(10) TYPE c.
  DATA: lc_value TYPE i.

  ld_begda = sy-datum.
  ld_endda = ld_begda + 30.

  WHILE ld_begda <= ld_endda.
    lc_day = ld_begda+6(2).
    CONCATENATE 't552a-tpr' lc_day INTO lc_field.
    lc_value = &lc_field.   " Need support at this point.
    

    ld_begda = ld_begda + 1.
  ENDWHILE.
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • 1
    Does this answer your question? [Use dynamic structure field in ABAP](https://stackoverflow.com/questions/54237862/use-dynamic-structure-field-in-abap) – Suncatcher May 19 '22 at 14:56
  • Also simple answer here: [How to dynamically call Field Symbols](https://stackoverflow.com/questions/51943257/how-to-dynamically-call-field-symbols) – Sandra Rossi May 19 '22 at 17:13

2 Answers2

1

To store the result of a dynamic field a variable is needed that can store values of arbitrary types, in ABAP this is supported through field symbols. A component of a structure (i.e. the row of the table) can then be assigned to a field symbol with ASSIGN COMPONENT:

ASSIGN COMPONENT lc_field OF STRUCTURE row_of_table TO FIELD-SYMBOL(<value>).
" work with <value> here

Recently new generic expressions were introduced (and now also support structures) which would allow you to write this (sooner or later):

 ... row_of_table-(lc_field) ...
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
1

Something like this (depending on the exact requirement):

  FIELD-SYMBOLS: <lv_tpr> TYPE tprog.  
  DATA: ls_t552a TYPE t552a.
  DATA: lv_field TYPE fieldname.

  WHILE ld_begda <= ld_endda.
    lv_field = |TPR| && ld_begda+6(2).  "Dynamic field name
    ASSIGN COMPONENT lv_field
           OF STRUCTURE ls_t552a
           TO <lv_tpr>.
    IF sy-subrc EQ 0.
      ... "<lv_tpr> has now the value of the corresponding field
    ENDIF.

    ld_begda = ld_begda + 1.
  ENDWHILE.
József Szikszai
  • 4,791
  • 3
  • 14
  • 24