存储过程中用临时表实现分页

CREATE PROCEDURE [dbo].[V2_WEB_A_Gis_GetList]
	@FatherId INT,
	@PageIndex INT,
	@PageSize INT,
	@Num INT OUTPUT
AS
BEGIN
	DECLARE @MinId INT
	DECLARE @MaxId INT
	SET @MinId = (@PageIndex - 1) * @PageSize
	SET @MaxId = @MinId + @PageSize
	CREATE TABLE #TempTable
	(
		IndexId INT IDENTITY(1,1) NOT NULL,
		Id INT
	)
	INSERT INTO #TempTable(ID)
		SELECT Id
			FROM Area
			WHERE FatherId = @FatherId
			ORDER BY Id DESC
	SET @Num = (SELECT count(0) From #TempTable)
	SELECT A.* 
		FROM Area A INNER JOIN #TempTable B ON A.Id = B.Id
		WHERE B.IndexId > @MinId AND B.IndexID <=@MaxId
		ORDER BY B.IndexId
	DROP TABLE #TempTable
END

  

posted @ 2012-03-27 22:20  Soar、毅  阅读(436)  评论(1编辑  收藏  举报