SQL Server更新语句执行

时间:2015-04-02 10:58:07

标签: sql-server database

我需要从顶部仅更新2行。

我在这里使用像

这样的更新语句
UPDATE orderDetails 
SET status = 56 
WHERE OrderID IN (SELECT TOP 2 orderID   
                  FROM orderDetails 
                  WHERE station = 701)

但它正在更新所有行,而不考虑' TOP 2'在select语句中。

2 个答案:

答案 0 :(得分:3)

试试这个:

update top (2) orderDetails 
SET status=56 
WHERE ORDErID in (select orderID   FROM orderDetails WHERE station=701)

或者您可以尝试这样:

;with cte as
( 
select top 2 * 
from orderDetails 
WHERE station = 701
order by somecolumn    --You need to specify the column on which you will make order by
) 
update cte set status=56

答案 1 :(得分:0)

使用 order by 子句尝试查询:

UPDATE orderDetails 
SET status = 56 
WHERE OrderID IN 
(
   SELECT TOP 2 orderID   
   FROM orderDetails WHERE station = 701
   ORDER BY <COLNAME> DESC   -- You need to decide what column you want to sort on
)