蓝色天空
close you eyes and open your mind
posts - 3,comments - 0,trackbacks - 0

 

问题          最近在程序实践课上在做一个管理系统,其中有统计某种产品某种型号需要多少个,其中不同员工不同工种发放的数量,发放的周期也不一样,这样统计起来比较麻烦,可以用游标建立一个临时表先以产品,型号,发放数量 聚合算出个数,然后对临时表进行统计。
 1create proc RequirementStat--根据发放规则统计出到未来某天对用品的需求量
 2@date datetime
 3as
 4
 5declare @thname varchar(20)
 6declare @size varchar(20)
 7declare @num int
 8
 9--创建一个临时表临时表用来存放第一次统计的结果
10create table #t
11(
12    用品名称 varchar(20),
13    型号 varchar(20),
14    数量 int
15)
16
17
18--声明一个游标
19declare c_total cursor for
20select g1.用品名称,型号,datediff(day,getDate(),@date)/发放周期*g1.发放数量 数量 from GrantData g1,发放规则 f,工种 w,用品 t 
21where f.工种编码=w.工种编码 and f.用品编码=t.用品编码 and t.用品名称=g1.用品名称 and w.工种名称=g1.工种名称 and datediff(day,getDate(),@date)>=0
22group by g1.用品名称,型号,g1.发放数量,发放周期
23
24open c_total
25--读取数据
26fetch next from c_total into @thname,@size,@num
27while @@fetch_status=0
28begin
29    --动态增加一组到临时表
30    insert into #t values(@thname,@size,@num)
31    fetch next from c_total into @thname,@size,@num
32end
33close c_total    
34deallocate c_total
35select 用品名称,型号,sum(数量) 数量 from #t group by 用品名称,型号
36drop table #t
37
38
39GO

       

 

posted on 2008-05-04 15:07 编程爱好者 阅读(218) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。