使用游标显示销售报表_使用游标和临时表

create table TotalMileByMonth

(

TMonth char(8),

TMile numeric(10,0)

)

select * into #temp from

(select PM,PD,sum(M) as M from

(select SUBSTRING ( PD , 1 , 6 ) as PM,SUBSTRING ( PD , 1 , 4 )+'/'+SUBSTRING ( PD , 5 , 2 ) as PD ,M

from dbo.GCCM) a group by PM,[PD]) b

Declare @PM varchar(8)

Declare @TempMile numeric(10,0)

DECLARE My_CURSOR CURSOR FOR select PM from #temp

open My_CURSOR

FETCH NEXT FROM My_CURSOR INTO @PM

WHILE @@FETCH_STATUS = 0

BEGIN

select @TempMile=sum(M) from #temp where PM<= @PM

insert into TotalMileByMonth values(@PM,@TempMile)

FETCH NEXT FROM My_CURSOR INTO @PM

END

CLOSE My_CURSOR --关闭游标

DEALLOCATE My_CURSOR

select c.PM,c.PD,c.M,d.TMile from #temp c inner join TotalMileByMonth d

on c.PM=d.TMonth

drop table #temp

drop table TotalMileByMonth

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值