分页存储过程不起作用

Stored procedure for pagination is not working

我正在 asp.net 应用程序中实现分页。为此,我创建了一个存储过程来从 CommunityPost table 中获取记录。但是这个存储过程不能正常工作。它没有 return 任何记录。

我的存储过程是:

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

Table CommunityPost table 的架构:

ColumnName       DataType
================================
Id               int
SharerId         int
Text             nvarchar(MAX)
Published        bit
CreatedOnUtc     datetime

如何获取 CommunityPost 条记录以实现分页?

请帮助我。

提前致谢。

该查询中有很多奇怪的东西。

最重要的是:您将 @sql 设置为 运行 一些动态 SQL,然后 - 不调用它。无论如何,我看不出有任何理由 运行 将查询作为动态 SQL,其中没有变量。已经 运行 代码了。

其次,您在该过程中 运行 宁多个 SELECT。这是一种常见的调试技术,但可能不是您在完成的代码中想要的。如果你真的想要它 return 一个 table ,然后取出最后一个之前的所有 SELECT (当然除了插入你的临时 table 的那些).事实上,它会输出几组值,我猜你的调用代码只需要其中一组。

三、ORDER BY Min(Id)是为了达到什么目的?我猜你的目标是 ORDER BY id,但也许不是。

第四,您的 @PageSize 默认值很大,几乎低于 INT 的最大限制。然后将它乘以某个值,然后将其插入到另一个 INT 值中。如果使用默认值,那几乎肯定会溢出。这是故意的吗?

祝你好运,

试试这个

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