使用临时表的存储过程

下面是一个使用临时表的存储过程例子

USE [JointFrame31_zw]
GO
/****** Object:  StoredProcedure [dbo].[Proc_ZL_GetMonthFlow]    Script Date: 2017/04/05 9:18:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        <水狼一族>
-- Create date: <2017.04.05>
-- Description:    <查看全市污染物各月份的排放量情况>
-- TSQL: Proc_ZL_GetMonthFlow 
-- =============================================

ALTER PROCEDURE [dbo].[Proc_ZL_GetMonthFlow]

AS
SET NOCOUNT ON 
begin
--创建废水临时表
    create table #w(MonitorDate varchar(6),CODFlow numeric(18,6),NH4Flow numeric(18,6))
 --插入废水临时表
    insert into #w
    exec [AutoMonitor].[dbo].Proc_Big_Get_Pollutant_Month_Data_Water
--创建废气临时表
    create table #g(
    MonitorDate varchar(6),
    YCFlow numeric(18,4),
    S02Flow numeric(18,4),
    NOXFlow numeric(18,4)
    )
 --创建废气临时表
    insert into #g
    exec [AutoMonitor].[dbo]. Proc_Big_Get_Pollutant_Month_Data_Gas

    select MonitorDate into #date from #w a union select b.MonitorDate from #g b

    select p.*,isnull(CODFlow,0) CODPercent,isnull(NH4Flow,0) NH4Percent,isnull(YCFlow,0) YCPercent,
    isnull(S02Flow,0) S02Percent,isnull(NOXFlow,0) NOXPercent
     from #date p left join #w a on p.MonitorDate = a.MonitorDate
    left join #g b on p.MonitorDate = b.MonitorDate

    truncate table #w
    drop table #w

    truncate table #g
    drop table #g

    truncate table #date
    drop table #date

end
原文地址:https://www.cnblogs.com/shuilangyizu/p/6669573.html