Monday, February 20, 2012

LIMIT rows with an offset

I have a query in MySQL which needs to be translated to an equivalent in MSSQL Server. The query uses a LIMIT clause with an offset.

select * from test LIMIT 10,5

(meaning from the 10th row containing 5 rows in the result set)

How can we port the same logic to SQL Server ? I know TOP can retrieve top n rows but how can we specify the offset ?

Any help is appreciated !Any such offsets or paging is meaningless without an ORDER BY clause.|||Blindman is quite correct, without an ORDER BY clause this is dangerous in any SQL dialect.

SQL-2000 doesn't have a good way to implement the functionality of MySQL's LIMIT clause. Until a new version of SQL provides a better solution, the best answer I can give you is that your code needs to provide the paging functionality.

The best way that I know to provide paging functionality is to use ORDER BY with a "rowset key" within your data and the TOP N syntax to limit the number of rows returned by a SELECT statement. Keep track of the rowset key of the last row returned, and use that as the seed for the next iteration (page) of your SELECT.

-PatP|||Appreciate your help ! Thanks !|||It must be paging day

http://weblogs.sqlteam.com/jeffs/archive/2004/03/22/1085.aspx

No comments:

Post a Comment