CREATE PROCEDURE GET_METR_MAN_FREQ_WISE_MBO_DETAILS
@BusLeadNm varchar(50),
@FreqDesc varchar(50),
@Month varchar(50),
@Quarter varchar(50)
AS
BEGIN
SET NOCOUNT ON;
if object_id('dbo.temp') is not null drop table dbo.temp
if @FreqDesc='Monthly'
begin
select distinct m.MBO_Name,f.Freq_Desc,ml.[Weight]/100 as WeightFin,r.End_DT as Due_DT,l.Leader_ID
,g.Goal,g.goal_ytd
,g.goal_dt,g.goal_ytd_dt,r.end_dt
from
MBO m, LKUP_Frequency f,MBO_Leader ml,Leader_NM l,Result r
,mbo_goal g
where
(l.Leader_Last+','+' '+l.Leader_First)=@BusLeadNm
and ml.Leader_ID=l.Leader_ID
and r.MBO_ID=m.MBO_ID
and m.MBO_ID=ml.MBO_ID
and f.freq_id=(select Freq_ID from lkup_frequency where freq_desc=@FreqDesc)
and m.Frequency_ID= f.freq_id
and r.Result is null
and r.Result_DT is null
and m.Owner_ID=l.Leader_ID
--and month(r.End_DT)<=month(getdate()) and month(r.End_DT)>=month(convert(datetime,'2014-02-25'))
and m.mbo_id=g.mbo_id
and month(g.End_DT)=month(r.end_dt)
and DATENAME(month,DATEADD(m,-1,r.End_DT)) = @Month
end
else if @FreqDesc='Quarterly'
begin
if @Quarter='Q1'
begin
select distinct m.MBO_Name,f.Freq_Desc,ml.[Weight]/100 as WeightFin,r.End_DT as Due_DT,l.Leader_ID
,g.Goal,g.goal_ytd
,g.goal_dt,g.goal_ytd_dt,r.end_dt
from
MBO m, LKUP_Frequency f,MBO_Leader ml,Leader_NM l,Result r
,mbo_goal g
where
(l.Leader_Last+','+' '+l.Leader_First)=@BusLeadNm
and ml.Leader_ID=l.Leader_ID
and r.MBO_ID=m.MBO_ID
and m.MBO_ID=ml.MBO_ID
and f.freq_id=(select Freq_ID from lkup_frequency where freq_desc=@FreqDesc)
and m.Frequency_ID= f.freq_id
and r.Result is null
and r.Result_DT is null
and m.Owner_ID=l.Leader_ID
--and month(r.End_DT)<=month(getdate()) and month(r.End_DT)>=month(convert(datetime,'2014-02-25'))
and m.mbo_id=g.mbo_id
and month(g.End_DT)=month(r.end_dt)
and DATENAME(month,DATEADD(m,-1,r.End_DT)) = 'March'
end
else if @Quarter='Q2'
begin
select distinct m.MBO_Name,f.Freq_Desc,ml.[Weight]/100 as WeightFin,r.End_DT as Due_DT,l.Leader_ID
,g.Goal,g.goal_ytd
,g.goal_dt,g.goal_ytd_dt,r.end_dt
from
MBO m, LKUP_Frequency f,MBO_Leader ml,Leader_NM l,Result r
,mbo_goal g
where
(l.Leader_Last+','+' '+l.Leader_First)=@BusLeadNm
and ml.Leader_ID=l.Leader_ID
and r.MBO_ID=m.MBO_ID
and m.MBO_ID=ml.MBO_ID
and f.freq_id=(select Freq_ID from lkup_frequency where freq_desc=@FreqDesc)
and m.Frequency_ID= f.freq_id
and r.Result is null
and r.Result_DT is null
and m.Owner_ID=l.Leader_ID
--and month(r.End_DT)<=month(getdate()) and month(r.End_DT)>=month(convert(datetime,'2014-02-25'))
and m.mbo_id=g.mbo_id
and month(g.End_DT)=month(r.end_dt)
and DATENAME(month,DATEADD(m,-1,r.End_DT)) = 'June'
end
else if @Quarter='Q3'
begin
select distinct m.MBO_Name,f.Freq_Desc,ml.[Weight]/100 as WeightFin,r.End_DT as Due_DT,l.Leader_ID
,g.Goal,g.goal_ytd
,g.goal_dt,g.goal_ytd_dt,r.end_dt
from
MBO m, LKUP_Frequency f,MBO_Leader ml,Leader_NM l,Result r
,mbo_goal g
where
(l.Leader_Last+','+' '+l.Leader_First)=@BusLeadNm
and ml.Leader_ID=l.Leader_ID
and r.MBO_ID=m.MBO_ID
and m.MBO_ID=ml.MBO_ID
and f.freq_id=(select Freq_ID from lkup_frequency where freq_desc=@FreqDesc)
and m.Frequency_ID= f.freq_id
and r.Result is null
and r.Result_DT is null
and m.Owner_ID=l.Leader_ID
--and month(r.End_DT)<=month(getdate()) and month(r.End_DT)>=month(convert(datetime,'2014-02-25'))
and m.mbo_id=g.mbo_id
and month(g.End_DT)=month(r.end_dt)
and DATENAME(month,DATEADD(m,-1,r.End_DT)) = 'September'
end
else if @Quarter='Q4'
begin
select distinct m.MBO_Name,f.Freq_Desc,ml.[Weight]/100 as WeightFin,r.End_DT as Due_DT,l.Leader_ID
,g.Goal,g.goal_ytd
,g.goal_dt,g.goal_ytd_dt,r.end_dt
from
MBO m, LKUP_Frequency f,MBO_Leader ml,Leader_NM l,Result r
,mbo_goal g
where
(l.Leader_Last+','+' '+l.Leader_First)=@BusLeadNm
and ml.Leader_ID=l.Leader_ID
and r.MBO_ID=m.MBO_ID
and m.MBO_ID=ml.MBO_ID
and f.freq_id=(select Freq_ID from lkup_frequency where freq_desc=@FreqDesc)
and m.Frequency_ID= f.freq_id
and r.Result is null
and r.Result_DT is null
and m.Owner_ID=l.Leader_ID
--and month(r.End_DT)<=month(getdate()) and month(r.End_DT)>=month(convert(datetime,'2014-02-25'))
and m.mbo_id=g.mbo_id
and month(g.End_DT)=month(r.end_dt)
and DATENAME(month,DATEADD(m,-1,r.End_DT)) = 'December'
end
end
else if @FreqDesc='Annually'
begin
select distinct m.MBO_Name,f.Freq_Desc,ml.[Weight]/100 as WeightFin,r.End_DT as Due_DT,l.Leader_ID
,g.Goal,g.goal_ytd
,g.goal_dt,g.goal_ytd_dt,r.end_dt
from
MBO m, LKUP_Frequency f,MBO_Leader ml,Leader_NM l,Result r
,mbo_goal g
where
(l.Leader_Last+','+' '+l.Leader_First)=@BusLeadNm
and ml.Leader_ID=l.Leader_ID
and r.MBO_ID=m.MBO_ID
and m.MBO_ID=ml.MBO_ID
and f.freq_id=(select Freq_ID from lkup_frequency where freq_desc=@FreqDesc)
and m.Frequency_ID= f.freq_id
and r.Result is null
and r.Result_DT is null
and m.Owner_ID=l.Leader_ID
--and month(r.End_DT)<=month(getdate()) and month(r.End_DT)>=month(convert(datetime,'2014-02-25'))
and m.mbo_id=g.mbo_id
and month(g.End_DT)=month(r.end_dt)
and DATENAME(month,DATEADD(m,-1,r.End_DT)) = month(getdate())
end
答案 0 :(得分:0)
首先,您必须使用选择查询创建具有相同字段的临时表,具体如下:
DECLARE @temptable TABLE (
mbo_name VARCHAR(200),
freq_desc INT,
weightfin INT,
...
)
然后在INSERT INTO temptable:
之后使用select语句INSERT INTO @temptable
SELECT distinct m.MBO_Name,f.Freq_Desc,ml.[Weight]/100 as WeightFin,r.End_DT as
Due_DT,l.Leader_ID, g.Goal,g.goal_ytd, g.goal_dt, g.goal_ytd_dt, r.end_dt
FROM
...;
DROP @temptable;
或者您可以使用更简单的方法SELECT INTO:
SELECT distinct m.MBO_Name,f.Freq_Desc,ml.[Weight]/100 as WeightFin,r.End_DT as
Due_DT,l.Leader_ID, g.Goal,g.goal_ytd, g.goal_dt, g.goal_ytd_dt, r.end_dt,
...
INTO #temptable
FROM
...;
DROP #temptable;
请注意,@
和#
,@
会创建一个表格,而#
会创建一个临时表格。