SqlServer高效分页查询

SqlServer高效分页查询

-- =============================================
-- Author:xxx
-- Create date: xxx
-- Description:	查询一个用户的发帖信息
-- =============================================
ALTER PROCEDURE [dbo].[p_Page_UserID]
@pageindex int,
@pagesize int ,
@UserID uniqueidentifier
AS
BEGIN
	SELECT * FROM
	(
		SELECT TOP (@pageindex*@pagesize) row_number() over(
			order by PostAddTime DESC
		) n,* from xyl_UserPost  WHERE PostUserID = @UserID AND PostState=1
	) hhh 
	where hhh.n > @PageSize * (@pageIndex - 1)
	and hhh.n <= @PageSize * @pageIndex
	SELECT COUNT(*) FROM xyl_UserPost WHERE PostUserID=@UserID AND  PostState=1
END


你可能感兴趣的:([07],SQL,Server)