LIMIT 10..20 in SQL Server

后端 未结 15 2284
礼貌的吻别
礼貌的吻别 2020-11-22 11:21

I\'m trying to do something like :

SELECT * FROM table LIMIT 10,20

or

SELECT * FROM table LIMIT 10 OFFSET 10
相关标签:
15条回答
  • 2020-11-22 11:50
    SELECT TOP 10 *
    FROM TABLE
    WHERE IDCOLUMN NOT IN (SELECT TOP 10 IDCOLUMN FROM TABLE)
    

    Should give records 11-20. Probably not too efficient if incrementing to get further pages, and not sure how it might be affected by ordering. Might have to specify this in both WHERE statements.

    0 讨论(0)
  • 2020-11-22 11:51
     SELECT * FROM users WHERE Id Between 15 and 25
    

    it will print from 15 to 25 as like limit in MYSQl

    0 讨论(0)
  • 2020-11-22 11:55

    For SQL Server 2012 + you can use.

    SELECT  *
    FROM     sys.databases
    ORDER BY name 
    OFFSET  5 ROWS 
    FETCH NEXT 5 ROWS ONLY 
    
    0 讨论(0)
  • 2020-11-22 11:56

    The LIMIT clause is not part of standard SQL. It's supported as a vendor extension to SQL by MySQL, PostgreSQL, and SQLite.

    Other brands of database may have similar features (e.g. TOP in Microsoft SQL Server), but these don't always work identically.

    It's hard to use TOP in Microsoft SQL Server to mimic the LIMIT clause. There are cases where it just doesn't work.

    The solution you showed, using ROW_NUMBER() is available in Microsoft SQL Server 2005 and later. This is the best solution (for now) that works solely as part of the query.

    Another solution is to use TOP to fetch the first count + offset rows, and then use the API to seek past the first offset rows.

    See also:

    • "Emulate MySQL LIMIT clause in Microsoft SQL Server 2000"
    • "Paging of Large Resultsets in ASP.NET"
    0 讨论(0)
  • 2020-11-22 11:58

    Just for the record solution that works across most database engines though might not be the most efficient:

    Select Top (ReturnCount) *
    From (
        Select Top (SkipCount + ReturnCount) *
        From SourceTable
        Order By ReverseSortCondition
    ) ReverseSorted
    Order By SortCondition
    

    Pelase note: the last page would still contain ReturnCount rows no matter what SkipCount is. But that might be a good thing in many cases.

    0 讨论(0)
  • 2020-11-22 11:58

    Use all SQL server: ;with tbl as (SELECT ROW_NUMBER() over(order by(select 1)) as RowIndex,* from table) select top 10 * from tbl where RowIndex>=10

    0 讨论(0)
提交回复
热议问题