查询:如果sql server中的条件更新(query: if condition in sql server update)

编程入门 行业动态 更新时间:2024-10-18 01:38:55
查询:如果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 = @ID

What 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 TRANSACTION

I 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

更多推荐

本文发布于:2023-07-07 21:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068480.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   server   sql   update   condition

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!