Row Offset in SQL Server

后端 未结 16 2389
醉酒成梦
醉酒成梦 2020-11-22 05:53

Is there any way in SQL Server to get the results starting at a given offset? For example, in another type of SQL database, it\'s possible to do:

SELECT * FR         


        
16条回答
  •  无人及你
    2020-11-22 06:29

    If you will be processing all pages in order then simply remembering the last key value seen on the previous page and using TOP (25) ... WHERE Key > @last_key ORDER BY Key can be the best performing method if suitable indexes exist to allow this to be seeked efficiently - or an API cursor if they don't.

    For selecting an arbitary page the best solution for SQL Server 2005 - 2008 R2 is probably ROW_NUMBER and BETWEEN

    For SQL Server 2012+ you can use the enhanced ORDER BY clause for this need.

    SELECT  *
    FROM     MyTable 
    ORDER BY OrderingColumn ASC 
    OFFSET  50 ROWS 
    FETCH NEXT 25 ROWS ONLY 
    

    Though it remains to be seen how well performing this option will be.

提交回复
热议问题