Skip to Content

Can I put Read inside loop?

Hi Folks

Can I put Read inside loop?

Performance wise is it acceptable?

Regards,

Sreeram

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

6 Answers

  • author's profile photo
    Former Member
    Posted on Sep 28, 2007 at 05:29 AM

    OF COURSE U CAN PUT READ IN THE LOOP.

    for performance...

    be sure for follwing things...

    sort the table with the key fields on which u r putting read.

    then write the following code.

    READ TABLE IT INTO WA WITH KEY XYZ = ' ' BINARY SEARCH.

    REWARD POINTS PLEASE...

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Posted on Sep 28, 2007 at 05:33 AM

    ya sree ram.. it is absoulatelty fine to put read inside the loop..

    just avoid nested loop for better performance..

    reward if useful..

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Posted on Sep 28, 2007 at 05:41 AM

    well ofcourse you can use it, however as far as performance is concerned, its better to use "FOR ALL ENTRIES".

    Please reward the helpful entries.

    Regards,

    Raman.

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Posted on Sep 28, 2007 at 08:57 AM

    sort itab2 by field.

    LOOP at itab1.

    itab_final-field2 = itab1-field2.

    read table itab2 with key field = itab1-field binary search.

    if sy-subrc = 0.

    itab_final-field1 = itab2-field1.

    endif.

    append itab_final.

    clear itab_final.

    endloop.

    This is how generally we populate final internal table with data from more than one internal tables. Performance wise this is better.

    Sort and reading with binary search is effective.

    And using read in loop is effective that loop inside a loop.

    Regards

    Vasu

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Posted on Sep 28, 2007 at 09:05 AM

    Hi

    there is no problem to put READ statement inside the LOOP

    its a better way to put

    to avoid SELECT in a LOOP

    see this example

    i had used that in my program

    in the performance point of view it is a good method

    LOOP AT IT_SOBID INTO WA_SOBID." where otype eq s_otype and objid eq s_objid.
    
        READ TABLE IT_HRP1026 WITH KEY OBJID = WA_SOBID-SOBID OTYPE = WA_SOBID-SCLAS INTO WA_HRP1026.
        IF SY-SUBRC EQ 0.
          READ TABLE IT_HRP1000 WITH KEY OBJID = WA_SOBID-SOBID INTO WA_HRP1000.
    
          WA_OUTPUT-OBJID = WA_HRP1026-OBJID.
          WA_OUTPUT-BEGDA = WA_SOBID-BEGDA.
          WA_OUTPUT-ENDDA = WA_SOBID-ENDDA.
          WA_OUTPUT-AEDTM = WA_HRP1026-AEDTM.
          WA_OUTPUT-UNAME = WA_HRP1026-UNAME.
          WA_OUTPUT-STEXT = WA_HRP1000-STEXT.
    
          READ TABLE IT_REASON WITH KEY CANCR = WA_HRP1026-CANCR INTO WA_REASON.
    
          WA_OUTPUT-CANCRT = WA_REASON-CANCRT.
          CLEAR WA_REASON-CANCRT.
    
          READ TABLE IT_LOCATION1 WITH KEY OBJID = WA_HRP1026-OBJID INTO WA_LOCATION1..
    
          READ TABLE IT_LSTEXT WITH KEY OBJID = WA_LOCATION1-SOBID OTYPE = 'F' INTO WA_LSTEXT.
    
                 WA_OUTPUT-LSTEXT = WA_LSTEXT-LSTEXT.
                 CLEAR WA_LSTEXT-LSTEXT.
    
    
          APPEND WA_OUTPUT TO IT_OUTPUT.
          CLEAR WA_OUTPUT.
          CLEAR WA_OUTPUT-CANCRT.
        ENDIF.
      ENDLOOP.

    <b>Reward if usefull</b>

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Posted on Sep 28, 2007 at 09:07 AM

    hi,,

    s u can ..

    performance vise is best too...

    regards,

    baskaran

    Add comment
    10|10000 characters needed characters exceeded