更新由两列选择的多个记录/行



我一直在尝试编写一个更新查询来更新几行/记录,以便我可以使用批量更新来测试更新触发器。我在某处看到它做了类似的事情

update atbv_Sales_OrdersLines 
set Amount = case OrderID
when 5 then 10
when 6 then 11
end
where OrderID in (5, 6)

但是我需要根据两列选择行,但我无法做到这一点 尝试类似的东西

set Amount = case OrderID, ProductID
when 6, 1 then 10

但正如预期的那样,这并不好

怎么样...

update atbv_Sales_OrdersLines 
set Amount = OrderId + 5
where (OrderId = 6 and ProductID = 1) or
(OrderId = 5...)

update atbv_Sales_OrdersLines 
set Amount =
CASE
WHEN OrderId = 6 and ProductId = 1 THEN 11
WHEN OrderId = 5 and ProductId = 2 THEN 10
...
END
where ...

您可能应该阅读Microsoft关于 CASE 表达式的文档,并特别注意示例"H. 在 UPDATE 语句中使用 CASE">

试试这个:

case
when OrderID in (6, 1) then 10
...
end

最新更新