我在我的asp.net應用程序中實現了分頁。爲此,我創建了一個存儲過程以獲取CommunityPost
表中的記錄。但是,此存儲過程無法正常工作。它不返回任何記錄。分頁存儲過程不起作用
我的存儲過程是:CommunityPost
表
ALTER PROCEDURE [dbo].[CommunityPostLoadAllPaged]
(
@PageIndex int = 0,
@PageSize int = 2147483644,
@TotalRecords int = null OUTPUT
)
AS
BEGIN
DECLARE @sql nvarchar(max)
--paging
DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
DECLARE @RowsToReturn int
SET @RowsToReturn = @PageSize * (@PageIndex + 1)
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
CREATE TABLE #DisplayOrderTmp
(
[Id] int IDENTITY (1, 1) NOT NULL,
[CommunityPostId] int NOT NULL
)
SET @sql = '
INSERT INTO #DisplayOrderTmp ([CommunityPostId])
SELECT p.Id
FROM
CommunityPost p with (NOLOCK)'
CREATE TABLE #PageIndex
(
[IndexId] int IDENTITY (1, 1) NOT NULL,
CommunityPostId int NOT NULL
)
INSERT INTO #PageIndex (CommunityPostId)
SELECT CommunityPostId
FROM #DisplayOrderTmp
GROUP BY CommunityPostId
ORDER BY min([Id])
SELECT *
FROM #PageIndex
--total records
SET @TotalRecords = @@rowcount
select * from #DisplayOrderTmp
DROP TABLE #DisplayOrderTmp
select * from #PageIndex
--return products
SELECT TOP (@RowsToReturn)
p.*
FROM
#PageIndex [pi]
INNER JOIN
dbo.CommunityPost p WITH (NOLOCK) ON p.Id = [pi].CommunityPostId
WHERE
[pi].IndexId > @PageLowerBound AND
[pi].IndexId < @PageUpperBound
ORDER BY
[pi].IndexId
DROP TABLE #PageIndex
END
表模式:
ColumnName DataType
================================
Id int
SharerId int
Text nvarchar(MAX)
Published bit
CreatedOnUtc datetime
我怎樣才能獲得CommunityPost
記錄分頁實現?
請幫幫我。
預先感謝您。
爲 「ID」 的主鍵? – CDC
當你甚至沒有加載該表時,你爲什麼要從#DisplayOrderTmp中選擇*?你有'從#PageIndex'中選擇*,然後選擇你的最終選擇。除非你有充分的理由不這樣做,否則請刪除所有不是你想要的實際結果集的選擇。 – Andrew