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

后端 未结 4 503
说谎
说谎 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: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 
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;

提交回复
热议问题