关于 游标 的问题
表acid res
500:0.5 0
2000:0.3 0
5000:0.2 0
通过游标 更新 表a 使 res 的值为 cid 字段中 “:” 前的值乘以“:”后的值。
更新完的效果如下:
cid res
500:0.5 250 (250=500*0.5)
2000:0.3 600 .....
5000:0.2 1000 .....
谁能给指点一下
[CODE]create table #t(
cid varchar(100),
res float
)
insert into #t values('500:0.5',0)
insert into #t values('2000:0.3',0)
insert into #t values('5000:0.2',0)
go
declare @cid varchar(100),@res float
declare @l varchar(50),@r varchar(50)
declare @k float
declare c cursor
for select cid,res from #t for update of res
open c
fetch c into @cid,@res
while @@fetch_status=0
begin
set @l=left(@cid,charindex(':',@cid)-1)
set @r=substring(@cid,charindex(':',@cid)+1,len(@cid)-charindex(':',@cid))
set @k=cast(@l as float)*cast(@r as float)
update #t set res=@k where current of c
fetch c into @cid,@res
end
close c
deallocate c
select * from #t
drop table #t[/CODE]