当前位置: 代码迷 >> Sql Server >> 怎么在update中作条件判断
  详细解决方案

怎么在update中作条件判断

热度:100   发布时间:2016-04-27 20:03:29.0
如何在update中作条件判断
有一个表table1  
里面有3个列column1   column2   columnid

现在要更新表
if     @parm1!=0,[email protected]
        @parm2!=0,[email protected]


如果用2条UPDATE语句很好实现
update   table1
set   column1=case   when   @parm1!=0   then   [email protected]   end  
where   columnid=1

update   table1
set   column2=case   when   @parm2!=0   then   [email protected]   end  
where   columnid=1

但是如果只使用一次UPDATE语句如何实现?(因为实际的情况是有100列,如果使用100次UPDATE肯定会造成效率低下)

------解决方案--------------------
update table1
set column1=case when @parm1!=0 then [email protected] end ,
column2=case when @parm2!=0 then [email protected] end
where columnid=1

不过你的语句应该有问题,[email protected]!=0 会把column1更新成null的,如果想保持不变,应该
update table1
set column1=case when @parm1!=0 then [email protected] else column1 end ,
column2=case when @parm2!=0 then [email protected] else column2 end
where columnid=1

  相关解决方案