Looping through a dynamic internal table in ABAP - unkown attributes

23,983

Solution 1

Combine vwegert's and Leelaprasad Kolapalli's answers:

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

Solution 2

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'.

Hope it's helpful.

Solution 3

You'll probably need to use Runtime Type Identification (RTTI) and ASSIGN COMPONENT name OF STRUCTURE <wa> TO <bar>.

Share:
23,983
Subham
Author by

Subham

Updated on July 09, 2022

Comments

  • Subham
    Subham almost 2 years

    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:

    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. 
    
  • Subham
    Subham over 7 years
    Can you please elaborate
  • vwegert
    vwegert over 7 years
    Read the documentation I've linked to, try it out, and if you encounter a specific problem, come back with a specific question. You've not mentioned what you're trying to achieve, and I'm not going to waste my time guessing.
  • Subham
    Subham over 7 years
    apologies for the non-clarity. I would like to fetch the field names from the structure <WA> and display the same. What I am doing wrong here
  • TheG
    TheG over 7 years
    Use RTTS framework of ABAP . You have helper classes to achieve what you want . If you are still stuck get back to us with details on what you tried and where you are stuck :) If you want to do dynamic programming in ABAP, you will have to get familiar with RTTS framework for efficient programming.
  • icbytes
    icbytes over 7 years
    The direct post below Yours (OP) also links to rtti: stackoverflow.com/questions/39331010/…