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

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Shiva,

You need to check for Sy-Subrc while using READ TABLE command...and more over, I assume itab_result must have a matnr which you need to check in READ TABLE...assuming you have matnr in itab_result too...then

sort it_matnr by matnr.

loop at itab_result.

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

<b>if sy-subrc eq 0.

.....

endif.</b>

endloop.

Regards,

Vivek

8 REPLIES 8

Former Member
0 Kudos

Hi Shiva,

You need to check for Sy-Subrc while using READ TABLE command...and more over, I assume itab_result must have a matnr which you need to check in READ TABLE...assuming you have matnr in itab_result too...then

sort it_matnr by matnr.

loop at itab_result.

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

<b>if sy-subrc eq 0.

.....

endif.</b>

endloop.

Regards,

Vivek

Former Member
0 Kudos

Shiva,

Shouldn't the statement be

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

Regards

Aneesh.

Former Member
0 Kudos

You have forgotten to write the component name (MATNR) at the end of internal table itab_result in your read statement. You were asking SAP to compare it_matnr-matnr with the entire internal table header line of internal table itab_result. If internal table itab_result has more components than MATNR your read will fail. Look at the change you need to make in bold.

loop at itab_result.

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

.....

endloop.

former_member1345686
Active Participant
0 Kudos

As you said :

" ... finally I used loop at where matnr = it_result-matnr ... "

is successful,

then your code is supposed to be like this :

sort it_matnr by matnr.

loop at itab_result.

read table it_matnr with key

matnr = itab_result-matnr binary search.

.....

endloop.

yes there is a lack of fieldname in your current code "-matnr"

rgds,

Tuwuh Sih Winedya

Former Member
0 Kudos

Hi Shiva,

Remember one thing while using READ statement and in that internal table having more than one records for with key condition it read the first record of similar records in IT. suppose in your code it_matnr having 5 records having same material number then READ statement fetch first record.

sort it_matnr by matnr.

<b>delete adjacent duplicates from it_matnr</b>.

loop at itab_result.

<b>clear it_matnr.</b> " you should clear the IT before READ statement.

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

if sy-subrc eq 0. " use the sy-subrc check for every READ statement

.....

.....

endif.

endloop.

Reward with points if helpful.

<b>Regards,</b>

Vijay

Former Member
0 Kudos

first of all use

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

n for improving performance u may add

<b>transporting no fields</b> at the end of read stmt

Former Member
0 Kudos

Hi,

u have missed the field name <b>MATNR</b> while reading the table

sort it_matnr by matnr.

loop at itab_result.

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

....

endloop.

Former Member
0 Kudos

Hi Shiva,

Use this.

sort it_matnr by matnr.

loop at itab_result.

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

.....

endloop.

Reward if Useful.

Regards,

Chitra