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

select into table within loop

Former Member
0 Likes
2,424

hi,

may i know if every loop those can map records from bsad can be added into table it_temp.

loop at it_itab where umskz = 'W'.

select bukrs belnr gjahr buzei

into table it_temp

from bsad

where bukrs = it_itab-bukrs.

endloop.

thanks

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,168

I guess this way would be better:


Select BUKRS BELNR GJAHR BUZEI
INTO TABLE IT_TEMP
FROM BSAD
FOR ALL ENTRIES IN IT_TAB
WHERE BUKRS = IT_TAB-BUKRS
  AND IT_TAB-UMSKZ = 'W'.

As Chandu said, in many cases it will improve the performance and give you a clearer code.

Tips for performance and code clearing:

- Sort your internal table by the key fields used for the comparisons on "Where" statement.

- If you think your comparison key values on IT_TAB will repeat many times, create an auxiliar table with just one entry of each value.

Regards

15 REPLIES 15
Read only

ThomasZloch
Active Contributor
0 Likes
2,168

use APPENDING TABLE instead of INTO TABLE.

Thomas

Read only

Former Member
0 Likes
2,168

yes..

regards,

venkat n

Read only

Former Member
0 Likes
2,168

You can write your code like this:

loop at it_itab where umskz = 'W'.

select bukrs belnr gjahr buzei

appending table it_temp

from bsad

where bukrs = it_itab-bukrs.

endloop.

But note that this is nota good programming practice(Select within a loop).

Regards,

Ravi

Read only

Former Member
0 Likes
2,168
Select inside a loop causes performance issue. 

Instead of that 
declare itab1 similar to itab.
loop at it_itab into wa_itab.
if wa_itam-umskz = 'W'.
move wa_itab to wa_itab1.
append wa_itab1 to it_itab1.
endif. 
endloop.

select bukrs belnr gjahr buzei
into table it_temp
from bsad
for all entries in it_itab1
where bukrs = it_itab1-bukrs.

Regards

Kannaiah

Read only

Former Member
0 Likes
2,168

Hiiii...instead of using select with in loop....

use for all entries statement that would give a better performance and solve the problem ...

regards

chandu reddy

Read only

Former Member
0 Likes
2,169

I guess this way would be better:


Select BUKRS BELNR GJAHR BUZEI
INTO TABLE IT_TEMP
FROM BSAD
FOR ALL ENTRIES IN IT_TAB
WHERE BUKRS = IT_TAB-BUKRS
  AND IT_TAB-UMSKZ = 'W'.

As Chandu said, in many cases it will improve the performance and give you a clearer code.

Tips for performance and code clearing:

- Sort your internal table by the key fields used for the comparisons on "Where" statement.

- If you think your comparison key values on IT_TAB will repeat many times, create an auxiliar table with just one entry of each value.

Regards

Read only

Former Member
0 Likes
2,168

hiii

you can use following type of query

SELECT carrid connid cityfrom cityto 
       FROM spfli 
       INTO (wa-carrid, wa-connid, wa-cityfrom, wa-cityto). 
  WRITE: / wa-carrid, wa-connid, wa-cityfrom, wa-cityto. 
ENDSELECT.

or else you can use

... INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE itab [PACKAGE SIZE n]

syntax for appending internal table

reward if useful

thx

twinkal

Read only

Former Member
0 Likes
2,168

hi,

once getiing data into ur internal table it_tab.

write the second select stmt using For All Entries.

ex:

select ...

if not it_tab is initial.

select ...... into it_ttab1 for all entries in it_tab where.......

Rgds.,

subash

Read only

Former Member
0 Likes
2,168

Hi,

don't use any DDL and DML statement within the loop.

anyway,

the it_temp will get last record only. if you want all the data use the below code.

loop at it_itab where umskz = 'W'.

select bukrs belnr gjahr buzei

appending table it_temp

from bsad

where bukrs = it_itab-bukrs.

endloop.

try to avoid the select statement within the loop.

Reward if found helpful.

Regards,

Boobalan Suburaj

Read only

Former Member
0 Likes
2,168

hi,

thank you so much.

i use hoffman suggestion.

Read only

Former Member
0 Likes
2,168

Instead of LOOPs, try using FOR ALL ENTRIES IN.

Doing so, you'll improve the performance.

Example:


SELECT bukrs belnr gjahr buzei
INTO TABLE it_temp
FROM bsad
FOR ALL ENTRIES IN it_itab
WHERE bukrs = it_itab-bukrs.

Regards,

Brian Gonsales

Read only

Former Member
0 Likes
2,168

hi hoffman,

actually it doesn't work.

the statement

it_itab-umskz = 'W'

is unknown to system.

thanks

Read only

0 Likes
2,168

hi,

IT_TAB1 =IT_TAB.

delete IT_TAB where umskz <> 'W'.

if you want other rec. take the copy of itab to itab1. otherwise leave it.

Select BUKRS BELNR GJAHR BUZEI

INTO TABLE IT_TEMP

FROM BSAD

FOR ALL ENTRIES IN IT_TAB

WHERE BUKRS = IT_TAB-BUKRS.

Reward if found helpful.

Regards,

Boobalan Suburaj

Edited by: Boobalan Suburaj on Jun 17, 2008 4:07 PM

Read only

0 Likes
2,168

hi,

define another internal table and copy all data into it:

itab2[] = itab[].

delete all lines where umskz is not W:

DELETE itab2 WHERE umskz NE 'W'.

Now you can do your select:

SELECT bukrs belnr gjahr buzei
INTO TABLE it_temp
FROM bsad
FOR ALL ENTRIES IN itab2
WHERE bukrs = itab2-bukrs

hope this helps

ec

Read only

Former Member
0 Likes
2,168

hi,

the problem is here

AND IT_TAB-UMSKZ = 'W'.

i guess it_tab field cannot declare here as this is for bsad field.

if we declare something like umskz = it_tab-umskz, then it is ok. so you see the different position?

Select BUKRS BELNR GJAHR BUZEI

INTO TABLE IT_TEMP

FROM BSAD

FOR ALL ENTRIES IN IT_TAB

WHERE BUKRS = IT_TAB-BUKRS

AND IT_TAB-UMSKZ = 'W'.