Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find last matching result using "read table with key"

I need to find the sy-tabix of a the last entry in an internal table that matches v_key = x. I'm trying to do it with:

read table i_tab with key v_key = x

But since there are multiple entries in the table that match v_key = x, how can I make sure I get the sy-tabix of the last matching entry? I can't search by another key unfortunately.

like image 357
Suimon Avatar asked Jan 11 '19 10:01

Suimon


People also ask

How do you read the last entry in a table in SAP?

try this: data: v_lines type sy-tabix. select * from zbt_bonus into table itab. if sy-subrc eq 0. describe itab lines v_lines read table itab index v_lines into endif.

Can we use not equal in read statement ABAP?

In Read table Can I Use Not Equal to ? No, you can use Not Equal To (NE) in Read table, instead, you can loop with Where Clause and Exit after a single record is read because you will get only single entry of your key fetched with the read statement.


1 Answers

READ TABLE is for reading single lines, for more lines you have to use LOOP:

LOOP AT itab
     ASSIGNING ...
     WHERE vkey EQ x.
ENDLOOP.

Right after the LOOP sy-tabix will contain the last line, where the condition is true.

As it was pointed (see discussion below), for the best performance there has to exist a NON-UNIQUE SORTED key (either primary or secondary) for this field

like image 137
József Szikszai Avatar answered Sep 17 '22 18:09

József Szikszai