我正在开发一个使用网格视图的应用程序。我想通过使用sql查询为该网格视图使用分页。
我希望在pagesize
和pageindex
的帮助下显示记录。
如何使用sql查询执行此操作?
答案 0 :(得分:0)
我想通过使用sql查询为该网格视图使用分页。
我想借助pagesize和pageindex显示记录。
将两个参数传递给sql查询@pageindex
和@pagesize
,您可以使用ROW_NUMBER()
函数进行分页。如下所示:
;With CTEForPagination AS
(
SELECT ROW_NUMBER() OVER(ORDER BY SomeFieldToOrderBy) AS RowNum, *
FROM YourTableReference
)
SELECT *
FROM CTEForPagination
WHERE RowNum BETWEEN ((@PageIndex - 1) * @PageSize + 1)
AND (@PageIndex * @PageSize)
ORDER BY AnotherFieldToOrderBy
--This is just an example
答案 1 :(得分:0)
WITH EmpOrdered AS
(
SELECT
Lastname,
ROW_NUMBER() Over (ORDER BY Lastname) AS rownum
FROM Employees
)
SELECT e.*
FROM Employees e
INNER JOIN EmpOrdered ON e.Lastname = EmpOrdered.Lastname
WHERE EmpOrdered.rownum BETWEEN 1000 AND 1050
ORDER BY EmpOrdered.rownum ASC
答案 2 :(得分:0)
试试这个
CREATE PROCEDURE GetUserByPaging
(
@PageSize int,
@PageIndex int
)
AS
/* SET NOCOUNT ON */
declare
@StartRow int,
@EndRow int
if(@PageIndex < 1)
Set @PageIndex = 1
Set @StartRow = (@PageSize * (@PageIndex - 1)) + 1
Set @EndRow = @PageSize * @PageIndex
SELECT * FROM(
SELECT
ROW_NUMBER() Over(Order By u.UserID) RowNumber,
u.UserID,
u.UserName
FROM
Users u) u
WHERE
((u.RowNumber Between @StartRow AND @EndRow))