if condition in sql server update query

前端 未结 5 796
日久生厌
日久生厌 2021-02-01 16:29

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, so

5条回答
  •  孤街浪徒
    2021-02-01 17:10

    Something like this should work:

    UPDATE
        table_Name
    SET 
      column_A = CASE WHEN @flag = '1' THEN column_A + @new_value ELSE column_A END,
      column_B = CASE WHEN @flag = '0' THEN column_B + @new_value ELSE column_B END
    WHERE
        ID = @ID
    

提交回复
热议问题