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: 

Can I put Read inside loop?

sreeramkumar_madisetty
Active Contributor
0 Kudos

Hi Folks

Can I put Read inside loop?

Performance wise is it acceptable?

Regards,

Sreeram

6 REPLIES 6

Former Member
0 Kudos

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...

Former Member

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

just avoid nested loop for better performance..

reward if useful..

Former Member
0 Kudos

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.

Former Member
0 Kudos

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

Former Member
0 Kudos

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>

Former Member
0 Kudos

hi,,

s u can ..

performance vise is best too...

regards,

baskaran