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

Selection from Database

Former Member
0 Likes
531

Hello Everybody,

Can someone tell me how can we select last 2 records from the database with a where condition.

I am looking for the logic but not able to get it.

Thanks,

Sneha Singh.

1 ACCEPTED SOLUTION
Read only

former_member222860
Active Contributor
0 Likes
500

Sneha,

Below snippet selects the last 2 records.

data: begin of itab occurs 0,
        matnr like mara-matnr,
      end of itab.

select matnr from mara into table itab up to 2 rows order by matnr descending.

loop at itab.
  write:/ itab-matnr.
endloop.

3 REPLIES 3
Read only

agnihotro_sinha2
Active Contributor
0 Likes
500

hi,

is it essential for you to select last 2 records by using "SELECT" query on database? i am asking since it will have a performance overhead. Rather you can select all records in an internal table and delete all except last two.

Anyways i think such an operation using a SELECt is tough.

ags.

Read only

Former Member
0 Likes
500

Hi,

It wont be possible to do this with a single select statement as there is no concept of selecting based on index directly from the database. You will have to select all the records and get the last two rows. You will also have to sort the itab based on the primary keys to get the records you intend.


select * 
from 
dbtab
into itab. 

sort itab by field1 field2.

describe table itab lines v_line. 

v_indx = v_line - 1.

read table itab into wa  index v_indx.
read table itab into wa1 index v_line.

Vikranth

Read only

former_member222860
Active Contributor
0 Likes
501

Sneha,

Below snippet selects the last 2 records.

data: begin of itab occurs 0,
        matnr like mara-matnr,
      end of itab.

select matnr from mara into table itab up to 2 rows order by matnr descending.

loop at itab.
  write:/ itab-matnr.
endloop.