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: 

loop inside loop

Former Member
0 Kudos

hi,

i have 2 internal tables (for ex it1 and it2).....it1 has 10 records and 9 fields and it2 has 6 records which has 1 field..........i.e i hav to display 10 fields in my output(i.e 9 from it1 and 1 from it2)........for 1st record in it1 i have 3 records in it2 and for 6th record in it1 i have 3 records in it2.....so i hav to get all 10 records from it1 and 6 from it2.........i.e totally i have to get 14 records in my output.......

now the issue is on wich table i have to put loop ad how to fill the final internal table....

4 REPLIES 4

Former Member
0 Kudos

Hey..

try this..

declare an internal table itab3 with fields of it1+it2 .

data flag type c.

Loop at it1.

move-corresponding it1 to itab3.

Loop at it2 where ( condition comparing a field of it1 ).

move it2 -f1 to itab3-f10.

append itab 3.

clear itab3.

flag = X.

clear: it2.

endloop.

if flag NE X.

append itab 3.

clear itab3.

endif.

clear it1,

endloop.

Regards,

Krishna Chaitanya G

Former Member
0 Kudos

you simply need to loop at it1, then loop at it2 checking conditions and update it3

data: check type char1.
loop at it1.
" it1 data to it3.
.
loop at it2 <your condition comparing the field from it1>.
clear check.
" it2 data to it3
append it3. " Here you need to update if entry exist in it2.
check = 'X'.
endloop.
if check ne 'X'.
append it3. " Here you need to update if entry does not exist in it2.
endif.
endloop.

Regards,

Lalit Mohan Gupta.

Former Member
0 Kudos

HI,

LOOP AT it1.
  LOOP AT it2 WHERE <field> = <it1-field>  AND/OR <field> = <it1-field> .
    "  WRITE all the 10 fields (9 from it1 and 1 from it2)
  ENDLOOP.
  IF sy-subrc NE 0.
    " write 9 fields from it1 and there is no correponding entry in it2 so 10 field will be blank
  ENDIF.
ENDLOOP.

Former Member
0 Kudos

HI,

Both tables should have a common field (IT1 and IT2).

Sort both tables on the field.

sort IT1 on FLD1(assuming fld1 is available in both tables)

sort IT2 on FLD1.

Then,

Loop through IT1 using work area (WA1) and read the second table IT2 using binary search with key as FLD1 = WA1-FLD1.

if read is success, then move the fields from IT1 and IT2 to IT3 using work area.

Before endloop of IT1 append the work area3 to IT3.

Hope this will solve your problem

Best wishes,

Sai Prasad