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

select latest record from a database table

Former Member
0 Likes
66,070

Hi,

How do you select the latest record from a databse table?

Regards

Nishant

9 REPLIES 9
Read only

dani_mn
Active Contributor
0 Likes
14,802

SELECT MAX (document_number) FROM <table>

Read only

Former Member
0 Likes
14,802

sort itab by field1 desending.

1st record is ur latest record.

ex--

sort itab1 by docno decending.

read table itab1 index 1.

Read only

Former Member
0 Likes
14,802

IF YOU HAVE AN INCREMENTAL NUMBER IN YOUR TABLE YOU CAN USE..

SELECT
MAX( FIELDNAME )
INTO MYVARIABLE
FROM TABLE.

Also see this link ==>

RGDS,

tm

Read only

0 Likes
14,802

Great! it works. Thank you.

Read only

Former Member
0 Likes
14,802

Unless you have a DATE and TIME field in the database table, there is no way of knowing which is the latest record. If you have those fields select all the fields that match your criteria, sort them in DESC manner of DATE and TIME and READ the first row.

Regards,

Ravi

Read only

Former Member
0 Likes
14,802

Select the record from the database table in the internal table.

Sort internal table.

READ TABLE itab INTO wa_itab INDEX 1.

You will get the latest record.

Message was edited by: mukesh kumar

Read only

Former Member
0 Likes
14,802

Hi Nishant,

You can use the option <b>ORDER BY </b> in your SELECT clause.

This is the SAP Documentation for <b>ORDER BY </b> option.

<i>Variant 1</i>

<b>...ORDER BY PRIMARY KEY</b>

<b>Effect</b>

Sorts the selected lines in ascending order by the primary key of the database table. This variant is only permitted for SELECT * ....

<i>Notes</i>

Since views do not have a primary key, specifying ORDER BY PRIMARY KEY only makes sense with database tables. If, however, you do specify ORDER BY PRIMARY KEY with a view, all fields of the view are sorted in ascending order.

<i>Variant 2</i>

<b>ORDER BY f1 ... fn</b>

<i>Effect</i>

Sorts the selected records in ascending order by the specified column references f1 ... fn. If a list is also specified in the SELECT clause, the column references f1, ..., fn must appear in this list.

By supplementing the statement with DESCENDING, you can sort in descending order using any of the fields f1, ..., fn.

The default sort sequence is ascending order, but you can make this explicit by adding the addition ASCENDING.

Consider this code this will select the latest 10 records form DB table(Note can use <b>Variant 2</b> for DB tables also.


REPORT zarun_1.


DATA : it_mara TYPE STANDARD TABLE OF mara WITH HEADER LINE.

START-OF-SELECTION.

  SELECT * FROM mara
            INTO TABLE it_mara
            <b>UP TO 10 ROWS                "No of rows you need(give 1 here)
           ORDER BY matnr DESCENDING.    "Specify the Key Fields here.</b>

  LOOP AT it_mara.

    WRITE : / it_mara-matnr,
              it_mara-mtart.


  ENDLOOP.

Regards,

Arun Sambargi.

Message was edited by: Arun Sambargi

Read only

Former Member
14,802

Hi,

As far as I know there is no way you can select latest records from a table in SAP unless date & time field are specified. Transactional data doesn't have date & time fields unlike master data.

Try with Native SQL there may be some options from database end. with in SAP I don't think it is possible but try this option come from Master record for example if you want latest record in EKPO try to come from EKKO as there you have date field.

Regards,

Karthik.k

Read only

andreas_mann3
Active Contributor
0 Likes
14,802

hi,

or look

regards Andreas