Click here to Skip to main content
16,012,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everybody,



I have One Table 'tbl_student' fields are (Name,Class,Age,Address) OK.


I entered records,

 Name   Class    Age    Address
------  ------   -----  --------
 Mohan   X        18     Erode
 Kumar   XII      20     Chennai           ---->1
 Arun    XI       19     Trichy
 Bala    VII      17     Coimbatore        ---->2
 Vijay   VIII     17     Chennai
 Karthi   X       19     Erode

Ok.

I stored Finally 'Kumar' and 'Bala' Details(1,2).

How I get these Last 2 inserted records using MS Sql Query?
any One Known plz tell me....

By from Mohan.

[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 8-May-11 21:08pm
v2
Comments
Toniyo Jackson 9-May-11 3:11am    
Are you using primary key in table?

You have primary key or identity in your table?
Consider ID as another column(Identity) in table
ID   Name   Class    Age    Address
--  ------  ------   -----  --------
 1  Mohan   X        18     Erode
 2  Kumar   XII      20     Chennai           ---->1
 3  Arun    XI       19     Trichy
 4  Bala    VII      17     Coimbatore        ---->2
 5  Vijay   VIII     17     Chennai
 6  Karthi   X       19     Erode

Now you can get last inserted records by following query
SQL
SELECT TOP 2 * FROM tbl_student ORDER BY ID DESC--For last 2 inserted records

SQL
SELECT * FROM tbl_student WHERE ID =(SELECT Max(ID) FROM tbl_student)--For last inserted record

Customize query based on your criteria.
 
Share this answer
 
v2
try this link

Get Latest Id and Display [^]
 
Share this answer
 
Try:
SQL
SELECT * FROM tbl_student WHERE Name=Kumar OR Name=Bala


[edit]Oops! Wrong names...:O - OriginalGriff[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900