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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With