如何在sql server中更新基于条件的列名? [英] How update conditionbased column name in sql server?

查看:68
本文介绍了如何在sql server中更新基于条件的列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

declare @Qtr int =1;
 update Appraisal_QtrUsers_Details  set ( case when @Qtr=1 then Q1AppseKRA_SubDate
 when @Qtr=2 then Q2AppseKRA_SubDate
  when @Qtr=3 then Q3AppseKRA_SubDate
  when @Qtr=4 then Q4AppseKRA_SubDate else null end )= null
  where user_id=9177

推荐答案

更正的sql是 -

Corrected sql is-

declare @Qtr int =1;
update Appraisal_QtrUsers_Details  
set  Q1AppseKRA_SubDate=(case when @Qtr=1 then null else Q1AppseKRA_SubDate end),
     Q2AppseKRA_SubDate=(case when @Qtr=2 then null else Q2AppseKRA_SubDate end),
     Q3AppseKRA_SubDate=(case when @Qtr=3 then null else Q3AppseKRA_SubDate end),
     Q4AppseKRA_SubDate=(case when @Qtr=4 then null else Q4AppseKRA_SubDate end)
where user_id=9177

此查询背后的逻辑是,如果列不是应该使用NULL值进行更新,然后只使用其当前值更新它,并且不会导致任何值更改。

The logic behind this query is if the column is not supposed to be updated with NULL value then just update it with the its current value and will not result in any value change.

注意:如果可以共享会更好此查询的一些业务逻辑,以便更容易建议您更正确的解决方案。

Note: It would be better if you can share some business logic for this query so that it would be easier to suggest you more correct solution.

谢谢:)


这篇关于如何在sql server中更新基于条件的列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