Pagination in SQL Server
How do i limit the result of a query (in my case about 60K rows) and select only from the X row to the Y row?
If I use ROW_NUMBER() I don't like my query because it involves 2 select queries .. one to return the rows and one to select the portion I need
Update:
Here's the query I use now:
SELECT *
FROM (
SELECT row_number() OVER (ORDER BY E.LastChangeDate DESC) AS row, E.*, U.[DisplayName] AS EntryCreatorDisplayName, U.[Email] AS EntryCreatorEmail
FROM entries e
INNER JOIN
users u
ON e.fk_user= u.id
WHERE e.EntryRank = 2
AND u.Administra开发者_如何学运维tor = 1
) as TableWithRows
WHERE (row >= 31 AND row <= 60)
WITH q AS
(
SELECT TOP (@Y) m.*, ROW_NUMBER() OVER (ORDER BY mycol) AS rn
FROM mytable m
ORDER BY
mycol
)
SELECT *
FROM q
WHERE rn >= @X
In SQL Server 2000
:
SELECT *
FROM (
SELECT TOP (@Y - @X) *
FROM (
SELECT TOP (@X) *
FROM mytable
ORDER BY
mycol
) q
ORDER BY
mycol DESC
) q2
ORDER BY
mycol
Row_Number() function can be used for this. Please refer to following article for usage http://www.databasejournal.com/features/mssql/article.php/3572301/RowNumber-function-in-SQL-Server-2005.htm
Don't have access to SQL at this very moment, but would something like this work?
SELECT tempid=IDENTITY(int, 1, 1), * FROM tbl WHERE tempid >= @x AND tempid <= @y
I'm not too familiar with MSSQL, But in MySQL, I'd do something like this:
LIMIT 50, 10
Where 10 is the number of records to skip, and 50 is the number to take.
精彩评论