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

Looping over fields from internal table

Former Member
0 Likes
4,279

In a FM I have to check records of two ITABs against each other. Most fields can be checked 1:1 but some must be checked according to some business logic.

I first want to check field-by-field and then I want to deal with exceptions.

Question: How can I loop over fields in an ITAB in such a manner that I can compare fields?

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
1,985

Hi, you can loop thru the columns(fields) of an internal table like so.

field-symbols: <fs>.

loop at itab.

do.

* Here you can use the field name instead of SY-INDEX
   assign component sy-index of structure itab to <fs>.
   if sy-subrc <>.
   exit.
   endif.

* Now that you have access to the field via field symbol, you can compare 
* this value.
Write:/ <fs>.

enddo.  

endloop.

Regards,

Rich Heilman

3 REPLIES 3
Read only

RichHeilman
Developer Advocate
Developer Advocate
1,986

Hi, you can loop thru the columns(fields) of an internal table like so.

field-symbols: <fs>.

loop at itab.

do.

* Here you can use the field name instead of SY-INDEX
   assign component sy-index of structure itab to <fs>.
   if sy-subrc <>.
   exit.
   endif.

* Now that you have access to the field via field symbol, you can compare 
* this value.
Write:/ <fs>.

enddo.  

endloop.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,985

Suggestion:

Loop at Itab1.

Read itab2 with key XXX = itab2-field1 using index.

Comapre things.

Endloop.

Regards,

Mohan.

*Reward if helpful*

Read only

Former Member
0 Likes
1,985

Hi Jans,

Consider that you have two internal tables ITAB1 & ITAB2.

If your table ITAB2 has got multiple entries for the same set of values for ITAB1 then you can use as under:

<b>LOOP AT ITAB1.

LOOP AT ITAB2 where <condition> EQ <ITAB1-->'Condition'>

ENDLOOP.

ENDLOOP.</b>

If ITAB2 has just got one corresponding entry for the value in ITAB1, then you can use the read statement as under:

<b>LOOP AT ITAB1.

READ TABLE ITAB2 WITH KEY <field for comaprison> EQ <>

IF SY-SUBRC EQ 0.

ELSE.

<You can treat your exception conditions here>

ENDIF.</b>

Regards,

Chetan.

PS: Reward points if this helps.