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: 

Using Read Statements

Former Member
0 Kudos

Hi guys,

Can anyone provide me an alternative for the following statement...

loop at it_tab into wa_tab.

select single field1

from dbtab

into l_var

where field1 eq wa_tab-field1.

endloop.

In the above statement i am accessing the database table for every loop....cn anyone suggest a code for better performance.

Thanks and regards,

Frank

6 REPLIES 6

former_member404244
Active Contributor
0 Kudos

hi,

Select single is fine in a loop..endloop..

u can do one thing use for all entries

if not it_tab[] is initial..

select field1 from dbtable into table itab

for all entries in it_tab

where field1 = it_tab-field1.

endif.

Regards,

Nagaraj

Former Member
0 Kudos

Please try this way:



if not itab1[] is initial.
   select single fld into itab2
          from tab2
          for all entries in itab1
          where fld = itab1-fld.
endif.
       
loop at itab1 into wa1.
   read table itab2 with key fld = itab1-fld.
   if sy-subrc eq 0.
      <code...>
   else.
      <code...>
   endif.
endloop.

Kind Regards

Eswar

Former Member
0 Kudos

Hi,

Try like this.

data: begin of it_tab2,

field1,

field2,

end of it_tab2.

if not it_tab[] is initial.

select field1 field2

from dbtab2 into table it_tab2 for all entries in it_tab

where field1 = it_tab-field1.

endif.

loop at it_tab into wa_tab.

read table it_tab2 into wa_tab2 with key field1 = wa_tab-field1.

if sy-subrc eq 0.

-


endif.

endloop.

Rewards points if helpful.

Regards,

Srinivas Ch

Former Member
0 Kudos

Frank,

First if you do like below

loop at it_tab into wa_tab.

select single field1

from dbtab

into l_var

where field1 eq wa_tab-field1.

endloop.

Every time it will over write your internal table l_var .Finally you will have only one value.

Do like below.

IF NOT it_tab[] IS INITIAL.

select field1, field2 ...

from dbtab

into table l_var

for all entries in it_tab

where field1 eq it_tab-field1.

ENDIF.

Don't forget to reward if useful...

Former Member
0 Kudos

Hi frank.

Try this

data : begin of lt_temp occurs 0,

field1 type dbtab-field1,

end of lt_temp.

field-symbols : <f_tab> like line of it_tab.

select field1

into table lt_temp

from dbtab

for all entries in it_tab

where field1 eq it_tab-field1.

sort lt_temp by field1.

delete adjacent duplicate from lt_temp comparing field1.

loop at it_tab assigning <f_tab>.

read table lt_temp with key field1 = <f_tab>-field1 binary search

if sy-subrc = 0.

....

endif.

endloop..

Regards,

Mohaiyuddin

Former Member
0 Kudos

Hi Frank,

In cases like the one you have asked, its always good to use 'For all Entries' concept.

First of all get all the records for which you are selecting inside the loop.

ie... select * from <dbtab> for all entries in <itab> where <db_field> = <itab-field>.

and then you can go for reading teh individual record from the selected entries in the new table.

Thsi will minimise the access to the database.

Thanks & Regards,

Basha

*Award points if found useful.