Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically call Field Symbols

Tags:

abap

Lets say I have a standard structure assigned to a field symbol. Is there a way to pass a variable to the field symbol for dynamic calls?

Eg:

  DATA: lt_mara            TYPE STANDARD TABLE OF mara WITH HEADER LINE,
        parameter_name(10) TYPE c.

  parameter_name = 'MATNR'.

  LOOP AT lt_mara ASSIGNING FIELD-SYMBOL(<fs_lt_mara>).
    WRITE: <fs_lt_mara>-(parameter_name).
  ENDLOOP.

Where the parameter_name contains a column name that is available in the mara structure.

like image 730
Isuru Avatar asked Aug 21 '18 07:08

Isuru


2 Answers

It is possible with ASSIGN instruction. Below is an example:

DATA:   lt_mara            TYPE STANDARD TABLE OF mara WITH HEADER LINE,
        parameter_name(10) TYPE c.

parameter_name = 'MATNR'.

FIELD-SYMBOLS: <fs_lt_mara> TYPE mara,
               <fs_value>   TYPE any.

SELECT * FROM mara UP TO 10 ROWS INTO TABLE lt_mara.

LOOP AT lt_mara ASSIGNING <fs_lt_mara>.

  ASSIGN COMPONENT parameter_name OF STRUCTURE <fs_lt_mara> TO <fs_value>.
  IF sy-subrc = 0.
    WRITE: / <fs_value>.
  ENDIF.

ENDLOOP.
like image 186
st4hoo Avatar answered Nov 09 '22 22:11

st4hoo


I provide another example of ASSIGN. You do ASSIGN once and do a LOOP INTO.

DATA: lt_mara  TYPE STANDARD TABLE OF mara,
      ls_mara  TYPE mara,
      lc_matnr TYPE char5 VALUE 'MATNR'.


FIELD-SYMBOLS: <ls_mara>  TYPE mara,
               <lv_value> TYPE any.

SELECT * FROM mara UP TO 10 ROWS INTO TABLE lt_mara.

ASSIGN COMPONENT lc_matnr OF STRUCTURE ls_mara TO <lv_value>.

CHECK <lv_value> IS ASSIGNED.

LOOP AT lt_mara INTO ls_mara.

    WRITE: / <lv_value>.

ENDLOOP.
like image 35
Haojie Avatar answered Nov 10 '22 00:11

Haojie