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

Nested LOOPs - Do you recommend?

Former Member
0 Likes
1,099

Hi,

I am using a nested LOOP in my print program that uses a form in SAPScript. Please see the code fragment:

LOOP AT it_final INTO wa_final.

LOOP AT it_mseg INTO wa_mseg.

CALL FUNCTION 'WRITE_FORM'

EXPORTING

element = 'HEAD'

type = 'TOP'

window = 'MAIN'.

CALL FUNCTION 'WRITE_FORM'

EXPORTING

element = 'INFO'

type = 'BODY'

window = 'MAIN'.

ENDLOOP.

ENDLOOP.

The internal table IT_MSEG usually contains only minimal number of lines, say less than 10. I can't find an alternative in this particular scenario. Is it gonna make the system considerably slower?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,030

Hi,

try to use where condition for your second loop base on the values from 1st internal table, may be you have 10 rec in development when coming to production system there may be a chance of getting more records.

Reward if useful.

Thanks,

Sreeram.

10 REPLIES 10
Read only

Former Member
0 Likes
1,031

Hi,

try to use where condition for your second loop base on the values from 1st internal table, may be you have 10 rec in development when coming to production system there may be a chance of getting more records.

Reward if useful.

Thanks,

Sreeram.

Read only

0 Likes
1,030

Dear Sreeram,

Data in IT_MSEG is already filtered based on input in select-options. The problem with your suggestion is, there is no direct link between IT_FINAL and IT_MSEG.

Arun.

Read only

Former Member
0 Likes
1,030

Instead of using nested Select loops it is often better to use subqueries

Check the following link:

how to avoid nested loops in this program to improve the performence

Reward points if useful.

Read only

0 Likes
1,030

Sravan,

I'm not talking about nested selects. I'm using nested "LOOP AT". In the loop, I'm not touching the database at all.

Read only

Former Member
0 Likes
1,030

Hi Arun,

IF it_final is your header level and it_mseg is your item level then I dont think you can avoid this nested loop.

But if you have to select a single entry from it_mseg, then you can do like this,

LOOP AT it_final INTO wa_final.

READ TABLE it_mseg INTO wa_mseg.

CALL FUNCTION 'WRITE_FORM'

EXPORTING

element = 'HEAD'

type = 'TOP'

window = 'MAIN'.

CALL FUNCTION 'WRITE_FORM'

EXPORTING

element = 'INFO'

type = 'BODY'

window = 'MAIN'.

ENDLOOP.

Regards

Sourabh Verma

Read only

0 Likes
1,030

Thats the problem Sourabh, It is not a single entry that i take from IT_MSEG. IT_FINAL is at header level. IT_MSEG has line items.

Read only

rodrigo_paisante3
Active Contributor
0 Likes
1,030

Hi, see this link:

/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops

Rob recomends the indexed or parallel loop...

Regards

Read only

0 Likes
1,030

Dear Rodrigo,

That blog by Rob was really worth. Thanks. Thanks to all.

Regards

Arun.

Read only

Former Member
0 Likes
1,030

For nested loops problem, go for parellel cursor technique , an eg. simmilar to ur req. header and line item can be printed/processed in the manner as given below.

SORT : it_mara BY matnr ,

it_makt BY matnr .

i = 1.

LOOP AT it_mara INTO wa_mara.

LOOP AT it_makt INTO wa_makt FROM i.

IF wa_makt-matnr <> wa_mara-matnr.

i = sy-tabix.

EXIT.

ENDIF.

write 😕 wa_mara-matnr , wa_makt-MAKTX .

ENDLOOP.

ENDLOOP.

PS: Reward Poins if Helpfull.

Regards

Naveen Gupta

Read only

0 Likes
1,030

Thanks for that Naveen. It helped.