1
create proc RequirementStat--根据发放规则统计出到未来某天对用品的需求量
2
@date datetime
3
as
4
5
declare @thname varchar(20)
6
declare @size varchar(20)
7
declare @num int
8
9
--创建一个临时表临时表用来存放第一次统计的结果
10
create table #t
11
(
12
用品名称 varchar(20),
13
型号 varchar(20),
14
数量 int
15
)
16
17
18
--声明一个游标
19
declare c_total cursor for
20
select g1.用品名称,型号,datediff(day,getDate(),@date)/发放周期*g1.发放数量 数量 from GrantData g1,发放规则 f,工种 w,用品 t
21
where f.工种编码=w.工种编码 and f.用品编码=t.用品编码 and t.用品名称=g1.用品名称 and w.工种名称=g1.工种名称 and datediff(day,getDate(),@date)>=0
22
group by g1.用品名称,型号,g1.发放数量,发放周期
23
24
open c_total
25
--读取数据
26
fetch next from c_total into @thname,@size,@num
27
while @@fetch_status=0
28
begin
29
--动态增加一组到临时表
30
insert into #t values(@thname,@size,@num)
31
fetch next from c_total into @thname,@size,@num
32
end
33
close c_total
34
deallocate c_total
35
select 用品名称,型号,sum(数量) 数量 from #t group by 用品名称,型号
36
drop table #t
37
38
39
GO
posted on 2008-05-04 15:07
编程爱好者 阅读(234)
评论(0) 编辑 收藏 引用