查询:如果sql server中的条件更新(query: if condition in sql server update)
我有一个SQL服务器表,其中有两列我想根据发送到存储过程的标志和新值更新其值,如:
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什么是正确的SQL服务器代码?
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 = @IDWhat is the correct SQL server code to do so??
最满意答案
我认为M.Ali的评论是正确的,所以我根据他的建议构建了这个。 我还假设状态字段是“已批准”或“已拒绝”,如您所说,是否已填充。 如果状态字段中有任何其他条件,那么您必须在语句中添加这些条件
BEGIN TRANSACTION Update Payment set post_date = new_postdate_value account_num = new_account_num_value pay_am = new_pay_am_value pay_type = new_pay_type_value authoriz = new_authoriz_value where status is not null UPDATE Payment SET account_num = new_account_num_value WHERE status is null COMMIT TRANSACTIONI thought M.Ali's comment was correct, so I've constructed this based on his suggestion. I'm also assuming the status field is 'approved' or 'declined' as you say based on if it's populated or not. If there are any other conditions on the status field, offcourse you must add these to you where statements
BEGIN TRANSACTION Update Payment set post_date = new_postdate_value account_num = new_account_num_value pay_am = new_pay_am_value pay_type = new_pay_type_value authoriz = new_authoriz_value where status is not null UPDATE Payment SET account_num = new_account_num_value WHERE status is null COMMIT TRANSACTION更多推荐
发布评论