让我们说我有一个分配给字段符号的标准结构。有没有一种方法可以将变量传递给字段符号以进行动态调用?
例如:
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.
parameter_name 包含在 mara 结构中可用的列名称。
答案 0 :(得分:12)
使用ASSIGN指令是可能的。 下面是一个示例:
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.
答案 1 :(得分:1)
我提供了ASSIGN
的另一个示例。您执行一次ASSIGN
,然后执行一次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.