2015-06-14 53 views
0

我在我的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記錄分頁實現?

請幫幫我。

預先感謝您。

+0

爲 「ID」 的主鍵? – CDC

+1

當你甚至沒有加載該表時,你爲什麼要從#DisplayOrderTmp中選擇*?你有'從#PageIndex'中選擇*,然後選擇你的最終選擇。除非你有充分的理由不這樣做,否則請刪除所有不是你想要的實際結果集的選擇。 – Andrew

回答

2

嘗試這個

ALTER PROCEDURE [dbo].[CommunityPostLoadAllPaged] 
(
    @PageIndex int = 0, 
    @PageSize  int = 50, 
    @TotalRecords int = null OUTPUT 
) 
AS 
BEGIN 

     Declare @inEndRow  Int 
     ,@inStartRow  Int 

    CREATE TABLE #DisplayOrderTmp 
    (
     [Id] int IDENTITY (1, 1) NOT NULL, 
     [inRowNum] Int Primary Key, 
     [CommunityPostId] int NOT NULL 
    ) 


    INSERT INTO #DisplayOrderTmp ([CommunityPostId]) 
    SELECT ROW_NUMBER() OVER(ORDER BY p.Id asc), p.Id 
    FROM 
     CommunityPost p with (NOLOCK) 


    Select @TotalRecords = Count(1) 
      ,@inEndRow = ((@PageIndex  + @PageSize ) + 1) 
      ,@inStartRow = @PageIndex  
    From #DisplayOrderTmp As d With (Nolock) 


    Select * 
    From #DisplayOrderTmp As d With (Nolock) 
    Where d.inRowNum > @inStartRow 
      And d.inRowNum < @inEndRow 
END 
0

該查詢中有很多奇怪的事情。

最重要的是:您將@sql設置爲運行一些動態SQL,然後 - 不調用它。無論如何,我看不到有任何理由將該查詢作爲動態SQL運行,並且沒有變量。只需運行代碼即可。

其次,你在程序中運行多個SELECT。這是一種常見的調試技術,但可能不是您在完成的代碼中想要的。如果你真的想要它返回一張表,那麼在最後一張表中取出所有的SELECT s(當然,除了插入臨時表的那些表除外)。事實上,它會輸出幾組值,我猜你的調用代碼只是期待其中的一個。

三,ORDER BY Min(Id)的意思是什麼?我想你是瞄準ORDER BY id,但也許不是。

第四,您的默認值@PageSize是巨大的,幾乎低於INT的最大限制。並且你將它乘以某個值,並將其插入另一個INT值。如果使用默認值,那實際上可以保證溢出。這是意圖嗎?

祝你好運,