0

After viewing some examples of creating a pagination function with SQL, I have managed to write the following working code:

SELECT * 
FROM Services
ORDER BY ServiceDateUpdated DESC
   OFFSET @Offset ROWS FETCH NEXT @RecordsPerPage ROWS ONLY;

Today, after working with this line of code for over a year, I have realized that it is not to be used with earlier versions of SQL Server than 2012. My current SQL Server version is 2008.

How can I re-write it to make it work with SQL Server 2008?

Thanks in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tommy Naidich
  • 752
  • 1
  • 5
  • 23
  • possible duplicate of [What is the best way to paginate results in SQL Server](http://stackoverflow.com/questions/109232/what-is-the-best-way-to-paginate-results-in-sql-server) – jpw Jul 10 '15 at 14:19
  • Also: http://stackoverflow.com/questions/2244322/how-to-do-pagination-in-sql-server-2008?rq=1 – jpw Jul 10 '15 at 14:19

1 Answers1

0

I will try this:

SET @MinPageRank = (@PageNumber - 1) * @NumInPage + 1  
SET @MaxPageRank =  @PageNumber * @NumInPage

SELECT * FROM (SELECT [RANK] = ROW_NUMBER() OVER (ORDER BY
ServiceDateUpdated  DESC),* FROM Service )
A WHERE A.[RANK] BETWEEN {MinPageRank} AND {MaxPageRank}
Belen Martin
  • 507
  • 5
  • 7