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

selecting the last record from a database table

Former Member
0 Likes
7,539

In my ABAP Program, I have to use a select statement to retrieve the last record from the database table with the same key. In other words, the Program will get more than one hit on the database table for the selected keys and I need to retrieve values from only the last record and not the first. I know I can use an internal table to sort the records first and then retrieve the right value. But to make things easier, is there a SELECT statement keyword than I can use to do this in one single step? Thanks!

In my ABAP Program, I have to use a select statement to retrieve the last record from the database table with the same key. In other words, the Program will get more than one hit on the database table for the selected keys and I need to retrieve values from only the last record and not the first. I know I can use an internal table to sort the records first and then retrieve the right value. But to make things easier, is there a SELECT statement keyword than I can use to do this in one single step? Thanks!

5 REPLIES 5
Read only

former_member194669
Active Contributor
0 Likes
2,568

May be this way


select single * from mara
  ....
  ....
  ....
order by matnr descending.

Read only

Former Member
0 Likes
2,568

Hi,

take sy-tfill or decribe table stmt, then u will get total no of records,

then read it with index sy-tfill.

Regards

Read only

Former Member
0 Likes
2,568

Try:

SELECT * FROM (table)
  UP TO 1 ROWS
  ORDER BY (key fields) DESCENDING.

ENDSELECT.

Rob

Read only

Former Member
0 Likes
2,568

Hi,

can u say one thing u want the last record after sorting your table or u want it after just selecting the table

Regards

Read only

Former Member
0 Likes
2,568

hi,

tables:mara.

data: begin of it_mara occurs 0,

matnr like mara-matnr,

meins like mara-meins,

mtart like mara-mtart,

end of it_mara.

select-options:s_matnr for mara-matnr.

select matnr

meins

mtart

from mara

into table it_mara

where matnr in s_matnr.

if not it_mara[] is initial.

sort it_mara by matnr descending.

read table it_mara index 1.

endif.

then you get the last record of the select statement.

reward points if useful,

venkat.