游标的使用,进行数据迭代相加。

来源:互联网 发布:锐捷查看网络接口 编辑:程序博客网 时间:2024/05/17 04:57


DECLARE date_cursor CURSOR FOR select distinct(date) from test order by date desc

OPEN date_cursor
DECLARE @minTime datetime,@dayTime datetime
select @minTime=min(date) from test where date is not null
print @minTime
FETCH NEXT FROM date_cursor INTO @dayTime
while @dayTime>@minTime
begin
 insert into test(number) select sum(number) from test where date < @dayTime
 FETCH NEXT FROM date_cursor INTO @dayTime
end
CLOSE date_cursor
DEALLOCATE date_cursor