NulisDefo
NulisDefo

Reputation: 335

Updating multiple records/rows selected by two columns

I've been trying to write an update query that would update several rows/records so that i could test update trigger with batch updates. I've seen somewhere it done something like that

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

but I need to select rows based on two columns and I can't manage to do it tries something like

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

but as expected it's no good

Upvotes: 0

Views: 42

Answers (2)

user4843530
user4843530

Reputation:

What about...

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

or

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 ...

You should probably read Microsoft's documentation on the CASE expression and pay especial attention to example "H. Using CASE in an UPDATE statement"

Upvotes: 2

Jeff Breadner
Jeff Breadner

Reputation: 1448

Try this:

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

Upvotes: 0

Related Questions