如何在不使用OFFSET MSSQL服务器的情况下在存储过程中使用分页?

发布于 2025-01-11 07:14:14 字数 1337 浏览 0 评论 0原文

如何在不使用OFFSET MSSQL服务器的情况下在存储过程中使用分页?
我想在 asp .net Web 应用程序 (MVC) 中实现像 Google 一样的分页

ALTER PROCEDURE BooksGetList
    @BookName VARCHAR(50) = null, @BookPublisherId INT = null, @BookCategoryId INT = null
    AS BEGIN
    SELECT 
        ISNULL([Books].[BookId], '') AS [BookId],
        ISNULL([Books].[BookName], '') AS [BookName],
        ISNULL([BookCategories].[BookCategoryId], '') AS [BookCategoryId],
        ISNULL([BookCategories].[BookCategoryName], '') AS [BookCategoryName],
        ISNULL([BookPublishers].[BookPublisherId], '') AS [BookPublisherId],    
        ISNULL([BookPublishers].[BookPublisherName], '') AS [BookPublisherName],    
        ISNULL([Books].[BookQuantity], '') AS [BookQuantity],
        ISNULL([Books].[IsActive], '') AS [IsActive]
    FROM
        [Books] Inner Join BookCategories On [BookCategories].BookCategoryId = [Books].BookCategoryId
                Inner Join BookPublishers On [BookPublishers].BookPublisherId = [Books].BookPublisherId
    Where
         ([Books].[BookName] LIKE '%'+@BookName+'%' OR @BookName IS NULL)
    AND  ([BookCategories].BookCategoryId = @BookCategoryId OR @BookCategoryId IS NULL)     
    AND  ([BookPublishers].BookPublisherId  = @BookPublisherId OR @BookPublisherId IS NULL)

    
    ORDER BY BookId

END
GO

How to use Pagination in stored procedure without using OFFSET MSSQL server?
I want to implement paging Like Google in a asp .net web application (MVC)

ALTER PROCEDURE BooksGetList
    @BookName VARCHAR(50) = null, @BookPublisherId INT = null, @BookCategoryId INT = null
    AS BEGIN
    SELECT 
        ISNULL([Books].[BookId], '') AS [BookId],
        ISNULL([Books].[BookName], '') AS [BookName],
        ISNULL([BookCategories].[BookCategoryId], '') AS [BookCategoryId],
        ISNULL([BookCategories].[BookCategoryName], '') AS [BookCategoryName],
        ISNULL([BookPublishers].[BookPublisherId], '') AS [BookPublisherId],    
        ISNULL([BookPublishers].[BookPublisherName], '') AS [BookPublisherName],    
        ISNULL([Books].[BookQuantity], '') AS [BookQuantity],
        ISNULL([Books].[IsActive], '') AS [IsActive]
    FROM
        [Books] Inner Join BookCategories On [BookCategories].BookCategoryId = [Books].BookCategoryId
                Inner Join BookPublishers On [BookPublishers].BookPublisherId = [Books].BookPublisherId
    Where
         ([Books].[BookName] LIKE '%'+@BookName+'%' OR @BookName IS NULL)
    AND  ([BookCategories].BookCategoryId = @BookCategoryId OR @BookCategoryId IS NULL)     
    AND  ([BookPublishers].BookPublisherId  = @BookPublisherId OR @BookPublisherId IS NULL)

    
    ORDER BY BookId

END
GO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城已无爱 2025-01-18 07:14:14

如果您使用 2008 R2 或更早版本,则无法使用 OFFSET FETCH,

您可以选择使用 ROW_NUMBER() 并重写您的查询,例如:

使用 OFFSET

SELECT Price
FROM dbo.Inventory
ORDER BY Price OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY

此查询不使用 OFFSET 使用 ROW_NUMBER()

SELECT Price
FROM
(
SELECT Price
ROW_NUMBER() OVER (ORDER BY Price) AS Seq
FROM dbo.Inventory
)t
WHERE Seq BETWEEN 11 AND 15

If you use 2008 R2 or older you can't use OFFSET FETCH,

you have alternative to use ROW_NUMBER() and rewrite your query for example:

with OFFSET

