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 do you get the last record out of a table?

Former Member
0 Kudos

Hello,

It's 'Probie' again

How do you get the last record out of a table without knowing anything about the entry?

Thanks.

1 ACCEPTED SOLUTION

JozsefSzikszai
Active Contributor
0 Kudos

is it an interview question?

if it is an internal table, then determine the number of lines of the table (DESCRIBE ... ) and read the last line of the table : READ TABLE ... INDEX n (the number you just got). Of course it works only with STANDARD and SORTED tables, not for HASHED ones

8 REPLIES 8

JozsefSzikszai
Active Contributor
0 Kudos

is it an interview question?

if it is an internal table, then determine the number of lines of the table (DESCRIBE ... ) and read the last line of the table : READ TABLE ... INDEX n (the number you just got). Of course it works only with STANDARD and SORTED tables, not for HASHED ones

former_member194613
Active Contributor
0 Kudos

seems to be an interview question. It is hardly of any practical relevance.

Siegfried

Former Member
0 Kudos

Hi,

SELECT * from Databasetable into the Internal Table.

Use Describe Statement to know the Number of Entries in the Internal Table.

Read the Internal Table with Index = Total no of Entires in the Table....

former_member585060
Active Contributor
0 Kudos

Hi,

Use DESCRIBE statement to know the number of lines in that Internal Table,

Then Use

IF sy-tabix = 'a'.

Logic.

ENDIF.

Ex:-

DATA : a TYPE i.

DESCRIBE TABLE itab LINES a.

LOOP at itab.

IF sy-tabix = 'a'.

Logic.

ENDIF.

ENDLOOP.

Where a is the variable which has the total number of records value from DESCRIBE statement.

Regards

Bala Krishna

Former Member
0 Kudos

I solved it like this:

DATA: P0000 like p0000 OCCURS 0 with header line.

SELECT *
       FROM PA0000
       INTO corresponding fields of table p0000.

DESCRIBE TABLE p0000 LINES NUMBER.

READ TABLE p0000 INDEX NUMBER.

PERNR = p0000-pernr.

Thanks for all your help.

0 Kudos

I wonder what you want here... The last record after a DB selection in this case is more or less random.

If you need the highest personal number, than you can :

SELECT MAX( pernr ) ...

(probably the highest number was created last time, but it also depends on the number ranges)

former_member194613
Active Contributor
0 Kudos

as I said, this question is absolutely useless.

If the data come from a database table, and no order or sort is used, then the end in the table in no defined order.

Any line could be the solution!

So fastest solution performance-wise is a select * dbtab (no WHERE condition)!

Siegfried

Former Member
0 Kudos

Hi,

The Simplest and shortest way is to read the values into an Internal table.

then Simply do.

Immediatly after the query you can write.

*Read ITAB index sy-dbcnt*

I found it to be simplest and shortest method.

regards

Vishal