Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Fetch a table's fieldname into variable

Former Member
0 Likes
1,408

hi

I want to fetch the value of a table's field-name into another variable, can you tell me how to do this..

Something like this

LOOP AT itab INTO wa_itab.
    
    CASE ITAB-COL.
      WHEN '0001'.
        WA_RECORD-MANDT = ITAB-VALUE.
      WHEN '0002'.
        WA_RECORD-NAME2 = ITAB-VALUE.
    ENDCASE.

    AT END OF row.
      APPEND wa TO itab_sflight.
      w_col = 1.
    ENDAT.

  ENDLOOP.

In the above code if COL value is 2 then I want to fetch the second fieldname from SFLIGHT table... so my answer should be CARRID..

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,314

I am not enough sure what is your exact requirement . What can I understand from the thread that you want to know the fieldnames of the itab. Then you check the fn module

DATA : IRSTRUCINFO LIKE RSTRUCINFO OCCURS 0 WITH HEADER LINE.

CALL FUNCTION 'GET_COMPONENT_LIST'

EXPORTING

PROGRAM = 'YOUR PROG NAME IN CAPS'

FIELDNAME = 'YOUR INT TABLE NAME IN CAPS'

TABLES

COMPONENTS = IRSTRUCINFO.

8 REPLIES 8
Read only

former_member195383
Active Contributor
0 Likes
1,314

modify ur loop as below

LOOP AT itab INTO wa_itab.

CASE wa_ITAB-COL.

WHEN '0001'.

WA_RECORD-MANDT = wa_ITAB-VALUE.

WHEN '0002'.

WA_RECORD-NAME2 = wa_ITAB-VALUE.

ENDCASE.

AT END OF row.

APPEND wa_itab TO itab_sflight.

w_col = 1.

ENDAT.

ENDLOOP.

Read only

0 Likes
1,314

Hi

No problem with coding part.. I want to fetch the first field in SFLIGHT..

Read only

0 Likes
1,314

Hi,

i dont think you will be able to get the field from the Internal table like this. Internal table is a storage area which will simply take the data for the Type you have declared it for.

in your case. i would suggest you to use the first line to simply populate the Fields so that you can read them later at your will. read table index 1.

Read only

0 Likes
1,314

Hi,

you can try this:


DATA gt_sflight TYPE STANDARD TABLE OF sflight.
FIELD-SYMBOLS <gv_value> TYPE any.
FIELD-SYMBOLS <gs_sflight> type sflight.

LOOP AT gt_sflight ASSIGNING <gs_sfligt>.
  ASSIGN COMPONENT 1 OF STRUCTURE <gs_sflight> TO <gv_value>.
* [...]
ENDLOOP.

[Assigning Components of Structures to a Field Symbol|http://help.sap.com/saphelp_nw70/helpdata/EN/fc/eb3923358411d1829f0000e829fbfe/frameset.htm]

Regards Rudi

Read only

Former Member
0 Likes
1,314

LOOP AT itab INTO wa_itab.

first you need to find out column headers....

AT FIRST.

CASE wa_ITAB-COL.

WHEN '0001'.

move this value (wa_ITAB-VALUE) whrer ever u wnat.

WHEN '0002'.

move this value (wa_ITAB-VALUE) whrer ever u wnat ENDCASE.

APPEND wa_head TO i_head.

ENDAT.

CASE wa_ITAB-COL.

WHEN '0001'.

WA_RECORD-MANDT = wa_ITAB-VALUE.

WHEN '0002'.

WA_RECORD-NAME2 = wa_ITAB-VALUE.

ENDCASE.

AT END OF row.

APPEND wa TO itab_sflight.

w_col = 1.

ENDAT.

ENDLOOP.

Edited by: Raghu on Jul 18, 2008 11:05 AM

Read only

Former Member
0 Likes
1,314

Hi,

if you only want do get the name of a field in a structure, you can read it from database table dd03l. Alternatively, you could use the class cl_abap_struct_descr.

In case that you want to have a pointer to field number x of a structure, use the command:

assign component x of structure y to <fs>.

Regards,

Thomas

Read only

Former Member
0 Likes
1,315

I am not enough sure what is your exact requirement . What can I understand from the thread that you want to know the fieldnames of the itab. Then you check the fn module

DATA : IRSTRUCINFO LIKE RSTRUCINFO OCCURS 0 WITH HEADER LINE.

CALL FUNCTION 'GET_COMPONENT_LIST'

EXPORTING

PROGRAM = 'YOUR PROG NAME IN CAPS'

FIELDNAME = 'YOUR INT TABLE NAME IN CAPS'

TABLES

COMPONENTS = IRSTRUCINFO.

Read only

Former Member
0 Likes
1,314

LOOP AT itab INTO wa_itab.

SELECT SINGLE fieldname FROM dd03l INTO w_fieldname

WHERE tabname = 'SFLIGHT'

AND position = w_col.

CONCATENATE 'WA_SFLIGHT' w_fieldname INTO string

SEPARATED BY '-'.

ASSIGN (string) TO <fname>.

<fname> = wa_itab-value.

w_col = w_col + 1.

AT END OF row.

APPEND wa_sflight TO itab_sflight.

w_col = '2'.

ENDAT.

ENDLOOP.