日期:2014-05-19  浏览次数:20402 次

如何在update中作条件判断
有一个表table1  
里面有3个列column1   column2   columnid

现在要更新表
if     @parm1!=0,column1=column1/@parm1
        @parm2!=0,column2=column2/@parm2


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

update   table1
set   column2=case   when   @parm2!=0   then   column2/@parm2   end  
where   columnid=1

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

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

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