2013 May 10 12:50 PM
I am looping and internal table, and reading the same internal table inside the loop when certain if conditions are satisfied.
The issue is the Read statement is not successful with the addition Binary search, even though is satifies all the if conditions.
It returns a sy-subrc value as 8.
When i remove the Binary search addition, the Read statement is successful with sy-subrc = 0. I have sorted the internal table before the loop.
Can anyone explain why its happeing so?
2013 May 10 4:33 PM
Define your tables as SORTED and a) you'll be using the more recent technology, introduced just 13 years ago... and b) you won't have BINARY SEARCH to add, so your problem will go away.
2013 May 10 4:50 PM
The problem is probably why u are not using the SORT command before giving the command read.
Note: The sort command should be done for the condition of the field used in the read
Example:
Puts the sort before
it_mara sort by ascending MATNR.
Read table with key it_mara into wa_mara lsmara-MATNR MATNR = binary search.
2023 Oct 26 8:15 AM
Yes, you are right. It is better to use SORT command exactly one line before the READ <ITAB> INTO <WA> WITH KEY <CONDITION> BINARY SEARCH
Reason: If the READ statement is used inside the loop, there may be chances that <ITAB> values gets added or removed and the sorting order of <ITAB> gets changed. So every time the READ statement is accessed inside the loop, the SORT statement should be accessed every time to get the successful results(SY-SUBRC = 0). Otherwise, We will get error while accessing READ statement with BINARY SEARCH(SY-SUBRC = 😎
2013 May 10 5:00 PM
Option 1.
define your internal table as sorted table
ex : lt_table type sorted table of <structure/table ref> with key -latest
option 2 :
Make sure you sorted your internal table with all the fields which you are using in read statement other wise read will fail .
2013 May 10 6:29 PM
You have to sort your internal table by the fields that you're comparing in READ statement. Place the SORT statement just before the READ TABLE.
2013 May 13 12:32 PM
2013 May 13 1:47 PM
Thanks experts, I got the solution.
The issue with my code was that, i had sorted the internal table with the specified keys in descending.
But Binary Search in the read statement works, only if the internal table is sorted with the same keys in the Read statement in Ascending order.
Thanks Raymond for your link.
2013 May 14 4:21 PM
Text removed by moderator
(And he couldn't give points as the post was unmarked as a question)
Message was edited by: Matthew Billingham
2013 May 15 7:59 AM
Hi Deepti,
Please SORT your table with KEY before you use it in READ with Binary Search.
This is the prerequisite with BINARY SEARCH, that your table must be SORTED before READ.
For example:
SORT itab by fld_xyz.
Read table itab with KEY fld_xyz = 'X'.
Hope this'll solve your issue.