We Recommend

SQL Cookbook SQL Cookbook
Written in O'Reilly's popular Problem/Solution/Discussion style, the SQL Cookbook is sure to please. Anthony's credo is: "When it comes down to it, we all go to work, we all have bills to pay, and we all want to go home at a reasonable time and enjoy what's still available of our days." The SQL Cookbook moves quickly from problem to solution, saving you time each step of the way.


Posted By

krisdb on 07/11/07


Tagged

sql


Versions (?)


ROW_NUMBER()


Published in: SQL 


  1. CREATE PROCEDURE [dbo].[selRecords]
  2. @id int,
  3. @iRowStart int,
  4. @iRowEnd int
  5.  
  6. AS
  7. BEGIN
  8.  
  9. SET NOCOUNT ON;
  10.  
  11. WITH RowEntries AS
  12. (
  13. SELECT ROW_NUMBER() OVER (ORDER BY datefield DESC)
  14. AS Row,id,firstname,lastname FROM [TABLE] WHERE id = @id
  15. )
  16.  
  17. SELECT id,firstname,lastname FROM RowEntries WHERE Row BETWEEN @iRowStart AND @iRowEnd
  18.  
  19. END
  20.  
  21.  
  22. ASP Code:
  23.  
  24. dim Id: Id = request.querystring("id")
  25. dim iCurrentPage: iCurrentPage = request.querystring("p")
  26.  
  27. dim iRecordsPerPage: iRecordsPerPage = 10
  28.  
  29. dim iTotalRecords: iTotalRecords = getTotalRecords(Id) 'build this function
  30.  
  31. if len(iCurrentPage) = 0 or not isnumeric(iCurrentPage) then _
  32. iCurrentPage = 1
  33.  
  34. iRowStart = ((iCurrentPage - 1) * iRecordsPerPage + 1)
  35. iRowEnd = (iRowStart + (iRecordsPerPage-1))
  36.  
  37.  
  38. sql= "dbo.selRecords " & Id & ", "&iRowStart&", " & iRowEnd
  39.  
  40.  
  41. if (iCurrentPage > 1) then _
  42. response.write "<div style=""float:left;""><a href="""&scriptName&"?id="&Id&"&p="&(iCurrentPage-1)&""">&lt;&lt; Previous</a></div>"
  43.  
  44. if (iRowEnd <= iTotalRecords) then _
  45. response.write "<div style=""float:right;""><a href="""&scriptName&"?id="&Id&"&p="&(iCurrentPage+1)&""">Next &gt;&gt;</a></div>"

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: krisdb on July 12, 2007

You need to login to post a comment.