Skip to Content
author's profile photo Former Member
Former Member

select into table within loop

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

13 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 12:43 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 17, 2008 at 12:40 PM

    use APPENDING TABLE instead of INTO TABLE.

    Thomas

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 12:41 PM

    yes..

    regards,

    venkat n

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 17, 2008 at 12:41 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 12:43 PM
    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 12:43 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 12:44 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 12:44 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 12:45 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 12:46 PM

    hi,

    thank you so much.

    i use hoffman suggestion.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.