SELECT Price
FROM dbo.Inventory
ORDER BY Price OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY

this query without OFFSET using ROW_NUMBER()

SELECT Price
FROM
(
SELECT Price
ROW_NUMBER() OVER (ORDER BY Price) AS Seq
FROM dbo.Inventory
)t
WHERE Seq BETWEEN 11 AND 15
诗酒趁年少 2025-01-18 07:14:14

为了解决我的问题,我使用了

ALTER PROCEDURE BooksGetList
    @BookName VARCHAR(50) = null, @BookPublisherId INT = null, @BookCategoryId INT = null, @PageNumber INT = 1, @PageSize INT = 10,@TotalRecords INT = null OUT
    AS BEGIN
    SELECT 
        [Books].[BookId],                           [Books].[BookName],
        [BookCategories].[BookCategoryId],          [BookCategories].[BookCategoryName],
        [BookPublishers].[BookPublisherId],         [BookPublishers].[BookPublisherName],   
        [BookQuantity],                             [Books].[IsActive],
        [Books].[CreatedBy],                        [Books].[CreatedOn],        
        [Books].[ModifiedBy],
        [Books].[ModifiedOn],                       ROW_NUMBER() OVER (ORDER BY BookId) as RowNumber into #TempBooks
    FROM
        [Books] Inner Join BookCategories On [BookCategories].BookCategoryId = [Books].BookCategoryId
                Inner Join BookPublishers On [BookPublishers].BookPublisherId = [Books].BookPublisherId
    Where
         ([Books].[BookName] LIKE '%'+@BookName+'%' OR @BookName IS NULL)
         AND  ([BookCategories].BookCategoryId = @BookCategoryId OR @BookCategoryId IS NULL)    
         AND  ([BookPublishers].BookPublisherId  = @BookPublisherId OR @BookPublisherId IS NULL) 
         AND  Books.IsActive = 1 
    ORDER BY 
         BookId 
    SELECT @TotalRecords = COUNT(BookId) FROM #TempBooks    
    SET @TotalRecords =  @TotalRecords
    SELECT *,  @TotalRecords AS TotalRecords FROM #TempBooks
    WHERE RowNumber between (@PageNumber - 1) * @PageSize + 1 and @PageNumber * @PageSize
    DROP TABLE #TempBooks
END
GO

To solve my Problem i used

ALTER PROCEDURE BooksGetList
    @BookName VARCHAR(50) = null, @BookPublisherId INT = null, @BookCategoryId INT = null, @PageNumber INT = 1, @PageSize INT = 10,@TotalRecords INT = null OUT
    AS BEGIN
    SELECT 
        [Books].[BookId],                           [Books].[BookName],
        [BookCategories].[BookCategoryId],          [BookCategories].[BookCategoryName],
        [BookPublishers].[BookPublisherId],         [BookPublishers].[BookPublisherName],   
        [BookQuantity],                             [Books].[IsActive],
        [Books].[CreatedBy],                        [Books].[CreatedOn],        
        [Books].[ModifiedBy],
        [Books].[ModifiedOn],                       ROW_NUMBER() OVER (ORDER BY BookId) as RowNumber into #TempBooks
    FROM
        [Books] Inner Join BookCategories On [BookCategories].BookCategoryId = [Books].BookCategoryId
                Inner Join BookPublishers On [BookPublishers].BookPublisherId = [Books].BookPublisherId
    Where
         ([Books].[BookName] LIKE '%'+@BookName+'%' OR @BookName IS NULL)
         AND  ([BookCategories].BookCategoryId = @BookCategoryId OR @BookCategoryId IS NULL)    
         AND  ([BookPublishers].BookPublisherId  = @BookPublisherId OR @BookPublisherId IS NULL) 
         AND  Books.IsActive = 1 
    ORDER BY 
         BookId 
    SELECT @TotalRecords = COUNT(BookId) FROM #TempBooks    
    SET @TotalRecords =  @TotalRecords
    SELECT *,  @TotalRecords AS TotalRecords FROM #TempBooks
    WHERE RowNumber between (@PageNumber - 1) * @PageSize + 1 and @PageNumber * @PageSize
    DROP TABLE #TempBooks
END
GO
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文