游标操作实例

来源:互联网 发布:什么叫网络打印机 编辑:程序博客网 时间:2024/06/06 02:21
更新歌手MV歌曲数和歌手总人气
declare @gsid int
declare @songnum int
declare @songallhit int
declare rq cursor for select distinct gsid from MV歌曲表 where sh=1 and isok=0 order by gsid asc
open rq
 fetch next from rq into @gsid 
 while @@FETCH_STATUS = 0
 begin
   select @songnum =count(id),@songallhit=sum(allhit) from MV歌曲表 where gsid=@gsid and sh=1 and isok=0

  update 歌手表 set mvnum=@songnum,mvrq=@songallhit
where gsid= @gsid 
       fetch next from rq into @gsid
 end
 close rq
 deallocate rq