How to fetch the last record from a database table?
How to fetch the last record from a database table?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Deepak VermaPosted Mar 9, 2011, 4:20 AM
Use the following query -
SELECT * FROM Table_Name WHERE ROWID IN (SELECT MAX(ROWID) FROM Table_Name);
Note : I execute it in Oracle 10g only. This query will give you exactly the last record in a table even you arranged records in any order.
Shalini JunejaPosted Mar 9, 2011, 3:17 AM
If you want to fetch the last inserted inserted in a TABLE in a DATABASE that has an IDENTITY column named ID, you could use the following:
SELECT *
FROM TABLE
WHERE ID = IDENT_CURRENT('TABLE')
or
SELECT *
FROM TABLE
WHERE ID = (SELECT MAX(ID) FROM TABLE)
I hope this helps u in fatching last record
thanks
Shalini juneja
ShankeyPosted Mar 8, 2011, 5:02 AM
Use CTE feature of SQL server as shown below to fetch last record of the table
with WITHROWID AS(
SELECT ROW_NUMBER() OVER(ORDER BY [StudentId]) AS RowID,[StudentId]
,[StudentName]
,[StudentAddress]
FROM [dbo].[Student])
SELECT TOP 1 * FROM WITHROWID ORDER BY RowID DESC
Hope it will help u and give you right direction .
Thanks,
Shankey