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

Sort before Read with Binary

Former Member
0 Likes
1,407

We have just updated our hardware to 64 bit. Now all of our programs that do a read with Binary search are failing that DON'T have a sort on them. Is anyone aware of any issues with this, and , should there always be a sort done before a read with Binary search?

Thanks

Mike

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,204

I think it just assumes the data is sorted correctly. If not it should just give a non-zero return code. What sort of failure are you getting?

And what release?

Rob

Message was edited by: Rob Burbank

6 REPLIES 6
Read only

Former Member
0 Likes
1,205

I think it just assumes the data is sorted correctly. If not it should just give a non-zero return code. What sort of failure are you getting?

And what release?

Rob

Message was edited by: Rob Burbank

Read only

0 Likes
1,204

I am getting a sy-subrc = 4. The record I am trying to read is in the internal table, in debug, if I view the internal table it looks sorted. But it fails the read. When I put a sort statement in front of the read, sorting by the key I am reading, the program works.

Read only

0 Likes
1,204

Hi Michael,

PL sort the itab with key you are searching on ie

SORT <itab> BY <fld>.

READ TABLe <itab> with Key <fld> BINARY SEARCH.

SY-SUBRC = 4 means the required value was not found.

regards,

Suresh Datti

Read only

0 Likes
1,204

Well, it's odd that it just showed up after a hardware upgrade. I suppose that it's possible, that before the upgrade, the records were sorted correctly by some default routine that is not running now. The bottom line is that you're going to have to make sure the table is sorted.

Rob

Read only

former_member186741
Active Contributor
0 Likes
1,204

when you say 'looks sorted' what do you mean?

What is the structure of your table?

For binary seacrh to 'work' the base table must be in the corect order but it doesn't actually have to have achieved this orderly state via a 'sort' command. It could, for example, have got that way from a select with an order by clause.

How are you populating your table? maybe the old release always happened to fill it in sequence but for some reason no longer does.

Read only

Former Member
0 Likes
1,204

Hi Michael,

A Binary search is to be used only when the table is already sorted. When you read the SAP documentation, you will find out that the binary search simply assumes that the table you provide is already sorted.

If it is not sorted, like in your case, the search will not dump or anything. Only, it could fail.

I suggest you change the program to have the sort statement before the read statement. Or the other option is you can use a sorted table, so the contents will always be sorted based on the key you specify.

Hope this helps.

Rgds,

Prabhu.