Application Development 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: 

How to get the last row of a database table.

Former Member
0 Kudos

HI ,

I want to get record exactly from the last row of a database table.

How is that possible?

9 REPLIES 9

naveen_inuganti2
Active Contributor
0 Kudos

Hi.,

Sort the internal table after fetching the DATA in descending order!

And dislpay the first record.

Or,

Find the number of records in your internal table aftre fetching the data(describe statement)

and read the internal table with index of above number!

Or,

You can use SELECT and ENDSELECT for this..!

--Naveen Inuganti.

Former Member
0 Kudos

Hi,

Use DESCRIBE TABLE <table> LINES. --> this gives the number of records.

then READ the table using INDEX <no. of records> ..

Regards,

Leonard Chomi.

Former Member
0 Kudos

hi,

since database table is sorted by primary key.

use this

SELECT max( pernr ) FROM pa0000 INTO TABLE itab .

it gives last pernr.

Former Member
0 Kudos

Hi,

Try using count(*). This gives the total number of records. Use that index to get the last record.

Hope this helps.

Regards,

Deepthi.

JozsefSzikszai
Active Contributor
0 Kudos

First it would be great to know, what you mean by "last row of a database table". In relational DB there is no last row!

All above replies are misleading / wrong.

0 Kudos

HI,

As we are not manipulating any DB table directly. First query all records to Internal table and by using index you can find last row.

regards,

Katta

Former Member
0 Kudos

HELLO Harshit


SORT it_mesg BY key  Descending.

sort the internal table by the field name with whom you want result in descending order.

in my code ket is the name of the field and it_mesg is the name of the internal table.

Hope it will help you.

Thanks

Arun Kayal

Former Member
0 Kudos

Hi,

To fetch last record from an internal table, just do find the number of records in it and read using index.

DESCRIBE TABLE ITAB LINES L_LINES.

READ TABLE ITAB INDEX L_LINES.

You can also use LOOP .. ENDLOOP but the above method is better (performance wise).

using LOOP .. ENDLOOP.

LOOP AT ITAB.

**do nothing

ENDLOOP.

**process ITAB (Header record of ITAB).

**after ENLOOP, ITAB will have the last record of the internal table.

[here ITAB is internal table as well as header record.]

But what is the requirement?

If you are looking for the current record of an employee then you can use ENDDA = HIGH_DATE.

My advice is to review your requirement again and try to fetch only that record which you need.

Mubeen

Former Member
0 Kudos

As Eric states, there is no such thing as "last row" in relational databases. If you're looking for the MAX of a certain column use the.....MAX operator.

It makes no sense to load all rows into an itab, then sort the itab, when you can just return one row from the database containing what you want.