I have a SQL server table in which there are 2 columns that I want to update either of their values according to a flag sent to the stored procedure along with the new value, something like:
UPDATE
table_Name
SET
CASE
WHEN @flag = '1' THEN column_A += @new_value
WHEN @flag = '0' THEN column_B += @new_value
END AS Total
WHERE
ID = @ID
What is the correct SQL server code to do so??
Something like this should work:
this worked great:
The current answers are fine and should work ok, but what's wrong with the more simple, more obvious, and more maintainable:
This is much easier to read albeit this is a very simple query.
Here's a working example courtesy of @snyder: SqlFiddle.
Since you're using SQL 2008:
If you were using SQL 2012: