how can I Update top 100 records in sql server

前端 未结 7 1183
醉梦人生
醉梦人生 2020-11-27 10:03

I want to update the top 100 records in SQL Server. I have a table T1 with fields F1 and F2. T1 has 200 records. I want

7条回答
  •  离开以前
    2020-11-27 10:23

    Without an ORDER BY the whole idea of TOP doesn't make much sense. You need to have a consistent definition of which direction is "up" and which is "down" for the concept of top to be meaningful.

    Nonetheless SQL Server allows it but doesn't guarantee a deterministic result.

    The UPDATE TOP syntax in the accepted answer does not support an ORDER BY clause but it is possible to get deterministic semantics here by using a CTE or derived table to define the desired sort order as below.

    ;WITH CTE AS 
    ( 
    SELECT TOP 100 * 
    FROM T1 
    ORDER BY F2 
    ) 
    UPDATE CTE SET F1='foo'
    

提交回复
热议问题