存储过程建临时表(处理复查业务时用)

 ALTER Procedure [dbo].[p_psbz_tssl_rate]
@year int
as
declare @bh numeric(18,2),@ns numeric(18,2),@lf numeric(18,2),@yt numeric(18,2),@i int,@clltotal numeric(18,2)
,@tsltotal numeric(18,2),@bz numeric(18,2),@tslyt numeric(18,2),@j int
CREATE TABLE [#t_tssl](
[nd] [varchar](4) COLLATE Chinese_PRC_CI_AS NOT NULL,
[xh] [int] NOT NULL,
[yd] [int] NULL,
[times] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL,
[clltotal] [numeric](18, 2) NULL,
[bh] [numeric](18, 2) NULL,
[ns] [numeric](18, 2) NULL,
[lf] [numeric](18, 2) NULL,
[yt] [numeric](18, 2) NULL,
[tsltotal] [numeric](18, 2) NULL,
[bz] [numeric](18, 2) NULL,
[tslyt] [numeric](18, 2) NULL,
[forecast] [numeric](18, 2) NULL,
[rate] [numeric](18, 2) NULL
)
--固定报表信息月份和序号
insert into #t_tssl(nd,xh,yd,times)
select @year,xh,yd,times from t_jy_wasteplan1 where nd = @year
set @i = 1
while @i<=12
begin
--滨河厂污水实际处理量
select @bh = isnull(sum(zclsl),0)
from PS_TZ_WSC_CLSL where year(RQ) =@year and  month(RQ)=@i and DeptID=94
--南山厂污水实际处理量
select @ns = isnull(sum(zclsl),0)
from PS_TZ_WSC_CLSL where year(RQ) =@year and  month(RQ)=@i and DeptID=93
--罗芳厂污水实际处理量
select @lf = isnull(sum(zclsl),0)
from PS_TZ_WSC_CLSL where year(RQ) =@year and  month(RQ)=@i and DeptID=95
--盐田厂污水实际处理量
select @yt = isnull(sum(zclsl),0)
from PS_TZ_WSC_CLSL where year(RQ) =@year and  month(RQ)=@i and DeptID=96
--合计
select @clltotal=@bh+@ns+@lf
--排水泵站污水实际处理量
select @bz= isnull(sum(tssl),0)
        from RZ_PSBZ_SCRB where year(RQ) = @year and month(RQ)=@i and bzbh<90
--盐田厂污水实际处理量
select @tslyt = isnull(sum(tssl),0)
from RZ_PSBZ_SCRB where year(RQ) = @year and month(RQ)=@i and bzbh in (90,91)
--合计
select @tsltotal = @bz + @tslyt
--更新
update #t_tssl set clltotal=@clltotal,bh=@bh,ns=@ns,lf=@lf,yt=@yt,tsltotal =@tsltotal,bz=@bz,tslyt=@tslyt
from #t_tssl 
where nd=@year and yd=@i
set @i = @i + 1
end
select @i=101
while @i<=104
begin
select @j=1
while @j<=4
begin
--滨河厂污水实际处理量
select @bh = isnull(sum(zclsl),0)
from V_PS_TZ_WSC_CLSL_rate where DYear =@year and  Jidu=@j and DeptID=94
--南山厂污水实际处理量
select @ns = isnull(sum(zclsl),0)
from V_PS_TZ_WSC_CLSL_rate where DYear =@year and  Jidu=@j and DeptID=93
--罗芳厂污水实际处理量
select @lf = isnull(sum(zclsl),0)
from V_PS_TZ_WSC_CLSL_rate where DYear =@year and  Jidu=@j and DeptID=95
--盐田厂污水实际处理量
select @yt = isnull(sum(zclsl),0)
from V_PS_TZ_WSC_CLSL_rate where DYear =@year and  Jidu=@j and DeptID=96
--合计
select @clltotal=@bh+@ns+@lf
        --更新

update #t_tssl set clltotal=@clltotal,bh=@bh,ns=@ns,lf=@lf,yt=@yt
from #t_tssl 
where nd=@year and yd=@i
set @j=@j+1
end
set @i=@i+1
end
--显示查询结果
select * from #t_tssl
posted @ 2014-05-08 19:34  第九剑  阅读(133)  评论(0)    收藏  举报