分页查询的原理:
1.先将预分页内容的码按照排序条件加上自增列导入到临时表中(或表变量)
2.针对临时表操作,找到相应的码列表
3.根据码列表,重新找相应原分页内容
需要注意的是:
第一是添加自增列,确定行号
第二缩减中间处理过程的操作数据量
CREATE PROC UP_tFunction_GetListByPage
(
@PageIndex INT,
@PageSize INT
)
AS
SELECT OID,
vName,
vLabel,
vRemark
FROM tFunction
WHERE OID in (
SELECT OID
FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY OID DESC) AS RowID, OID
FROM tFunction
) AS t
WHERE RowID between (@PageIndex-1)*@PageSize and @PageIndex*@PageSize
)
ORDER BY OID DESC
GO
-- UP_tFunction_GetListByPage 2, 10
CREATE PROC UP_tFunction_GetListByPage2
(
@PageIndex INT,
@PageSize INT
)
AS
WITH abc
AS
(
SELECT ROW_NUMBER() OVER(ORDER BY OID DESC) AS RowID, OID
FROM tFunction
)
SELECT *
FROM tFunction
WHERE OID in (
SELECT OID
FROM abc
WHERE RowID between (@PageIndex-1)*@PageSize and @PageIndex*@PageSize
)
GO
-- UP_tFunction_GetListByPage2 1, 10