![]() |
|
Why is there still no LIMIT clause? Updated: 6/20/2008 12:25:00 AM Requests: 16943 LIMIT is not in the SQL standard, and in fact only exists in MySQL. It is popular enough, however, that many SQL Server users want to see it implemented here. They say it is intuitive to use, but I disagree. Does LIMIT(8,12) really make it unambiguous that the query should return rows 9-20? In SQL Server 2005, you can use the new ROW_NUMBER() function in a subquery, and filter against the result. For example: CREATE TABLE #foo
Now, let's say I wanted to retrieve the runners who finished 5th through 10th. SELECT
Of course, the 5 and 10 could have easily been populated by variables, whereas in existing versions you would only be able to do this with a nested TOP clause if you also used dynamic SQL. As an added twist, SQL Server 2005 now allows variables/expressions in the TOP clause, so we could also do this: DECLARE @rowA INT
Personally, I think that query is far less intuitive and far more cumbersome to write than the ROW_NUMBER() version listed above. Some extra complications it adds are the fact that you need a third outer query and ORDER BY to return the results in the right order, it does include the actual rank or rownumber you were looking for, and it makes it much more difficult to derive a properly formatted character string that doesn't need to be used in the ORDER BY that drives the TOP functionality. Just look at the differences in the output:
I just need to get used to the ROW_NUMBER() syntax, that's all. Of course, this kind of methodology can be< |
| © 2004-2010 Aaron Bertrand, All Rights Reserved. SQL Server 2005, of course, belongs to Microsoft. |