如何在where条件下更新多行

How to update multiple rows on where condition

我有以下 table 数据 我想使用默认 数量 =1 更新行,但 ChildItemID 列上的最大(序列)组除外。 我也在下面写了查询。

 UPDATE #tempBOM1 SET Quantity=1
    WHERE [Sequence] < (SELECT MAX([Sequence]) FROM #tempBOM1 GROUP BY ChildItemID) and ChildItemID is not null;

with cte as (
    select 
        t.*, 
        max(sequence) over(partition by childItemID) max_sequence
    from mytable t
)
update cte 
set quantity = 1
where sequence <> max_sequence

SELECT * FROM cte

您可以使用 window 函数和可更新的 CTE:

with cte as (
    select 
        t.*, 
        max(sequence) over(partition by childItemID) max_sequence
    from mytable t
)
update cte 
set quantity = 1
where sequence <> max_sequence