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

Read table with key doubt

FabioPagoti
Active Contributor
0 Likes
2,954

Hi guys!

Please, what can i do when i use the


Read table xxxx WITH KEY tab_key = value ASSIGNING <fs>

statement and there is more than one entry with the specified selection criteria? How can i return the values to an internal table?

I need to get all the entries and i don't have the full table key.

thanks!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,450

One method is shown here:

[The Performance of Nested Loops|/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops]

Rob

18 REPLIES 18
Read only

Former Member
0 Likes
2,451

One method is shown here:

[The Performance of Nested Loops|/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops]

Rob

Read only

Former Member
0 Likes
2,450

Hi Fabio...

You cannt retrieve more than one row using READ TABLE. You have to use a LOOP - ENDLOOP statement in order to get each row of your table that fulfill your condition, and append them to your new table.

Write if you need more info.

Dimas Salazar

Caracas - Venezuela

Read only

Former Member
0 Likes
2,450

Hi Fabio,

An efficient way to do this is to have your table sorted by a specific key. Let's say I have a table with the feild material number.

You can do this:

data: lv_index like sy-tabix.

sort itab by material_number.

ready table itab with key material_number = yournumber

binary search.

if sy-subrc eq 0.

move sy-tabix to lv_index. " this holds the index of your first material record

  • now we loop at the table start at that index

loop at itab index lv_index.

  • perform your processing

  • if we come accross a different material number - exit the loop.

if itab-material_number ne yournumber.

exit.

endif.

endloop.

endif.

This way you only processes the records you are looking for.

thanks.

JB

Read only

0 Likes
2,450

But you process the first one twice. How wouild you get around that.

Rob

Read only

0 Likes
2,450

Hi,

If you are concerned about processing the first record twice. You can just add one to the index before you start looping.

thanks.

JB

Read only

0 Likes
2,450

In my example the read is only used to find the index, but you could process the first record at this time, add one to the index and then start looping.

thanks.

JB

Read only

0 Likes
2,450

thanks for the replies.

i'll test some scenarios and then return back.

At the moment i'm wondering if binary search would always return the first line in the criteria.

See you soon.

Read only

0 Likes
2,450

>

> At the moment i'm wondering if binary search would always return the first line in the criteria.

> See you soon.

No, it will not.

Read only

0 Likes
2,450

Rainer - from the help for READ with BINARY SEARCH:

> if there are several hits (because of an incompletely specified search key or because there are

> duplicate entries in the table), then the first hit in terms of the sequence of the rows is always

> returned, that is, the row with the smallest index.

Rob

Read only

0 Likes
2,450

Ok, didnt know that cause a binary search itself will not position on the first row (row with lowest index). If you have 5 rows with the same key, it will give you back the second or third index depending on the algorithm.

So in ABAP there must be an additional "read back until" to get the row with the lowest index.

Read only

0 Likes
2,450

yes.. probably is that... good to know it.

this thread is going deep, that's cool!

i already start a resolution without using the where clause.. after i'll do using that and post my results too. Then i check this thread answered.

thanks to everybody.

Read only

0 Likes
2,450

Hi guys! after a long time I'm back to post the results.

Rob Burbank was very helpful and the solution was create nested loops using read table binary search and then start the 'inside' loops from the found index.

a friend of mine told me this technique is called 'binary loop'. It has a really good performance and don't create internal tables like the where condition in the loop statement.

thanks for everybody!

Read only

Former Member
0 Likes
2,450

Hello Fabio

Unfortunately ABAP doesn't provide any means to just read multiple line from one internal table to other. So you can not use READ TABLE for your purpose.

You will have to use LOOP... ENDLOOP only. You can use the following construct:

LOOP AT XXX ASSIGNING <fs>.

CHECK <fs>-tabkey = value.

..... Process <fs>......

ENDLOOP.

I didnt suggest the WHERE clause in LOOP statement because its very non-performant. So better check your condition inside with CHECK statement. If CHECK fails, the loop will continue with the next iteration.

Thanks and Regards

Anand.

[How2SAP.com|http://www.how2sap.com/]

Read only

0 Likes
2,450

Anand - you can read every record in an internal table and never use a LOOP statement.

Why do you say that the CHECK is better than using LOOP...WHERE? If you can show an example with run time statistics, that would be great.

Rob

Read only

matt
Active Contributor
0 Likes
2,450

>

> Anand - you can read every record in an internal table and never use a LOOP statement.

>

> Why do you say that the CHECK is better than using LOOP...WHERE? If you can show an example with run time statistics, that would be great.

>

> Rob

As I understand it, WHERE is usually better performance-wise.

Read only

0 Likes
2,450

>

> As I understand it, WHERE is usually better performance-wise.

I would expect that they do basically the same thing, but I'll try to check it Monday or Tuesday.

Rob

Read only

venkat_o
Active Contributor
0 Likes
2,450

Hi Fabio, <li>READ TABLE statement does not return multiple values as you said.


   READ TABLE sflight_tab
        WITH TABLE KEY carrid = p_carrid
                       fldate = p_fldate
        ASSIGNING <sflight>.
<li>Use LOOP -ENDLOOP statement by setting WHERE clause also. It gives good results when you multiple records for the condition given .
LOOP AT sflight_tab ASSIGNING <sflight> WHERE carrid = p_carrid
                                             AND fldate = p_fldate.
   ENDLOOP.
Thanks Venkat.O

Read only

Former Member
0 Likes
2,450

OK - I ran a quick test. The results are mixed.

LOOP AT ... WHERE consistently ran in about 72% of the time as LOOP/CHECK. So it is faster.

The saving is about 0.3 msec for each hit.

Code:

REPORT ztest MESSAGE-ID 00.

DATA: start TYPE i,
      end   TYPE i,
      dif   TYPE i.

DATA: BEGIN OF itab OCCURS 0,
        f1,
      END   OF itab.

DO 100000 TIMES.
  itab-f1 = 'A'.
  APPEND itab.
  itab-f1 = 'B'.
  APPEND itab.
  itab-f1 = 'C'.
  APPEND itab.
  itab-f1 = 'D'.
  APPEND itab.
  itab-f1 = 'E'.
  APPEND itab.
ENDDO.

DO 10 TIMES.
  GET RUN TIME FIELD start.
  LOOP AT itab WHERE f1 = 'C'.
  ENDLOOP.
  GET RUN TIME FIELD end.
  dif = end - start.
  WRITE: /001 'time for LOOP...WHERE     :', dif, 'msec'.

  GET RUN TIME FIELD start.
  LOOP AT itab.
    CHECK itab-f1 = 'C'.
  ENDLOOP.
  GET RUN TIME FIELD end.
  dif = end - start.
  WRITE: /001 'time for CHECK within LOOP:', dif, 'msec'.
ENDDO.