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

Sorting by various keys internal table

Former Member
0 Likes
1,975

Hi All ,

There is a small confusion i am having in my mind so wanted to clear it

In my internal table i have 4 fields

1. Material no.

2.material plant no.

3.sold to party

4.ship to party

I need to read this internal table different times in different places based on various keys as like

Read itab with key material no BINARY SEARCH.

Read itab with key material plant no. BINARY SEARCH.

Read itab with key sold to party BINARY SEARCH.

Read itab with key ship to party BINARY SEARCH.

So my questions is shld i sort the table by each key sepertly before reading it

like this

sort itab by material no

Read itab with key material no BINARY SEARCH.

sort itab by material plant no

Read itab with key material plant no. BINARY SEARCH.

sort itab by sold to party

Read itab with key sold to party BINARY SEARCH.

sort itab by ship to party

Read itab with key ship to party BINARY SEARCH.

Or i can once sort table by material no material plant no sold to party ship to party

Please explain me how best this can be done and why?

Regards

Bhanu

15 REPLIES 15
Read only

Former Member
0 Likes
1,929

If your read statements are inside a same loop then DO NOT USE BINARY SEARCH.

If it is as different places then you have to sort it prior to corresponding read statements. But in these cases, you should be very cautious while reading data with binary search as if the sorting is not proper, you will get abrupt results.

Do not go for BINARY SEARCH in such cases.

Read only

rainer_hbenthal
Active Contributor
0 Likes
1,929

If you use binary search you must sort the table, otherwise the results are unpredictable.

Read only

Former Member
0 Likes
1,929

Dear Bhanu,

As you want to read a internal table with different variable field, then you can sort your internal table with hierarchy and avoid 'Binary Search ' to read the record.

Hope this will solve your problem. Feel free to ask if you have still any doubt.

Regards,

Vijay

Read only

0 Likes
1,929

i am using this read statement inside a loop which is unavoidable so for sure i should not use sort inside

loop .

sort your internal table with hierarchy ??Can you explain a little bit more?

Read only

0 Likes
1,929

Dear Bhanu,

what I mean sort your internal table before loop with say Material no material plant no sold to party ship to party.

so that your internal table will be in sequence. It will become easy to read.

Feel free to ask still you have any doubt.

Regards,

Vijay

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,929

Hi,

Since the condition changes each time, it is good to sort each time based on the read condition and then read using binary search.

Read only

Former Member
0 Likes
1,929

Hi,

Why is it required for you to write 4 read statements... can you do it with one read statement....

Regards,

Siddarth

Read only

0 Likes
1,929

Why is it required for you to write 4 read statements... can you do it with one read statement....

Its like i have to read the internal table on different conditions .

Say if

WHEN 'PROGRAM ID 'ABC ON SELECTION SCREEN '

READ ITAB BY MATERIAL NO.

WHEN PROGRAM ID 'XYZ'

I HAVE TO READ ON PLANT KEY

Read only

0 Likes
1,929

then probably its better not to use binary search... it will lead on very high performance issue if you do it, because you will have to sort the table again and again and then give a read on it.... so its better just give a read statement to the table removing binary search.....

Regards,

Siddarth

Read only

Former Member
0 Likes
1,929

Hi,

sort itab by material no

Read itab with key material no .

sort itab by material plant no

Read itab with key material plant no.

sort itab by sold to party

Read itab with key sold to party .

sort itab by ship to party

Read itab with key ship to party.

Regards,

munibabu.k

Read only

vadrevu_kk
Participant
0 Likes
1,929

Hi,

Instead of using the same internal table, copy the table into 4 internal tables and sort it according to the key. Then you can use bianry search.

Regards,

Kiran.

Read only

ThomasZloch
Active Contributor
0 Likes
1,929

Do not sort before each read inside a loop, the cost of each sort would far outweigh the binary search effect.

I think Kiran's idea is OK, if the table is not too large, copy it 4 times and sort once by each key.

Look forward to internal tables with secondary indexes, will be in the next Netweaver release.

Thomas

correction: you need 3 tables, mat# and mat# plant# can be handled with the same table. this implies that just mat# is not unique, are you sure a read is sufficient, don't you need a loop here?

Read only

0 Likes
1,929

When storing the table three times or four times in the report and if the only purpose of those tables is to be read for ceratin values, i would prefer hashed tables. They are much more performant rather than sorted tables with binary search.

Read only

0 Likes
1,929

Yes, if the full key is used for access. If only a part of the key is used (from left to right of course), then sorted tables are the best option.

Thomas

Read only

Former Member
0 Likes
1,929

your task is a bit strange and I guess the reason was giving by you:

> WHEN 'PROGRAM ID 'ABC ON SELECTION SCREEN '

> READ ITAB BY MATERIAL NO.

This looks to me as if you have to use onyl one read type per execution, depending to the

selection.

So it should be enough to sort once also depending on the selection.

To be on the safe side, it would better to split the whole program in different branches, such that

only one branch is used in one execution. Inside the branch there is the sort, the loop and the

respective read binary search inside.

Otherwise, if you really have to read in one program execution with 4 different keys inside one loop.

Then you must prepare 4 identical tables and sort them outside of the loop and use for each

read binary search the respectivly sorted table.

=> and please update to the new Netweaver release. 7.0 EhP 2 as soon as possible, there you can define different secondary keys, which solves your problem.

Siegfried