简体   繁体   English

循环遍历 ABAP 中的动态内部表 - 未知属性

[英]Looping through a dynamic internal table in ABAP - unkown attributes

Constructed a dynamic internal table with the table name as input string from the user, how do I loop through the same?用表名作为用户的输入字符串构造了一个动态内部表,我如何循环?

Please find the MWE:请找到 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. 

Combine vwegert's and Leelaprasad Kolapalli's answers:结合 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

Refer this code:参考这个代码:

  PARAMETERS:p_table TYPE string.
  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,
                 <wa> TYPE ANY,
                 <lv_field_val> 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>.
   DO.     
      ASSIGN COMPONENT sy-index OF STRUCTURE <wa> TO <lv_field_val>.
      IF sy-subrc NE 0.     
        EXIT.  
      ENDIF.   
      WRITE: sy-index.  
      WRITE:':' ,<lv_field_val>.         "Here we can get individual field value
      skip.
   ENDDO.
   exit.  
 ENDLOOP.

if you want field names for table to use this FM 'DD_GET_FIELD_INFO'.如果您希望表的字段名称使用此 FM 'DD_GET_FIELD_INFO'。

Hope it's helpful.希望它有帮助。

The only way I could get this to work is the function ' DDIF_FIELDINFO_GET '.我可以让它工作的唯一方法是函数' DDIF_FIELDINFO_GET '。 It receives the name of a dictionary structure, table or type, and returns a list of its fields, and a lot of useful details about them, such as the field's data element, description, length and so on.它接收字典结构、表或类型的名称,并返回其字段的列表,以及有关它们的许多有用的详细信息,例如字段的数据元素、描述、长度等。 Here is a basic example:这是一个基本示例:

DATA: lt_fields_info TYPE dfies_tab.

CALL FUNCTION 'DDIF_FIELDINFO_GET'
  EXPORTING
    tabname        = 'MARA'
  TABLES
    dfies_tab      = lt_fields_info[]
  EXCEPTIONS
    not_found      = 1
    internal_error = 2
    OTHERS         = 3.

IF sy-subrc <> 0.
* Handle errors.
ENDIF.

LOOP AT lt_fields_info[] INTO ls_field_info.
  " Dynamically printing the fields' details:
  WRITE: / 'Field name: ', 
           ls_field_info-fieldname,
           'Field data element: ',
           ls_field_info-rollname,
           'Field description: ',
           ls_field_info-fieldtext.

ENDLOOP.

Leelaprasad Kolapalli (sorry, I can't find out how to tag the username in my comment) suggested using the function 'DD_GET_FIELD_INFO'. Leelaprasad Kolapalli (抱歉,我不知道如何在评论中标记用户名)建议使用函数“DD_GET_FIELD_INFO”。 Unfortunately, it didn't work for some DDIC tables, for no apparent reason.不幸的是,它对某些 DDIC 表不起作用,原因不明。 This prompted me to search Google for a similar function, and then I found the better one.这促使我在谷歌搜索类似的功能,然后我找到了更好的。 Sadly, both functions don't work with local (internal) structures, as defined in classes or includes, so I don't know how to get fields' details for them.遗憾的是,这两个函数都不适用于在类或包含中定义的本地(内部)结构,因此我不知道如何获取它们的字段详细信息。

I couldn't get any of those CL_ABAP______DESCR classes/methods to work, because they either caused a conversion error or just didn't tell me the field's name at all.我无法让任何CL_ABAP______DESCR类/方法工作,因为它们要么导致转换错误,要么根本没有告诉我字段的名称。 They did tell me the field's value and basic type, which are NOT what the OP and me are trying to get.他们确实告诉了我该字段的值和基本类型,这不是我和 OP 想要得到的。

ASSIGN COMPONENT and all its variations are not helpful either. ASSIGN COMPONENT及其所有变体也无济于事。 I can't do ASSIGN COMPONENT 'MANDT' OF STRUCTURE ... , because I don't know the name of the field!我不能做ASSIGN COMPONENT 'MANDT' OF STRUCTURE ... ,因为我不知道该字段的名称! In my specific case at work, I'm using the field's position (index) in the structure, and the command is ASSIGN COMPONENT sy-index OF STRUCTURE ... .在我工作的特定情况下,我在结构中使用字段的位置(索引),命令是ASSIGN COMPONENT sy-index OF STRUCTURE ...

I've researched the web and found about ten different posts with a whole lot of misleading answers and people who didn't really read the questions or understand them, and I tried all of them without luck, until I found the above function.我研究了网络,发现了大约 10 个不同的帖子,其中包含大量误导性的答案,以及没有真正阅读或理解问题的人,我尝试了所有这些都没有运气,直到我找到了上述功能。 I hope it is useful for anybody as it was useful to me.我希望它对任何人都有用,因为它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM