cancel
Showing results for 
Search instead for 
Did you mean: 

Select Statement within a Loop

Former Member
0 Kudos

Hi All,

I am currently having a problem with the optimization of my code. The code works well, but i want to know how can i avoid the use of select within a loop as shown..

let us suppose that i have 5 tables -> tab1, tab2, tab3, tab4, and tab5 having corresponding internal tables intab1.... intab5

unrelated to all of these, i am getting an internal table (intab) from a function module FM1. This internal table has fields f1,f2,f3,f4,f5 which are keys in tab1....tab5

Finally i have an output table (out_tab) which has all the fields of tab1, tab2...tab5.... corresponding workarea for this is wa_out_tab.

This is the code that i have written so far.


call function 'fm1'
importing xyztab = intab.

loop at intab into wa_intab.

clear wa_out_tab.

select * from tab1 into intab1 where f1 eq wa_intab-f1.

..
//copy all fields from intab1 into corresp. fields of wa_out_tab

select * from tab2 into intab2 where f2 eq wa_intab-f2.

...
//copy all fields from intab1 into corresp. fields of wa_out_tab

select * from tab3 into intab3 where f3 eq wa_intab-f3.

..
//copy all fields from intab1 into corresp. fields of wa_out_tab

select * from tab4 into intab4 where f4 eq wa_intab-f4.

..
//copy all fields from intab1 into corresp. fields of wa_out_tab

select * from tab5 into intab5 where f5 eq wa_intab-f5.
//copy all fields from intab1 into corresp. fields of wa_out_tab


append wa_out_tab to out_tab


endloop.



My problem is that i cannot find a way to removed the numerous select statements within the loop.

Please Help.

Best Regards,

Mazin

Accepted Solutions (0)

Answers (4)

Answers (4)

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

General questions like these do not belong in the Web Dynpro ABAP forum. This is what the main ABAP forums are for. This forum is for questions specifically related to Web Dynpro ABAP only. I am locking this thread.

Former Member
0 Kudos

You could build a range inside the loop and only perform each select statement once:


DATA: selections_f1 LIKE RANGE OF wa_intab-f1,
      selection_f1 LIKE LINE OF selections.

selection_f1-sign = 'I'.
selection_f1-option = 'EQ'.
LOOP AT intab into wa_intab.
  selection_f1-low = wa_intab-f1.
  APPEND selection_f1 TO selections_f1.
ENDLOOP.

"(same for remaining intab fields)


SELECT * FROM tab1 INTO intab1 WHERE f1 IN selections_f1.
..
//copy all fields from intab1 into corresp. fields of wa_out_tab

"(same for remaining selection statements)


append wa_out_tab to out_tab

Former Member
0 Kudos

Hi,

Fill your internal table out side the loop and use read statement inside the loop.

select * from tab1 into intab1

for all entries in intab

where f1 eq intab-f1.

select * from tab2 into intab2

for all entries in intab

where f2 eq intab-f2.

select * from tab3 into intab3

for all entries in intab

where f3 eq intab-f3.

select * from tab4 into intab4

for all entries in intab

where f4 eq intab-f4.

select * from tab5 into intab5

for all entries in intab

where f5 eq intab-f5.

loop at intab into wa_intab.

read table itab1 into wa1 with key f1 = wa_intab-f1.

//copy all fields from wa1 into corresp. fields of wa_out_tab.

//.......do as mentioned above for rest of the sections.

append wa_out_tab to out_tab

endloop.

Thanks

Vishal Kapoor

Former Member
0 Kudos

Since Table1, Table2...Table5 are unrelated..you can create a VIEW from all these tables including only the required fields. Once you have the view in database, you can have a single select on that VIEW with where conditions.

Greetings

Prashant