展开全部
sql2000的update和select都可以使用变量赋值,使用这点可以比较有效率地实现很多循环计算问题,比如LZ这题create table table1(id int,bb float,mingxi int,ee float)
insert into table1(id,bb,mingxi)
select 1,10,1
union all select 1,100,2
union all select 1,80,3
union all select 2,20,1
union all select 2,40,2
union all select 2,20,3
union all select 3,10,1
union all select 3,20,2
declare @e68a84e8a2ad62616964757a686964616f31333332633036id int,@ee float,@bb float
update table1 set @ee=case when isnull(@id,-1)=ID then bb/@bb else bb/2 end,ee=@ee,@bb=bb,@id=id
如果数据顺序是默认按 id 和 mingxi从小到大排列的话,一句update即可实现此功能
如果原始数据不是按id和mingxi从小到大排列,不能直接update,也可以通过先将数据插入临时表后再更新,如select * into #table1 from table1 order by id,mingxi
declare @id int,@ee float,@bb float
update #table1 set @ee=case when isnull(@id,-1)=ID then bb/@bb else bb/2 end,ee=@ee,@bb=bb,@id=id
最后再把#table1的ee根据id和mingxi写回table1