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

getting last record

Former Member
0 Likes
1,313

Hi experts,

Is there any way to get last record from any db table?

Thanks

kaki

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,259

Hi,

report adtest.
data: index type i.

data : begin of itab occurs 0.
       include structure zdbtable.
data: end of itab.

select * from zdbtable into table itab .

Describe table itab lines index.
read table itab index index.

write index.

Regards,

Anjali

11 REPLIES 11
Read only

Former Member
0 Likes
1,260

Hi,

report adtest.
data: index type i.

data : begin of itab occurs 0.
       include structure zdbtable.
data: end of itab.

select * from zdbtable into table itab .

Describe table itab lines index.
read table itab index index.

write index.

Regards,

Anjali

Read only

Former Member
0 Likes
1,259

Hello Kaki,

As such there is no way you can get the number of records in a db table without querying it.

So, in your case, first get all the records from database into an internal table. Then using the DESCRIBE keyword find the number of records in that internal table. Then read that internal table using that total number of records as index.

Hope this would be useful.

Regards

Priya

Read only

vinod_gunaware2
Active Contributor
0 Likes
1,259

hi

U can be used MAX in the select query. i.e. Suppose Material or Sales order u want select then u can use

SELECT MAX (VBELN) .........

Hope this will be useful.

regards

vinod

Read only

Former Member
0 Likes
1,259

HI

YOU CAN USE THE CODE

<b>DESCRIBE TABLE ITAB LINES SY-TFILL.

READ TABLE ITAB INDEX SY-TFILL.</b>

TO GET THE LAST RECORD INSERTED TO A TABLE

IF USEFUL PLEASE REWARD POINTS

REGARDS

ANOOP

Read only

Former Member
0 Likes
1,259

hi,

first declare a variable of type i. say count.

data : count type i.

next, select all the rows from the database table into an internal table, so that we get the total number of rows in the table.

The total count corresponds to the last row in the table.

select * from tablename into table internaltablename.

Describe table internaltablename lines count.

read table internaltablename index count.

This will have the last row read from the internal table.

regards,

Smitha

Read only

Former Member
0 Likes
1,259

Hi kaki,

1. Is there any way to get last record from any db table?

Last in what sense ?

2. Last added to the table ?

3. If so then,

if the fields are available in the table,

ie. (add date)

then still we can get.

4. Other wise, there is not technique

by which database can provide

the last record.

(the sequence of records returned in

sql by the database,

is never the same way, i mean,

no body can guarantee that it returns

the last record, at the last)

5. eg

suppose we want to get the lastest record from

ekko table,

then based upon the field AEDAT

we can write this logic

(just copy paste in new program)

REPORT abc.

*----


DATA : aedat LIKE ekko-aedat.

DATA : ekko LIKE TABLE OF ekko WITH HEADER LINE.

*----


First get MAX / LATEST date

SELECT SINGLE MAX( aedat ) FROM

ekko INTO aedat.

*----


Then get the record

SELECT * FROM ekko

INTO TABLE ekko

WHERE aedat = aedat.

BREAK-POINT.

*----


*

*PARAMETERS : a RADIOBUTTON GROUP g1 USER-COMMAND rad,

  • b RADIOBUTTON GROUP g1.

*

*PARAMETERS : t1(10) TYPE c DEFAULT 'Hello' MODIF ID p,

  • t2(10) TYPE c DEFAULT 'Abdul' MODIF ID q.

*

*

**----


*

*INITIALIZATION.

  • a = 'X'.

*

*

**----


*

*

*AT SELECTION-SCREEN OUTPUT.

  • LOOP AT SCREEN.

  • IF a = 'X'.

  • IF screen-group1 = 'Q'.

  • screen-input = 0.

  • screen-invisible = 1.

  • MODIFY SCREEN.

  • ENDIF.

  • ENDIF.

*

*

*

*

  • IF b = 'X'.

*

  • IF screen-group1 = 'P'.

  • screen-input = 0.

  • screen-invisible = 1.

  • MODIFY SCREEN.

  • ENDIF.

  • ENDIF.

*

*

*

  • ENDLOOP.

regards,

amit m.

Read only

0 Likes
1,259

..

Read only

0 Likes
1,259

select * into corresponding fields of table wa_zgkpf from zgkpf.

(populate data of table into wa)

read table wa_zgkpf index sy-dbcnt.

(read the last record of wa)

required field is wa_zgkpf-docno.

hope this will give u idea

Read only

Former Member
0 Likes
1,259

Thanks for all replies,

points alloted.

cheers

kaki

Read only

former_member184495
Active Contributor
0 Likes
1,259

hi,

what i have done here is that

i have read the data from the table

<b>marav </b>into an internal table <b>itab</b>.

after which i have read the last index

of the internal table and displayed.

try the following logic.

*----


data: begin of itab occurs 0,

matnr like marav-matnr,

end of itab.

data: i_lines like sy-tabix.

select matnr from marav into corresponding

fields of table itab.

clear i_lines.

describe table itab lines i_lines.

read table itab index i_lines.

write:/ itab-matnr.

*----


reward if found helpful...

cheers,

Aditya.

Read only

Former Member
0 Likes
1,259

Hi,

you can try to use Select upto 1 row and use decending in select statment.

sample code:

data: wa_matnr like mara.

select * into wa_matnr

from mara up to 1 rows

order by matnr descending.

endselect.

- Niranjan