sql 具有偏移和限制的MS SQL分页1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 具有偏移和限制的MS SQL分页1相关的知识,希望对你有一定的参考价值。

-- Offset starting from 1.
-- This kind of paging returns one extra row 
-- such that in mobile apps, 
-- we can automatically handle the scroll event 
-- in cases where the record contains extra rows

-- For example: If we asked for 10 records and 
-- the script returns 11, We can write code to fetch 
-- the next 10 records if the last returned list
-- has one extra data.

-- Note : We always need to hide the extra rows that 
-- the script returns. 
-- Example : If the script returns 11 when 10 was asked, 
-- hide the last 11th record.

-- That last row is applicable to trigger the next fetch 
-- when the user reaches the end of the page
-- and needs to scroll further

CREATE PROCEDURE [dbo].[usp_SampleSPToSearchUsers]
    @offset INT ,
    @limit INT ,
    @Name NVARCHAR(250)
AS
    BEGIN
        SELECT  * ,
                ROW_NUMBER() OVER 
                 ( ORDER BY ( [UserID] ) DESC ) AS RowNum
        FROM    SampleUsersTable
        WHERE   FirstName + ' ' + LastName LIKE '%' + @Name + '%'
        ORDER BY ( [UserID] ) DESC
                OFFSET ( @offset - 1 ) ROWS 
                FETCH NEXT ( @limit + 1 ) ROWS
                ONLY;
    END;

以上是关于sql 具有偏移和限制的MS SQL分页1的主要内容,如果未能解决你的问题,请参考以下文章

具有限制和偏移量的 CodeIgniter sql 查询未显示结果

使用MySQL的递延Join连接实现高效分页 - Aaron

Sql Server 按偏移量分页行 - 没有'ORDER BY'

具有限制/偏移的 Firebase 查询可能的解决方案

这是不是可以获得具有偏移限制的总行数

sql MS SQL分页下一个上一个方法