Equivalent of LIMIT and OFFSET for SQL Server?

前端 未结 16 2213
天命终不由人
天命终不由人 2020-11-22 06:07

In PostgreSQL there is the Limit and Offset keywords which will allow very easy pagination of result sets.

What is the equivalent syntax f

16条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 06:48

    Another sample :

    declare @limit int 
    declare @offset int 
    set @offset = 2;
    set @limit = 20;
    declare @count int
    declare @idxini int 
    declare @idxfim int 
    select @idxfim = @offset * @limit
    select @idxini = @idxfim - (@limit-1);
    WITH paging AS
        (
            SELECT 
                 ROW_NUMBER() OVER (order by object_id) AS rowid, *
            FROM 
                sys.objects 
        )
    select *
        from 
            (select COUNT(1) as rowqtd from paging) qtd, 
                paging 
        where 
            rowid between @idxini and @idxfim
        order by 
            rowid;
    

提交回复
热议问题