0

I know, we can use scope identity, or top 1 with order by column name desc, then will give a last record.

But I want like this:

SELECT [LAST RECORD] * FROM [TABLE NAME]   --> without any WHERE and ORDER

Can MSSQL do it ?

slugster
  • 49,403
  • 14
  • 95
  • 145
Ken Le
  • 1,787
  • 2
  • 22
  • 34
  • Oh, so, that mean, anyway I must ORDER BY or get last identity for WHERE ... Teeheee, so sad, but I always do like that. I just want to know if there is a better way without sort records. – Ken Le Jul 08 '11 at 23:12

1 Answers1

4

As I was just noting recently in another question, SQL tables have no inherent order. There's no such thing as the "last record" until the table has been sorted.

  • Maybe [this](http://stackoverflow.com/questions/5061595/select-bottom-rows-in-natural-order/5061707#5061707)? – Tim Schmelter Jul 08 '11 at 23:06
  • Oh, so, that mean, anyway I must ORDER BY or get last identity for WHERE ... Teeheee, so sad, but I always do like that. I just want to know if there is a better way without sort records. – Ken Le Jul 08 '11 at 23:12
  • @Tim: Different answer, but same idea! –  Jul 08 '11 at 23:21
  • 1
    @ken - the records are unordered in the database. They may display to you in the same order most of the time (likely clustered index key order) but it's not guaranteed. – JNK Jul 08 '11 at 23:24
  • fetching TOP 1 rows will not result in sorting in SQL server. sorting is meant to sort all records which has cost of `O (n log (n))`. but finding TOP 1 is like finding minimum or maximum of a record set which has a cost of `Theta (n)`. – Maziar Taheri Jul 09 '11 at 18:48