update different columns based on criterea in one update statement
I have 7 quantity columns in a table representing the order for each day of a week for a customer...this is by design and cannot be changed. Is it possible to perform an update like this
update customerOrder
case weekday(someDate) when 'SUN' then set Quantity1 = 1 end,
case weekday(someDate) when 'MON' then set Quantity2 = 1 end,
case weekday(someDate) when 'TUE' then set Quantity3 = 1 end,
case weekday(someDate) when 'WED' then set Quantity4 = 1 end,
case weekday(someDate) when 'THU' then set Quantity5 = 1 end,
case weekday(someDate) when 'FRI' then set Quantity6 = 1 end开发者_StackOverflow社区,
case weekday(someDate) when 'SAT' then set Quantity7 = 1 end
WHERE accountNumber = 'ABC123'
Currently I am checking the "someDate" and executing a specific update statement. I was just wondering if it might be possible to wrap all this in one update statement within a stored procedure.
UPDATE
customerOrder
SET
Quantity1 = CASE WEEKDAY(someDate) WHEN 'SUN' THEN 1 ELSE Quantity1 END,
Quantity2 = CASE WEEKDAY(someDate) WHEN 'MON' THEN 1 ELSE Quantity2 END,
Quantity3 = CASE WEEKDAY(someDate) WHEN 'TUE' THEN 1 ELSE Quantity3 END,
Quantity4 = CASE WEEKDAY(someDate) WHEN 'WED' THEN 1 ELSE Quantity4 END,
Quantity5 = CASE WEEKDAY(someDate) WHEN 'THU' THEN 1 ELSE Quantity5 END,
Quantity6 = CASE WEEKDAY(someDate) WHEN 'FRI' THEN 1 ELSE Quantity6 END,
Quantity7 = CASE WEEKDAY(someDate) WHEN 'SAT' THEN 1 ELSE Quantity7 END
WHERE
accountNumber = 'ABC123'
精彩评论