gpt4 book ai didi

dynamic - 如何使用动态字段名称循环遍历动态 itab 名称?

转载 作者:行者123 更新时间:2023-12-02 04:06:14 25 4
gpt4 key购买 nike

构造了一个动态内部表,其中表名作为用户的输入字符串,如何循环遍历它?

请找到 MWE:

DATA W_TABNAME TYPE W_TABNAME.
DATA W_DREF TYPE REF TO DATA.
DATA W_WA TYPE REF TO DATA.

FIELD-SYMBOLS <ITAB> TYPE ANY TABLE.
FIELD-SYMBOLS <WA> TYPE ANY.

W_TABNAME = P_TABLE.

CREATE DATA W_DREF TYPE TABLE OF (W_TABNAME).
ASSIGN W_DREF->* TO <ITAB>.

CREATE DATA W_WA LIKE LINE OF <ITAB>.
ASSIGN W_WA->* TO <WA>.

SELECT * FROM (W_TABNAME) INTO TABLE <ITAB>.

LOOP AT <ITAB> INTO <WA>.
**WRITE:/ <WA>.** ---> how do I fetch the field name here
ENDLOOP.

最佳答案

结合 vwegert 和 Leelaprasad Kolapalli 的答案:

DATA: lro_structdescr TYPE REF TO cl_abap_structdescr,
lt_components TYPE cl_abap_structdescr=>component_table.
FIELD-SYMBOLS: <ls_comp> LIKE LINE OF lt_components.

LOOP AT itab ASSIGNING <wa>
IF lt_components IS INITIAL. "get columns' names only once.
lro_structdescr ?= cl_abap_typedescr=>describe_by_data( <wa> ).
lt_components = lro_structdescr->get_components( ).
ENDIF.

DO. "iterate all columns in the row
ASSIGN COMPONENT sy-index OF STRUCTURE <wa> TO <fs_field>.
IF sy-subrc <> 0.
EXIT.
ENDIF.

READ TABLE lt_components ASSIGNING <ls_comp> INDEX sy-index.
"field name: <ls_comp>-name.
"field value: <fs_field>.
ENDDO.
ENDLOOP

关于dynamic - 如何使用动态字段名称循环遍历动态 itab 名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39427537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com