sybe how do you calclute how many total pages for paging? do you run a seperate SQL statement just to return the COUNT of all records? Also i do ORDER BY FIELD DESC and ASC (based upon user input), which will create problems wih your suggestion.
I was reading about SQL Row_number Function, which does exactaly the same thing as you mentioned.
How about Data Tables?
Thanks
Main Topics
Browse All Topics





by: sybePosted on 2009-03-11 at 22:05:48ID: 23864945
I like to use SQL-statements like this:
for page 1:
SELECT TOP 20 <fields>
FROM <table>
page 2:
SELECT TOP 20 <fields>
FROM <table>
WHERE <id> NOT IN (SELECT TOP 20 <id> FROM <table>)
page 3:
SELECT TOP 20 <fields>
FROM <table>
WHERE <id> NOT IN (SELECT TOP 40 <id> FROM <table>)
etc
Or as a formula depending on RecordsPerPage and PageNumber
i RecordsPerPage = 20
iPageNumber = 5
"SELECT TOP " & i RecordsPerPage & "<fields> "
"FROM <table>"
"WHERE <id> NOT IN (SELECT TOP " & (iRecordsPerPage * (iPageNumber - 1)) " & <id> FROM <table>)"
If you have any WHERE clauses and/or ORDER BY, they need to be also in the subquery
i RecordsPerPage = 20
iPageNumber = 5
"SELECT TOP " & i RecordsPerPage & "<fields> "
"FROM <table>"
"WHERE <id> NOT IN (SELECT TOP " & (iRecordsPerPage * (iPageNumber - 1)) " & <id> FROM <table> WHERE <clause> ORDER BY <fields>)"
"WHERE <clause>"
"ORDER BY <fields>"
Works on every database