Pagination in SQL Server

How to limit the query result (in my case about 60K rows) and select only from row X to row Y?

If I use ROW_NUMBER (), I do not like my query because it includes 2 select queries. one to return rows and one to select the part I need

Update:

Here is the query I'm using 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.Administrator = 1
        ) as TableWithRows
WHERE   (row >= 31 AND row <= 60)
+3
source share
4 answers
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
+6
source

You do not have access to SQL at this moment, but something like this work?

SELECT tempid=IDENTITY(int, 1, 1), * FROM tbl WHERE tempid >= @x AND tempid <= @y
0
source

I am not very familiar with MSSQL, but in MySQL I would do something like this:

LIMIT 50, 10

Where 10 is the number of missing records, and 50 is the number.

-1
source

Source: https://habr.com/ru/post/1723255/


All Articles