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

A problem with two loops.

Former Member
0 Likes
1,041

Hi, experts!

My program need to print the data when the account

number, company_code and profit_center are equals between two tables <b>(T_ITAB1, T_ITAB2)</b>.

I have two loops like this:


LOOP AT t_itab1 INTO WA_ITAB.

LOOP at T_ITAB2 WHERE  account = wa_itab-RACCT AND    
        PROFIT_CENTER = wa_itab-prctr and company_code = 
        wa_itab-rbukrs.

The T_ITAB2 have data that is key. No have repeatedly data and the relationship is one to one. But when I execute the program the information of the internal table T_ITAB2 is repeatedly many times.

How can I resolve this problem.

Thanks for the help!!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,022

Instead of looping around itab2, read it.

like this

LOOP AT t_itab1 INTO WA_ITAB.

read table T_ITAB2 into wa_itab2 with key account = wa_itab-RACCT AND PROFIT_CENTER = wa_itab-prctr and company_code = wa_itab-rbukrs.

if sy-subrc = 0.

*Do something

endif.

endloop.

REgards,

Ravi

9 REPLIES 9
Read only

Former Member
0 Likes
1,022

Hi Carlos,

You can do a read statement on T_ITAB2 to get single value & use

IF SY-SUBRC = 0.

ENDIF.

Can you tell me whether the where condition fields are the key fields.

Read only

Former Member
0 Likes
1,023

Instead of looping around itab2, read it.

like this

LOOP AT t_itab1 INTO WA_ITAB.

read table T_ITAB2 into wa_itab2 with key account = wa_itab-RACCT AND PROFIT_CENTER = wa_itab-prctr and company_code = wa_itab-rbukrs.

if sy-subrc = 0.

*Do something

endif.

endloop.

REgards,

Ravi

Read only

Former Member
0 Likes
1,022

Try the following:


SORT T_ITAB2 by account PROFIT_CENTER company_code.
LOOP AT t_itab1 INTO WA_ITAB.
 
read table T_ITAB2 with key
  account       = wa_itab-RACCT
  PROFIT_CENTER = wa_itab-prctr
  company_code  = wa_itab-rbukrs
  binary search.

ENDLOOP.

Rob

Read only

Former Member
0 Likes
1,022

Do it the other way around..

Loop at Itab2.

read table itab1 comparing keys from itab2.

if sy-subrc eq 0.

write : Itab2 field values...

endif.

EndLoop.

Read only

Former Member
0 Likes
1,022

Hi carlos,

Sort both the internal tables on key fields(same in both tables)

Now ,

<b>Loop on itab1.

clear itab2.

read table itab2 with key1 = itab1-key1....

if sy-subrc = 0.

<process>

endif.

Endloop.</b>

Regards,

Raj

Read only

former_member182371
Active Contributor
0 Likes
1,022

Hi,

it seems as if you had duplicate entries in your internal tables.

Have a look at abap sentence:

<b>DELETE ADJACENT DUPLICATE ENTRIES FROM <itab>

[COMPARING <f1> <f 2> ...

|ALL FIELDS].</b>

... before processing both loops.

Best regards.

Read only

Former Member
0 Likes
1,022

Phani Kiran Nudurupati, Yes, the where condition are the key fields.

Read only

0 Likes
1,022

Carlos,are these the Key fields for the first table also T_ITAB1.

If yes then you can go with the read statement on the T_ITAB2 with the same where condition.

Read only

Former Member
0 Likes
1,022

Even though you have unique records in T_ITAB2 for the combination of account number, company code, and profit center, the outer loop of T_ITAB1 may be having duplicate records for this combination. It is like looping at items table and within that looking at the header table with the key. Even though there is only one header record for each key, there may be several items for that header table, so obviously you will see repetetion.

May be you need to reverse the order of the loops.

Srinivas