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

Alternative to Nested Loops ???

Former Member
0 Likes
3,698

Guys

I just wanted to know if there is any alternative to LOOP inside LOOP (nested loops) if there is no primary key available ?

Help would be appreciated.

Thank You

Guys

I just wanted to know if there is any alternative to LOOP inside LOOP (nested loops) if there is no primary key available ?

Help would be appreciated.

Thank You

15 REPLIES 15
Read only

Former Member
0 Likes
2,807

What is the exact scenario ??

One alternative for a nested loop case is:

loop at vbak.

loop at vbap where vbeln = vbak-vbeln.

......

endloop.

endloop.

Instead of this, we can write..

loop at vbap.

read table vbak with key vbeln = vbap-vbeln.

endloop.

Read only

Former Member
0 Likes
2,807

Hi ,

i dont think you need a primary key

to use LOOP..

can you be more clear..

regards

satesh

Read only

Former Member
0 Likes
2,807

u can use read statement inside loop.

sort itab2 by fld1.

loop at itab1.

  read table itab2 with key fld1 = itab1-fld1 binary search.
  if sy-subrc eq 0.
  endif.

endloop.

Read only

Former Member
0 Likes
2,807

Loop at itab.

read table itab1 with key x = itab-x binary search.

if sy-subrc = 0.

---

endif.

endloop.

Read only

Former Member
0 Likes
2,807

Depends on what exactly is the processing you want to do. Could you please explain?

Regards,

Ravi

Read only

Former Member
0 Likes
2,807

Hello Reddy,

What do you mean by <i>Primary Key</i> here ? Are you saying that the two internal tables have got no common fields at all ?

If that's the case, then you have no other alternative. By the way, it would really help if you colud state your problem with much more detail...

Regards,

Kinshuk Saxena.

Read only

0 Likes
2,807

Thanks everyone for respondng.

Yes Kinshuk,

I do not have any common field in the two internal tables. Had there been a common field I would have used the read statement (as suggested by others, and I already knew that)

Best Regards

Read only

0 Likes
2,807

If you don't have any common field, then on what basis are you building a relationship between the two. How will know if you have to do a loop at itab1 first and then itab2 or vice-versa? May be it will help if you post your current loop statements along with the internal table definitions.

Read only

0 Likes
2,807

Hi,

This is My Code

LOOP AT I_PRGLIST_FIN.

W_PROGNAME = I_PRGLIST_FIN-NAME.

READ REPORT W_PROGNAME INTO I_PRGCODE.

DELETE I_PRGCODE WHERE LINE IS INITIAL.

DELETE I_PRGCODE WHERE LINE+0(1) = '*'.

DELETE I_PRGCODE WHERE LINE CS 'REPORT'.

CLEAR : I_PRGCODE.

*Searching for the tables used in the programs

CLEAR I_DD02L_FINAL.

LOOP AT I_DD02L_FINAL.

CLEAR : I_RESULT_TAB.

SEARCH I_PRGCODE FOR I_DD02L_FINAL-TABNAME.

IF SY-SUBRC = 0.

I_RESULT_TAB-NAME = W_PROGNAME.

I_RESULT_TAB-TABNAME = I_DD02L_FINAL-TABNAME.

APPEND I_RESULT_TAB.

CLEAR I_RESULT_TAB.

ENDIF.

ENDLOOP. "I_DD02L_FINAL

ENDLOOP. "I_PRGLIST_FIN.

I_DD02L_FINAL contains all the tables (Exactly 1,38,572) in a particular server.

I_PRGLIST_FIN contains all the Z and Y programs (Exactly 3,116 ) in that server.

You can understand the use of I_RESULT_TAB.

I hope you understand the code and expecting a better solution because this piece of code is taking 18 hours (because of nested loops (remember internal tables have no common fields)) to get executed obviously in background.

Read only

0 Likes
2,807

Hello Reddy,

You are trying to find out all the tables that are used in the custom programs (beginning with Y or Z). There's a table in SAP called D010TAB which contains all the

(a). tables

(b). data-elements

(c). structures and

(d). views

used in an ABAP Program. This table is always populated during the ABAP Compile Time and is guaranteed to contain the correct and complete information.

Now, the problem just reduces to selecting the data from this table and excluding the records for structures, views and data-elements.

I have written the following program which would meet this functionality. Please have a look at this -

tables d010tab.

data : itab_main type table of d010tab with header line,
       itab_temp type table of d010tab with header line,
       itab_tabl type table of tabname with header line.
       
ranges r_tabname for d010tab-tabname.

start-of-selection.

  select * from d010tab
           into table itab_main
          where ( master like 'Y%'
             or master like 'Z%' ).
  
  itab_temp[] = itab_main[].
  
  sort itab_temp by tabname.
  
  delete adjacent duplicates from itab_temp comparing tabname.
  
  select tabname from dd02l
    into table itab_tabl
    for all entries in itab_temp
    where tabname eq itab_temp-tabname
    and tabclass eq 'TRANSP'.          
* TRANSP ----> TRANSPARENT TABLES.  
  
  r_tabname-sign = 'I'.
  r_tabname-option = 'EQ'.
  
  loop at itab_tabl.
    r_tabname-low = itab_tabl.
    append r_tabname.
  endloop.
  
  delete itab_main where not ( tabname in r_tabname ).
  
  loop at itab.
    write :  / itab-master , itab-tabname.
  endloop.

The above program when run the background has taken about 110 seconds (less than 2 minutes) for a system with 2540 ( Z* and Y* ) programs. which means that this report can also be run in the foreground.

Please do let me know if this has helped. And reward points as well...:-)

Regards,

Anand Mandalika.

Read only

0 Likes
2,807

Thanks a lot Anand.

The table name (D010TAB) has helped me a lot. Is there any table which contains the fields used in the ABAP Programs like D010TAB contains table names ????

Thank You very much once again. I will definitely reward you points ....

Read only

0 Likes
2,807

just go through transaction SE49..

ENTER THE PROGRAM NAME OR TRANSACTION NAME OR FUNCTION GROUP...

U CAN FIND OUT THE TABLE USED .

ALSO FIND OUT THE QUEY REGARDING THAT PERTICULAR TABLE...

Read only

0 Likes
2,807

Hello Reddy,

It's not easy to get the fields used in a program. I have tried it sometime back for one of my projects. You can, however, take a look at the following statements in ABAP -

1. SCAN

2. LOAD

These statements are for internal use only. So, be careful while you're using them. The documentation for these keywords is avilable in the F1 Help.

By the way, why do you need the fields information? What information about the fields do you need? Do you want to know all the fields which are declared with reference to some DDIC type?

Regards,

anand Mandalika.

Read only

Former Member
0 Likes
2,807

Hello Reddy garu,

Try using Read table statement with binary search option instead of loops. This will optimise the performance also. Do not forget to sort the internal table before using the binary search

<b>some tips:</b>

1)u can use modify itab transporting f1 f2 inside a loop if u want to modify only few fields instead of just saying modify itab.

2) if unable to avoid loops then first do a read table with key and then do a loop from sy-tabix.

If not <b>Use the inner join conditions to avoid loops</b>.

regards,

Srikanth.

Please reward points if helpful.

Read only

Former Member
0 Likes
2,807

I wrote a <a href="/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops">blog</a> on exactly this topic.

Rob