Click to See Complete Forum and Search --> : retrieving last row


themoon
10-06-2007, 02:59 AM
Hi
I was wondering how can i retrieve the last record in mssql-server ?

Thanks in advance.

scragar
10-06-2007, 09:20 PM
taking a guess here, but on mysql to get the last row you do:

SELECT * FROM tbl ORDER BY id DESC LIMIT 1

basicly, order the row by an autoincremented ID descending(so the latest ID is first) then limit it to returning a single row.

mattyblah
10-08-2007, 01:51 AM
taking a guess here, but on mysql to get the last row you do:

SELECT * FROM tbl ORDER BY id DESC LIMIT 1

basicly, order the row by an autoincremented ID descending(so the latest ID is first) then limit it to returning a single row.

in SQL Server the corresponding syntax would be:

SELECT TOP 1 * FROM tbl ORDER BY id;