簡體   English   中英

在存儲過程mysql中執行多個動態查詢

[英]Execute multiple dynamic query in stored procedure mysql

我想執行查詢並將結果存儲在臨時表中。 然后我要使用臨時表。 查詢是動態創建的,存儲過程如下:

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;

上面的代碼是否正確? 如何使用?

為什么不使用view 如果您不想更改表格,這將很有幫助:

在SQL中,視圖是基於SQL語句的結果集的虛擬表。

視圖包含行和列,就像真實表一樣。 視圖中的字段是數據庫中一個或多個實際表中的字段。

您可以將SQL函數,WHERE和JOIN語句添加到視圖中,並像顯示數據來自單個表一樣顯示數據。

您可以閱讀有關w3school中的視圖的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM