日期:2014-05-18  浏览次数:20460 次

整列条件更新问题
表kk中有id,a1,a2,a3,b1,b2,b3,c等8列,表有N行的记录。
id,a1,a2,a3,b1,b2,b3,为数值,a3,b3,有可能为Null,
c为字符
c值的定义是:
a1+a2+a3> b1+b2+b3时,取A
a1+a2+a3=b1+b2+b3时,取B
a1+a2+a3 <b1+b2+b3时,取C
-----

--我写的代码是
declare   @i   int
set   @i=1
while   @i <=(select   count(1)   from   kk)
begin
update   kk  
set   c=case
when   a1+a2+isnull(a3,0)> b1+b2+isnull(b3,0)   then   'A '
when   a1+a2+isnull(a3,0)=b1+b2+isnull(b3,0)   then   'B '
when   a1+a2+isnull(a3,0) <b1+b2+isnull(b3,0)   then   'C
set   @i=@i+1
end
---
求更简洁或更好的代码?


------解决方案--------------------
--try

update kk
set c=case
when a1+a2+isnull(a3,0)> b1+b2+isnull(b3,0) then 'A '
when a1+a2+isnull(a3,0)=b1+b2+isnull(b3,0) then 'B '
when a1+a2+isnull(a3,0) <b1+b2+isnull(b3,0) then 'C '
end
------解决方案--------------------
update kk set c=char(66-sign(a1-b1+a2-b2+isnull(a3,0)-isnull(b3,0)))
这样应该不会了