Friday, November 13, 2009

T-SQL: alternative to LIMIT clause

In MySQL as well as in PostgreSQL there's a LIMIT clause with which you can specify the offset of starting row and number of total rows that you wish to retrieve. There's no such thing, AFAIK, in T-SQL (SQL Server) so I'm using WITH statement with ROW_NUMBER().

WITH tbl AS
(
SELECT
id,
ROW_NUMBER() OVER (ORDER BY id) AS rowNum
FROM user
)
SELECT id
FROM tbl
WHERE rowNum BETWEEN 5 AND 10

No comments:

Post a Comment