|
|
Question : Pagination query with MSSQL Server
|
|
Hii,
I am using the following for doing the pagination in Oracle.
SELECT * FROM (SELECT tt.*, ROWNUM ROWNO FROM ( SELECT blah...blah.. FROM table WHERE some=some ) tt WHERE ROWNUM <= 20) WHERE ROWNO >0
How can i emulate it for MS SQL Server?
Any ideas please.
Thanks aks
|
Answer : Pagination query with MSSQL Server
|
|
Hi aks143,
The best I have for paging with MS SQL is ...
in MySQL : SELECT * FROM test order by idtest LIMIT 3000, 20
in MSSQL : select * from (select top 20 * from (select top 3020 * from test order by idtest) as t1 order by idtest DESC) as t2 order by idtest
This is from my own pager notes. MySQL supports paging, MSSQL does not. I have the comment "You can also use SET ROWCOUNT before the main select". Maybe useful.
set rowcount $limit select * from Table1 where pk in( select top $total pk from Table1 order by pk desc )order by pk
Table1: name of the table PK : Primary Key of Table1 $limit : The number of records you need each time $total : Total number of records in each time
The only thing you need to do is to get the total number of records (select count(*)) for the first $total value then $n =$total/$limit
//Forloading next page $total = $total - $limit //then run the sql statement with new $totalvalue
//For loading page 2 $total = $total - ($limit)*2
//For loading page n $total = $total - ($limit)*n
1-You can put this code in a stored procedure and pass $limit and $total values when you calling the stored procedure or you can use it in your php code
2-You can change the SQL statement to work with your search-query results instead of Table1
Regards,
Richard Quadling.
|
|
|
|
|