‎2009 Oct 25 9:23 PM
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.
‎2009 Oct 26 4:42 AM
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.
‎2009 Oct 26 4:29 AM
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.
‎2009 Oct 26 4:31 AM
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
‎2009 Oct 26 4:42 AM
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.