How the heck in a SQL statement can I select the LAST record in my DB?
thanks
Printable View
How the heck in a SQL statement can I select the LAST record in my DB?
thanks
There isn't a SQL command for getting the last record.
The only way would be to grab the entire collection and use .MOVELAST.
Code:SELECT * FROM tablename WHERE key_column = (SELECT MAX(key_column) FROM tablename)
That will work, as long as you have got unique keys.
Actually that won't work :rolleyes:
the MAX clause is quantitive, and not chronological, therefore you would only return the record with the highest value, and not necessarily the last record.
If you have a time stamped field you could use the MAX clause on this to achieve the desired effect.
go with the MOVELAST command. that will always work.
This will work in SQL Server, Access and Sybase:
Code:Select Top 1 * From MyTable Order By FieldId Desc
ahh good thinking serge.... TOP with a sort by Desc(ending) would work perfectly.
I bow to your experience o wise one :rolleyes: ;)
thank you all for your comments. They all work especially Serge's idea!Quote:
Originally posted by Serge
This will work in SQL Server, Access and Sybase:
Code:Select Top 1 * From MyTable Order By FieldId Desc
:)