CREATE PROC pain
(
@PageSize int , -- 记录条数
@PageIndex int -- 页码
)
AS
declare @strBeginNum INT --起始页
declare @strEndNum int --结束页
declare @strSQL nvarchar(1000) -- main sql
--
SET @strBeginNum= (@PageIndex - 1) * @PageSize + 1 --(索引-1)*10+1 每页显示的条数和当前页码,比如第一页开始是1,结束是10,第二页开始是11,结束是20,这样的规则计算
SET @strEndNum=@strBeginNum + @PageSize -1 --开始*条数-1 1+10 -1=10 获取1*10
--实现分页查询
SET @strSQL= 'SELECT * FROM
( SELECT ROW_NUMBER() OVER (ORDER BY id )num,* FROM SysMenu) a WHERE num BETWEEN ' +convert(nvarchar,@strBeginNum)+' and '+convert(nvarchar,@strEndNum) +''
EXEC(@strSQL)
PRINT(@strSQL)
GO
EXEC pain 10,1
DROP PROC pain
SELECT * FROM SysMenu sm