Application Development 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: 

How can I access data whose field name is in itab?

acamli
Explorer
0 Kudos

Hi everyone,

I have an itab is name my_itab. This itab has stored a some variable names in fields0.

For example; my_itab-fields0 has fields name like my_itab2-field2.

I need a my_itab2-field2 values in loop.

I tried something but I can not access the field data.

DATA: my_itab TYPE  OCCURS 0 WITH HEADER LINE.

FIELD-SYMBOLS: <field> TYPE ANY.

DATA: i_field TYPE REF TO DATA.

DATA: lv_sum TYPE p DECIMALS 2.

lv_sum = 0.

...

LOOP AT my_itab.
ASSIGN COMPONENT my_itab-fields0 TO <field>.
lv_sum = lv_sum + <field>.
ENDLOOP.

my_itab-fields0 stored my_itab2-fields2 name.

I want to get a data my_itab2-fields2 value in loop.

Thank you anyone who helps.

1 ACCEPTED SOLUTION

acamli
Explorer
When I change the assign code it was solved my problem.
DATA: my_itab TYPE  OCCURS 0 WITH HEADER LINE.

FIELD-SYMBOLS: <field> TYPE ANY.

DATA: i_field TYPE REF TO DATA.

DATA: lv_sum TYPE p DECIMALS 2.

lv_sum = 0.

...

LOOP AT my_itab.
ASSIGN (my_itab-fields0) TO <field>.
lv_sum = lv_sum + <field>. 
ENDLOOP.
7 REPLIES 7

FredericGirod
Active Contributor

should be somethind like

loop at my_itab
reference into data(o_line_itab).
data(my_field) = conv fieldname( o_line_itab->fields0 ).
assign (my_field) into field-symbol(<my_field>).
check <my_field> is assigned and sy-subrc eq 0.
lv_sum = ..... <my_field>.
endloop.

acamli
Explorer
0 Kudos

Thanks your answer. But how can I write this? When I want to try this code block, I have many error about syntaxs.

acamli
Explorer
0 Kudos

Okey. I understand. I will solved. Thank you very much.

FredericGirod
Active Contributor
0 Kudos

Sorry I just wrote it without testing the code or using ABAP editor

acamli
Explorer
0 Kudos

No problem. I was solved with your help. Thank you so much.

FredericGirod
Active Contributor
0 Kudos

you should post your correction in answer and accept it

acamli
Explorer
When I change the assign code it was solved my problem.
DATA: my_itab TYPE  OCCURS 0 WITH HEADER LINE.

FIELD-SYMBOLS: <field> TYPE ANY.

DATA: i_field TYPE REF TO DATA.

DATA: lv_sum TYPE p DECIMALS 2.

lv_sum = 0.

...

LOOP AT my_itab.
ASSIGN (my_itab-fields0) TO <field>.
lv_sum = lv_sum + <field>. 
ENDLOOP.