Paging results in SQL Server 2000

Asked

Viewed 443 times

1

Sometimes I need to make queries in SQL Server 2000 and paginate the results but there is only the clause TOP that limits the number of records returned without an interval (offset) other banks like Mysql or Postgresql have it and query would be like this:

SELECT * FROM tabela LIMIT 0, 50
ou
SELECT * FROM tabela LIMIT 50, OFFSET 0

How can I get around this problem or emulate this feature in SQL Server 2000.

  • From Sql Server 2012 we have Offset which has exactly the same function as the limit. As the upgrade is not possible ...

2 answers

2

First of all I would recommend a nice upgrade, however there is a way to induce to do something similar

;WITH Results_CTE AS
(
    SELECT
        Col1, Col2, ...,
        ROW_NUMBER() OVER (ORDER BY SortCol1, SortCol2, ...) AS RowNum
    FROM Table
    WHERE <whatever>
)
SELECT *
FROM Results_CTE
WHERE RowNum >= @Offset
AND RowNum < @Offset + @Limit

In that article of the SOEN in a much more complete way, it shows other possibilities, besides giving a more detailed explanation

  • I fully agree with the upgrade but it is not my decision. I will test, thank you for the answer.

  • I know how it is... sad reality when we can’t do the proper upgrades when we want to right? Tbm suffer from this, especially when I need to maintain . NET Framework 2.0 with Access

0

The query below has the same result of the "limit 50, 100", that is brings between the record 51 until the 100.

SELECT TOP 50 *
FROM tabela 
WHERE not ID in ( 
  SELECT TOP 50 ID
  FROM   tabela
  ORDER BY ID ASC
) a 
ORDER BY ID ASC;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.