Is there a 'START AT' equivalent in MS-SQL?

后端 未结 4 504
说谎
说谎 2021-02-08 06:45

Some databases support commands such as:

SELECT TOP 10 START AT 10 * FROM 

Essentially I need to pull the first 10 records, then t

相关标签:
4条回答
  • 2021-02-08 06:51

    Which version of SQL Server?

    In SQL Server 2000 this is a real pain (though possible using ugly tricks like that posted by stingyjack).

    In 2005 and later it's a little easier- look at the Row_Number() function.

    And, depending on your client application it may not even be that hard. Some of the ASP.Net grid controls have support for automatic paging.

    0 讨论(0)
  • 2021-02-08 06:55

    If you want to be compatible with SQL Server 2000 you could use

    SELECT * FROM
    (
        SELECT TOP 10 FROM
        (
            SELECT TOP (n * 10) FROM <table> ORDER BY (column) ASC
        ) AS t1 ORDER BY (column) DESC
    ) AS t2 ORDER BY (column) ASC
    

    In SQL Server 2005 there is a new function Row_Number(). You could use it this way:

    WITH Orders AS 
    (
         SELECT SalesOrderID, OrderDate, 
         ROW_NUMBER() OVER (order by OrderDate) AS 'RowNumber' 
         FROM SalesOrder
    ) 
    SELECT * 
    FROM Orders 
    WHERE RowNumber between 10 and 19;
    
    0 讨论(0)
  • 2021-02-08 06:56
    SELECT Top 10 * 
    FROM Table 
    WHERE <primary key> Not IN (
        SELECT Top 10 <primaryKey> 
        FROM Table 
        ORDER BY <primary Key> ASC) 
    ORDER BY <primary Key> ASC
    
    0 讨论(0)
  • 2021-02-08 06:58

    For SQL Server 2012

    SELECT * 
    FROM <TABLE>
    ORDER BY <SomeCol>
    OFFSET 10 ROWS
    FETCH NEXT 10 ROWS ONLY;
    
    0 讨论(0)
提交回复
热议问题