简体   繁体   English

使用MSSQL 2005对记录进行分页

[英]Pagination of records with MSSQL 2005

I am trying to paginate the records fetched into pages of 10 (using PHP AJAX on MSSQL Server 2005). 我正在尝试对提取到10页的记录进行分页(在MSSQL Server 2005上使用PHP AJAX)。 (I am passing SQL as PHP strings to MSSQL server) I wish to display button for each page (for example if there are 45 records then I wish to display 5 buttons with each button fetching 10 records). (我将SQL作为PHP字符串传递给MSSQL服务器)我希望为每个页面显示按钮(例如,如果有45条记录,那么我希望显示5个按钮,每个按钮获取10条记录)。 I have came across this code- 我遇到了这段代码-

SELECT  *
FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY OrderDate ) AS RowNum, *
          FROM      Orders
          WHERE     OrderDate >= '1980-01-01'
        ) AS RowConstrainedResult
WHERE   RowNum >= 1
    AND RowNum <= 10
ORDER BY RowNum

But this is highly ineffective because firstly whole database is fetched then from it 10 rows is fetched every time. 但这是非常无效的,因为首先要获取整个数据库,然后每次要从中获取10行。 Could anyone suggest some technique (that works with MSSQL server 2005) wihich fetches only 10 rows at a time and not the whole 100 records at a time. 谁能建议某种技术(与MSSQL Server 2005一起使用),一次只能获取10行,而一次不能获取全部100条记录。 The above method also includes an extra column (it requires a primary key). 上面的方法还包括一个额外的列(它需要一个主键)。 I wish to get rid of that column too! 我也希望摆脱该专栏!

Also, since I have to display the page numbers (pagination tabs), I wish to have a count of total number of records beforehand. 另外,由于我必须显示页码(分页选项卡),所以我希望预先记录总数。

Thanks! 谢谢!

Learn to use LINQ. 学习使用LINQ。 Very efficient. 非常有效率。

Edit 编辑
Well then. 好吧。

SELECT * 
FROM etc
WHERE Row_Number() BETWEEN '".$startLimit."' AND '".$endLimit."'

Might work for you. 可能会为您工作。 Remember to escape the variables so no dangerous code can bass by. 请记住要对变量进行转义,以免危险的代码陷入困境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM