Pagination in Sql Server 2005

Method 1:
  1. DECLARE @rowsPerPage int,
  2. @pageNum int,
  3. @startRow int,
  4. @endRow int
  5. SET @rowsPerPage = 10
  6. SET @pageNum = 3
  7. SET @startRow = ((@pageNum- 1) * @rowsPerPage)+1
  8. SET @endRow = @startRow + @rowsPerPage -1
  9. SELECT * FROM (
  10. SELECT row_number() OVER (ORDER BY id) as resultNum,
  11. id FROM myTable
  12. ) as numberResults
  13. WHERE resultNum BETWEEN @startRow AND @endRow

Method2:
  1. SET @rowsPerPage = 10
  2. SET @pageNum = 3
  3. With SQLPaging
  4. As
  5. (
  6. Select Top(@rowsPerPage * @pageNum) ROW_NUMBER() OVER (ORDER BY id) as resultNum, id
  7. FROM myTable
  8. )
  9. select * from SQLPaging where resultNum > ((@pageNum - 1) * @rowsPerPage)

Comments

Popular posts from this blog

IBM FileNet: Bulk Processing using JavaScript

ASP.NET Web API Tutorial for Beginners

DB2 Date Time Functions