Row Offset in SQL Server

后端 未结 16 2383
醉酒成梦
醉酒成梦 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:37

    With SQL Server 2012 (11.x) and later and Azure SQL Database, you can also have "fetch_row_count_expression", you can also have ORDER BY clause along with this.

    USE AdventureWorks2012;  
    GO  
    -- Specifying variables for OFFSET and FETCH values    
    DECLARE @skip int = 0  , @take int = 8;  
    SELECT DepartmentID, Name, GroupName  
    FROM HumanResources.Department  
    ORDER BY DepartmentID ASC   
        OFFSET @skip ROWS   
        FETCH NEXT @take ROWS ONLY; 
    

    https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql?view=sql-server-ver15

    Note OFFSET Specifies the number of rows to skip before it starts to return rows from the query expression. It is NOT the starting row number. So, it has to be 0 to include first record.

提交回复
热议问题