tags:

views:

29

answers:

4

Hi,

I want to get the entire row of a table in SQL Server, but only the last inserted row (which is the row with the highest "ID"). I tried top, max etc, but can't seem to get this to work. So if the highest ID is 15, I want to get all the fields of that row (15).

Any ideas?

Thanks

+2  A: 

Perhaps try this?

 SELECT * FROM MyTable
 WHERE ID = (SELECT MAX(ID) FROM MyTable)

OR

SELECT TOP 1 * FROM MyTable
ORDER BY ID DESC
p.campbell
+1  A: 
SELECT * from Table1
WHERE
  ID = ( SELECT MAX(ID) FROM Table1)
Eton B.
A: 

This should do it ...

select top 1 *
  from yourtable
 order by id desc
Peter
+1  A: 

SELECT * FROM table where id = (select max(id) from table);

JoeChin