Application Development and Automation 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: 
Read only

help needed with read statment

Former Member
0 Likes
510

Hi ,

LOOP AT itab.

READ TABLE itab1 WITH KEY num = itab-NO BINARY SEARCH.

IF SY-SUBRC NE 0.

DELETE ITAB.

ENDIF.

ENDLOOP.

EVEN THOUGH THE RECORD EXIST IN ITAB1 FOR ITAB-NO SOMETIMES ITS RETURNING A SY-SUBRC NE 0.

I thought it is because of binarch search and my data in itab1 is not in a sorted order.

does that causes sy-subrc to be non-zero even though the records exist in internal table itab1

Thanks

4 REPLIES 4
Read only

Former Member
0 Likes
490

Yes, it does. You must sort when you are reading a table with BINARY SEARCH.

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
490

Make sure that you sort the internal table being read before the LOOP. Sort it in the order of which you are accessing it. In this case, sort by NUM. If you remove the BINARY SEARCH extension, it will work without sorting, but will be a little slower.

Regards,

RIch Heilman

Read only

Former Member
0 Likes
490

Hi,

You need to sort ITAB, please try this.


SORT ITAB.

LOOP AT ITAB.
  READ TABLE ITAB1 WITH KEY NUM = ITAB-NO BINARY SEARCH.

  IF SY-SUBRC NE 0.
    DELETE ITAB.
  ENDIF.

ENDLOOP.

Regards,

Ferry Lianto

Read only

0 Likes
490

a small correction to Ferry's response.

it should be sort itab1 by .NUM.

ie the internal table must be sorted by the key field with which you READ.

~Suresh