我想执行查询并将结果存储在临时表中。然后我想使用临时表。查询是动态创建的,存储过程如下:
CREATE DEFINER=`be4`@`%` PROCEDURE `Report_filter_Avg`(in _year nvarchar(500),in _month nvarchar(500), in _region nvarchar(500),
in _team nvarchar(500),in _project nvarchar(50000),in _netmr nvarchar(10),in _proposal nvarchar(10),in _maconomy nvarchar(500))
BEGIN
if(_year!='') then begin set @year_=concat(' and ',_year); end; else set @year_=''; end if;
if(_month!='') then begin set @month_=concat(' and ',_month); end; else set @month_=''; end if;
if(_region!='') then begin set @region_=concat(' and ',_region); end; else set @region_=''; end if;
if(_team!='') then begin set @team_=concat(' and ',_team); end; else set @team_=''; end if;
if(_project!='') then begin set @project_=concat(' and ',_project); end; else set @project_=''; end if;
if(_netmr!='') then begin set @netmr_=concat(' and netmr_sn=\'',_netmr,'\''); end;else set @netmr_=''; end if;
if(_proposal!='') then begin set @proposal_=concat(' and Proposal_sn=\'',_proposal,'\''); end; else set @proposal_=''; end if;
if(_maconomy!='') then begin set @maconomy_=concat(' and Maconomy_no like \'',_maconomy,'%\''); end; else set @maconomy_=''; end if;
set @query_T=concat('create temporary table tbl engine=memory select * from(
select year, month,Team_Name,sum(studies)Studies, sum(SO)Hrs from production_report where SO > 0 ',coalesce(@year_,''),coalesce(@month_,''),coalesce(@region_,''),
coalesce(@team_,''),coalesce(@project_,''),coalesce(@netmr_,''),coalesce(@proposal_,''),coalesce(@maconomy_,''),' group by Year,Month,Team_Name union all
select year, month,concat(Region_Name,''_SP'')Team_Name,sum(studies)Studies, sum(SP)Hrs from production_report where SP > 0 ',coalesce(@year_,''),coalesce(@month_,''),coalesce(@region_,''),
coalesce(@team_,''),coalesce(@project_,''),coalesce(@netmr_,''),coalesce(@proposal_,''),coalesce(@maconomy_,''),' group by Year,Month,Team_Name union all
select year, month,concat(Region_Name,''_DP'')Team_Name,sum(studies)Studies, sum(DP)Hrs from production_report where DP > 0 ',coalesce(@year_,''),coalesce(@month_,''),coalesce(@region_,''),
coalesce(@team_,''),coalesce(@project_,''),coalesce(@netmr_,''),coalesce(@proposal_,''),coalesce(@maconomy_,''),' group by Year,Month,Team_Name union all
select year, month,concat(Region_Name,''_CS'')Team_Name,sum(studies)Studies, sum(CS)Hrs from production_report where CS > 0 ',coalesce(@year_,''),coalesce(@month_,''),coalesce(@region_,''),
coalesce(@team_,''),coalesce(@project_,''),coalesce(@netmr_,''),coalesce(@proposal_,''),coalesce(@maconomy_,''),' group by Year,Month,Team_Name union all
select year, month,Region_Name,sum(studies)Studies, sum(TAB)Hrs from production_report where TAB > 0 ',coalesce(@year_,''),coalesce(@month_,''),coalesce(@region_,''),
coalesce(@team_,''),coalesce(@project_,''),coalesce(@netmr_,''),coalesce(@proposal_,''),coalesce(@maconomy_,''),' group by Year,Month,Team_Name union all
select year, month,''OE'',sum(studies)Studies, sum(OE)Hrs from production_report where OE > 0 ',coalesce(@year_,''),coalesce(@month_,''),coalesce(@region_,''),
coalesce(@team_,''),coalesce(@project_,''),coalesce(@netmr_,''),coalesce(@proposal_,''),coalesce(@maconomy_,''),' group by Year,Month,Team_Name
)B;');
PREPARE st FROM @query_T;
EXECUTE st;
DEALLOCATE PREPARE st;
set @query_C=('create temporary table tbl_count(select Year,Month,Team_Name,count(Month)count from tbl group by Year,Team_Name);');
PREPARE st FROM @query_C;
EXECUTE st;
DEALLOCATE PREPARE st;
set @query_M=('select B.Year,concat(B.Team_Name,''('',B.Year,'')'')Team_Name,sum(Jan)Jan,sum(Feb)Feb,sum(Mar)Mar,sum(Apr)Apr,sum(May)May,sum(Jun)Jun,sum(Jul)Jul,
sum(Aug)Aug,sum(sep)Sep,sum(Oct)Oct,sum(Nov)Nov,sum(dec_)Dec_,round((sum(Jan)+sum(Feb)+sum(Mar)+sum(Apr)+sum(May)+sum(Jun)+sum(Jul)+sum(Aug)
+sum(sep)+sum(Oct)+sum(Nov)+sum(Dec_))/count,1) Total from(
SELECT Year,Month,Team_Name,case when Month=1 then Hrs else 0 end Jan,case when Month=2 then Hrs else 0 end Feb,
case when Month=3 then Hrs else 0 end Mar,case when Month=4 then Hrs else 0 end Apr,case when Month=5 then Hrs else 0 end May,
case when Month=6 then Hrs else 0 end Jun,case when Month=7 then Hrs else 0 end Jul,case when Month=8 then Hrs else 0 end Aug,
case when Month=9 then Hrs else 0 end Sep,case when Month=10 then Hrs else 0 end Oct,case when Month=11 then Hrs else 0 end Nov,
case when Month=12 then Hrs else 0 end Dec_ from(
select Year,Month,Team_Name,round(Hrs/Studies,1)Hrs from tbl group by Team_Name,Month,Year)A group by Year,Month,Team_Name
)B left join tbl_count on tbl_count.Team_Name=B.Team_Name and tbl_count.Year=B.Year
group by Year,Team_Name order by Team_Name ASC;');
PREPARE st FROM @query_M;
EXECUTE st;
DEALLOCATE PREPARE st;
set @query_D1=('drop temporary table tbl;drop temporary table tbl_count;');
PREPARE st FROM @query_D;
EXECUTE st;
DEALLOCATE PREPARE st;
set @query_D2=('drop temporary table tbl_count;');
PREPARE st FROM @query_D2;
EXECUTE st;
DEALLOCATE PREPARE st;
以上代码是否正确?我该怎么用?
答案 0 :(得分:0)
为什么不使用view
?如果你不想换桌子,这会很有帮助:
在SQL中,视图是基于SQL语句的结果集的虚拟表。
视图包含行和列,就像真实表一样。视图中的字段是数据库中一个或多个真实表的字段。
您可以将SQL函数,WHERE和JOIN语句添加到视图中,并将数据呈现为数据来自一个表。
您可以在w3school
中详细了解观看次数