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: 

Hello experts read table statement is not working properly

Former Member
0 Kudos

Hello Experts.

my code is like this.

sort it_matnr by matnr.

loop at itab_result.

read table it_matnr with key matnr = itab_result-matnr binary search.

.....

endloop.

there are nearly 2000 records in it_matnr . The records which satisfy the above condition is there here in this internal table. But it is unable to read the record which matches the condition. One thing is that , there are more than one record with the same material. ie mblnr is different but material is the same. In it_matnr table i have only 1 field ie matnr ,so i need to compare only with the matnr.I also tried by sorting it_result by matnr but of no use. Then finally I used loop at where matnr = it_result-matnr , it is working fine. But due to performance reasons we should not use this. so please tell me what to do and correct the code.

8 REPLIES 8

Former Member
0 Kudos

How you are sorting the itab_result.

sort it_matnr by matnr. " -> comment the sort

loop at itab_result.

read table it_matnr with key matnr = itab_result-matnr. " Comment binary search.

.....

endloop.

Now see the results.

Thanks

Seshu

Former Member
0 Kudos

Try this:

SORT it_matnr BY matnr.

LOOP AT itab_result.
  READ TABLE it_matnr
    WITH KEY matnr = itab_result-matnr
    BINARY SEARCH.
* Process record
  LOOP AT it_matnr FROM sy-tabix.
    IF it_matnr <> itab_result-matnr.
      EXIT.
    ENDIF.
* Process record
  ENDLOOP.

ENDLOOP.

Rob

Former Member
0 Kudos

Hi

i am sending you a sample code where i had writen for my program

there i did't faced any performance problem

sort it_sobid by objid.

  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.

      APPEND WA_OUTPUT TO IT_OUTPUT.
      CLEAR WA_OUTPUT.
        ENDIF.
  ENDLOOP.

<b>Reward if usefull</b>

Former Member
0 Kudos

Hi

if the multiple entries are there, then it will change all the values which macthes the matnr. look for some different field which changes each time and has different value

Regards

Shiva

Former Member
0 Kudos

hi,

read statement suppress duplicate records..it reads only the first record when

there are duplicate records.

hence you have to use nested loops.

ie. loop at itab_result.

loop at it_matnr.

endloop.

endloop.

if performance is taken into accout write where condition

i.e., loop at itab_result.

loop at it_matnr where matnr = itab_result-matnr.

endloop.

endloop.

regards

sree

Former Member
0 Kudos

Hi Shiva,

Try this.

data : wa_itab_result like line of itab_result.

sort it_matnr by matnr.

loop at itab_result into <b>wa_itab_result</b>.

read table it_matnr with key matnr = <b>wa_itab_result-</b>matnr binary search.

.....

endloop.

Reward if Useful.

Regards,

Chitra

Former Member
0 Kudos

Hi,

Try with this following code,

sort it_matnr by matnr.

loop at itab_result.

<b>Clear : it_matnr</b> " u should use always clear stat before READ, suppose if read statement fails it contains the previous read data.

read table it_matnr with key matnr = itab_result-matnr binary search.

.....

endloop

if u using version above 4.7e use the following code.

data : wa_itab_result like line of itab_result.

sort it_matnr by matnr.

loop at itab_result into wa_itab_result.

<b>Clear : it_matnr</b> " u should use always clear stat before READ, suppose if read statement fails it contains the previous read data.

read table it_matnr with key matnr = wa_itab_result-matnr binary search.

.....

clear : itab_matnr.

endloop.

<b>Reward with points if useful.</b>

Regards,

Vijay

Former Member
0 Kudos

1. Make sure that the table IT_MATNR is not getting updated inside the loop. Because it will then destroy the sorting.

2. Secondly, if there are multiple records in IT_MATNR for the same material then it will search for the first record encountered in binary algo. So it is advisable that you may provide further filter criteria if possible. check if ITAB_RESULT & IT_MATNR have any other matching field that can be put in filter criteria.

3.Thirdly, check if your requirement can be achieved by STABLE SORT.

<b>SORT <itab> ... STABLE BY <f1>[ASCENDING/DESCENDING].</b>

It will allows you to perform a stable sort, that is, the relative sequence of lines that are unchanged by the sort is not changed. If you do not use the STABLE option, the sort sequence is not preserved. If you sort a table several times by the same key, the sequence of the table entries will change in each sort. However, a stable sort takes longer than an unstable sort.

4. Lastly, you can use parallel cursor technique of multiple loops which is given below:

LOOP AT itab_result.

READ TABLE it_matnr

WITH KEY matnr = itab_result-matnr

BINARY SEARCH.

  • Process record

LOOP AT it_matnr FROM sy-tabix.

IF it_matnr <> itab_result-matnr.

EXIT.

ENDIF.

  • Process record based on yuor condition

ENDLOOP.

ENDLOOP.