How can I delete or select a row from a table that

2019-04-12 23:06发布

问题:

I have a question about Microsoft SQL Server 2005. How can I delete or select a row from a table that has a specific row number?

回答1:

Edit: Modified the code so that it matches more closely to OP's intentions

Declare @RowNum as INT
SET @RowNum = 15 ---Just for example

WITH OrdersRN AS
(
    SELECT ROW_NUMBER() OVER(ORDER BY OrderDate, OrderID) AS RowNum
          ,OrderID
          ,OrderDate
          ,CustomerID
          ,EmployeeID
      FROM dbo.Orders
)

SELECT * 
  FROM OrdersRN
 WHERE RowNum = @RowNum
 ORDER BY OrderDate
         ,OrderID;


回答2:

Check this URL out. Since SQL Server 2005, there is a function called "row_number()" that is what you are looking for.