Reputation: 21
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.
Upvotes: 2
Views: 32404
Reputation: 1
The only way I could get this to work is the function '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'. Unfortunately, it didn't work for some DDIC tables, for no apparent reason. 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. They did tell me the field's value and basic type, which are NOT what the OP and me are trying to get.
ASSIGN COMPONENT
and all its variations are not helpful either. I can't do ASSIGN COMPONENT 'MANDT' OF STRUCTURE ...
, because I don't know the name of the field! 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 ...
.
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. I hope it is useful for anybody as it was useful to me.
Upvotes: 0
Reputation: 897
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
Upvotes: 3
Reputation: 27
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.
Upvotes: 1
Reputation: 18483
You'll probably need to use Runtime Type Identification (RTTI) and ASSIGN COMPONENT name OF STRUCTURE <wa> TO <bar>.
Upvotes: 1