MySQL儲存過程語句及呼叫
BEGIN
/*
儲存過程名稱:Pro_Get_Carbon_Climate
儲存過程功能說明:不同氣候區碳排放佔比
建立日期:
涉及的表或試圖:
表或試圖名稱 用途
儲存過程呼叫:
儲存過程名稱 用途
NONE
引數說明:
傳入引數:
引數名 說明
ivF_Year 年度
返回欄位說明:
呼叫示例:
CALL Pro_Get_Carbon_Climate(ivF_Year);
CALL Pro_Get_Carbon_Climate('2018');
*/
declare SQL1 varchar(5000);
declare SQL2 varchar(5000);
declare SQLTotal varchar(5000);
declare SQLQuery varchar(5000);
-- 一次能源碳排放總量
SET SQL1='select t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year,
sum(t2.F_Value*t3.F_NCV*t3.F_EF) as TPF_HL,sum(t2.F_Value*t3.F_Formular) as ZBM
FROM t_organization t1
join t_energy_data t2 ON t1.F_ID = t2.F_OrgID
join v_zjpflist t3 on t2.F_FieldID=t3.F_FieldID
where t3.F_IsSum=1 ';
SET SQL1 = CONCAT(SQL1,' and t2.F_Year= ', ivF_Year);
-- 二次能源碳排放總量
SET SQL2='select t1.F_ID, t2.F_Year,
sum(CASE WHEN t3.F_CalcType = 2 THEN(t2.F_Value * t3.F_EF)END ) as TPF_DL,
sum(CASE WHEN t3.F_CalcType = 3 THEN(t2.F_Value * t3.F_EF) END ) as TPF_RL,
sum(t2.F_Value*t3.F_Formular) as ZBM
FROM t_organization t1
join t_energy_data t2 ON t1.F_ID = t2.F_OrgID
join v_jjpflist t3 on t2.F_FieldID=t3.F_FieldID AND t1.F_ID =T3.F_OrgID
where t3.F_IsSum=1 ';
SET SQL2 = CONCAT(SQL2,' and t2.F_Year=', ivF_Year);
SET SQL1 = CONCAT(SQL1,' group by t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year ');
SET SQL2 = CONCAT(SQL2,' group by t1.F_ID, t2.F_Year ');
#SELECT SQL1;# 輸出拼接sql語句,相當於print
#SELECT SQL2;# 輸出拼接sql語句,相當於print
SET SQLTotal='select AA.F_Climate, SUM(AA.F_BuildArea) AS F_BuildArea, SUM(AA.F_EnergyPerson) AS F_EnergyPerson,
SUM(AA.ZBM + BB.ZBM) AS F_TotalZBM,
SUM(AA.TPF_HL + BB.TPF_DL + BB.TPF_RL) AS F_TotalTPF,
SUM(AA.TPF_HL) AS TPF_HL,
SUM(BB.TPF_DL) AS TPF_DL,
SUM(BB.TPF_RL) AS TPF_RL ';
SET SQLTotal = CONCAT(SQLTotal,' from (', SQL1,') AA JOIN (',SQL2,') BB ON AA.F_ID=BB.F_ID AND AA.F_Year=BB.F_Year ');
SET SQLTotal = CONCAT(SQLTotal,' GROUP BY AA.F_Climate ');
#SELECT SQLTotal;
SET SQLQuery ='select BBB.F_Climate, BBB.F_Name, AAA.F_TotalTPF ';
SET SQLQuery = CONCAT(SQLQuery,' from (select a.F_Code as F_Climate ,a.F_Name,a.F_Order from t_dt_datadict a where a.F_DictType=2) BBB LEFT JOIN (',SQLTotal,') AAA ON AAA.F_Climate=BBB.F_Climate ');
SET SQLQuery = CONCAT(SQLQuery,' ORDER BY BBB.F_Order ');
#SELECT SQLQuery;
SET @SQLQuery = SQLQuery;
prepare s1 from @SQLQuery;
EXECUTE s1 ;
deallocate prepare s1 ;
END
BEGIN
/*
儲存過程名稱:Pro_Get_CO2
儲存過程功能說明:不同型別機構碳排放佔比
建立日期:
涉及的表或試圖:
表或試圖名稱 用途
儲存過程呼叫:
儲存過程名稱 用途
NONE
引數說明:
傳入引數:
引數名 說明
ivF_Year 年度
返回欄位說明:
呼叫示例:
CALL Pro_Get_Carbon_OrgType(ivF_Year);
CALL Pro_Get_Carbon_OrgType('2018');
*/
declare SQL1 varchar(5000);
declare SQL2 varchar(5000);
declare SQLTotal varchar(5000);
declare SQLQuery varchar(5000);
-- 一次能源碳排放總量
SET SQL1='select t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year,
sum(t2.F_Value*t3.F_NCV*t3.F_EF) as TPF_HL,sum(t2.F_Value*t3.F_Formular) as ZBM
FROM t_organization t1
join t_energy_data t2 ON t1.F_ID = t2.F_OrgID
join v_zjpflist t3 on t2.F_FieldID=t3.F_FieldID
where t3.F_IsSum=1 ';
SET SQL1 = CONCAT(SQL1,' and t2.F_Year= ', ivF_Year);
-- 二次能源碳排放總量
SET SQL2='select t1.F_ID, t2.F_Year,
sum(CASE WHEN t3.F_CalcType = 2 THEN(t2.F_Value * t3.F_EF)END ) as TPF_DL,
sum(CASE WHEN t3.F_CalcType = 3 THEN(t2.F_Value * t3.F_EF) END ) as TPF_RL,
sum(t2.F_Value*t3.F_Formular) as ZBM
FROM t_organization t1
join t_energy_data t2 ON t1.F_ID = t2.F_OrgID
join v_jjpflist t3 on t2.F_FieldID=t3.F_FieldID AND t1.F_ID =T3.F_OrgID
where t3.F_IsSum=1 ';
SET SQL2 = CONCAT(SQL2,' and t2.F_Year=', ivF_Year);
SET SQL1 = CONCAT(SQL1,' group by t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year ');
SET SQL2 = CONCAT(SQL2,' group by t1.F_ID, t2.F_Year ');
#SELECT SQL1;# 輸出拼接sql語句,相當於print
#SELECT SQL2;# 輸出拼接sql語句,相當於print
SET SQLTotal='select AA.F_OrgType, SUM(AA.F_BuildArea) AS F_BuildArea, SUM(AA.F_EnergyPerson) AS F_EnergyPerson,
SUM(AA.ZBM + BB.ZBM) AS F_TotalZBM,
SUM(AA.TPF_HL + BB.TPF_DL + BB.TPF_RL) AS F_TotalTPF,
SUM(AA.TPF_HL) AS TPF_HL,
SUM(BB.TPF_DL) AS TPF_DL,
SUM(BB.TPF_RL) AS TPF_RL ';
SET SQLTotal = CONCAT(SQLTotal,' from (', SQL1,') AA JOIN (',SQL2,') BB ON AA.F_ID=BB.F_ID AND AA.F_Year=BB.F_Year ');
SET SQLTotal = CONCAT(SQLTotal,' GROUP BY AA.F_OrgType ');
#SELECT SQLTotal;
SET SQLQuery ='select BBB.F_OrgType, BBB.F_Name, AAA.F_TotalTPF ';
SET SQLQuery = CONCAT(SQLQuery,' from (select a.F_Code as F_OrgType ,a.F_Name,a.F_Order from t_dt_datadict a where a.F_DictType=1) BBB LEFT JOIN (',SQLTotal,') AAA ON AAA.F_OrgType=BBB.F_OrgType ');
SET SQLQuery = CONCAT(SQLQuery,' ORDER BY BBB.F_Order ');
#SELECT SQLQuery;
SET @SQLQuery = SQLQuery;
prepare s1 from @SQLQuery;
EXECUTE s1 ;
deallocate prepare s1 ;
END
BEGIN
/*
儲存過程名稱:Pro_Get_CO2
儲存過程功能說明:獲得年度統計分析(碳排放,折標煤)
建立日期:
涉及的表或試圖:
表或試圖名稱 用途
儲存過程呼叫:
儲存過程名稱 用途
NONE
引數說明:
傳入引數:
引數名 說明
F_Year 年度
F_ProvinceCode 地區
F_Climate 氣候區
F_OrgType 機構型別
返回欄位說明:
呼叫示例:
CALL Pro_Get_CO2(F_Year,F_ProvinceCode,F_Climate,F_OrgType);
CALL Pro_Get_CO2('2018','','','');
*/
declare SQL1 varchar(5000);
declare SQL2 varchar(5000);
declare SQLTotal varchar(5000);
-- 一次能源碳排放總量
SET SQL1='select t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year,
sum(t2.F_Value*t3.F_NCV*t3.F_EF) as TPF_HL,sum(t2.F_Value*t3.F_Formular) as ZBM
FROM t_organization t1
join t_energy_data t2 ON t1.F_ID = t2.F_OrgID
join v_zjpflist t3 on t2.F_FieldID=t3.F_FieldID
where t3.F_IsSum=1 ';
-- 二次能源碳排放總量
SET SQL2='select t1.F_ID, t2.F_Year,
sum(CASE WHEN t3.F_CalcType = 2 THEN(t2.F_Value * t3.F_EF)END ) as TPF_DL,
sum(CASE WHEN t3.F_CalcType = 3 THEN(t2.F_Value * t3.F_EF) END ) as TPF_RL,
sum(t2.F_Value*t3.F_Formular) as ZBM
FROM t_organization t1
join t_energy_data t2 ON t1.F_ID = t2.F_OrgID
join v_jjpflist t3 on t2.F_FieldID=t3.F_FieldID AND t1.F_ID =T3.F_OrgID
where t3.F_IsSum=1 ';
IF F_Year IS NOT NULL AND F_Year <> '' THEN
SET SQL1 = CONCAT(SQL1,' and t2.F_Year=', F_Year);
SET SQL2 = CONCAT(SQL2,' and t2.F_Year=', F_Year);
END IF;
IF F_ProvinceCode IS NOT NULL AND F_ProvinceCode <> '' THEN
SET SQL1 = CONCAT(SQL1,' and t1.F_ProvinceCode=''', F_ProvinceCode,''' ');
SET SQL2 = CONCAT(SQL2,' and t1.F_ProvinceCode=''', F_ProvinceCode,''' ');
END IF;
IF F_Climate IS NOT NULL AND F_Climate <> '' THEN
SET SQL1 = CONCAT(SQL1,' and t1.F_Climate=''', F_Climate,''' ');
SET SQL2 = CONCAT(SQL2,' and t1.F_Climate=''', F_Climate,''' ');
END IF;
IF F_OrgType IS NOT NULL AND F_OrgType <> '' THEN
SET SQL1 = CONCAT(SQL1,' and t1.F_OrgType=''', F_OrgType,''' ');
SET SQL2 = CONCAT(SQL2,' and t1.F_OrgType=''', F_OrgType,''' ');
END IF;
SET SQL1 = CONCAT(SQL1,' group by t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year ');
SET SQL2 = CONCAT(SQL2,' group by t1.F_ID, t2.F_Year ');
#SELECT SQL1;# 輸出拼接sql語句,相當於print
#SELECT SQL2;# 輸出拼接sql語句,相當於print
SET SQLTotal='select AA.F_OrgName, AA.F_ProvinceCode,AA.F_Climate, AA.F_OrgType, AA.F_BuildArea, AA.F_EnergyPerson, AA.F_Year,
AA.ZBM+BB.ZBM AS F_TotalZBM,
CASE WHEN AA.F_BuildArea=0 THEN 0 ELSE (AA.ZBM+BB.ZBM )/AA.F_BuildArea END AS F_PerAreaZBM,
CASE WHEN AA.F_EnergyPerson=0 THEN 0 ELSE (AA.ZBM+BB.ZBM )/AA.F_EnergyPerson END AS F_PerPersonZBM,
AA.TPF_HL+BB.TPF_DL+BB.TPF_RL AS F_TotalTPF,AA.TPF_HL,BB.TPF_DL,BB.TPF_RL,
CASE WHEN AA.F_BuildArea=0 THEN 0 ELSE (AA.TPF_HL+BB.TPF_DL+BB.TPF_RL )/AA.F_BuildArea END AS F_PerAreaTPF,
CASE WHEN AA.F_EnergyPerson=0 THEN 0 ELSE (AA.TPF_HL+BB.TPF_DL+BB.TPF_RL )/AA.F_EnergyPerson END AS F_PerPersonTPF ';
SET SQLTotal = CONCAT(SQLTotal,' from (', SQL1,') AA JOIN (',SQL2,') BB ON AA.F_ID=BB.F_ID AND AA.F_Year=BB.F_Year ');
SET SQLTotal = CONCAT(SQLTotal,' ORDER BY AA.F_ID,AA.F_Year ');
#SELECT SQLTotal;# 輸出拼接sql語句,相當於print
SET @SQLTotal = SQLTotal;
prepare s1 from @SQLTotal;
EXECUTE s1 ;
deallocate prepare s1 ;
END
BEGIN
/*
儲存過程名稱:Pro_Get_CO2
儲存過程功能說明:獲得近5年的能耗資料,碳排放資料
建立日期:
涉及的表或試圖:
表或試圖名稱 用途
儲存過程呼叫:
儲存過程名稱 用途
NONE
引數說明:
傳入引數:
引數名 說明
ivF_Year 年度
返回欄位說明:
呼叫示例:
CALL Pro_Get_EnergyData(ivF_Year);
CALL Pro_Get_EnergyData('2018');
*/
declare SQL0 varchar(5000);
declare SQL1 varchar(5000);
declare SQL2 varchar(5000);
declare SQLTotal varchar(5000);
declare SQLQuery varchar(5000);
-- 構造近5年列表
SET SQL0=CONCAT('SELECT @cdate :=@cdate-1 AS F_Year FROM (SELECT @cdate := ',ivF_Year+1,' FROM at_sys_errorlog LIMIT 5 ) t4');
-- 一次能源碳排放總量
SET SQL1='select t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year,
sum(t2.F_Value*t3.F_NCV*t3.F_EF) as TPF_HL,sum(t2.F_Value*t3.F_Formular) as ZBM
FROM t_organization t1
join t_energy_data t2 ON t1.F_ID = t2.F_OrgID
join v_zjpflist t3 on t2.F_FieldID=t3.F_FieldID
where t3.F_IsSum=1 ';
SET SQL1 = CONCAT(SQL1,' and t2.F_Year>= ', ivF_Year-4,' and t2.F_Year<= ', ivF_Year);
-- 二次能源碳排放總量
SET SQL2='select t1.F_ID, t2.F_Year,
sum(CASE WHEN t3.F_CalcType = 2 THEN(t2.F_Value * t3.F_EF)END ) as TPF_DL,
sum(CASE WHEN t3.F_CalcType = 3 THEN(t2.F_Value * t3.F_EF) END ) as TPF_RL,
sum(t2.F_Value*t3.F_Formular) as ZBM
FROM t_organization t1
join t_energy_data t2 ON t1.F_ID = t2.F_OrgID
join v_jjpflist t3 on t2.F_FieldID=t3.F_FieldID AND t1.F_ID =T3.F_OrgID
where t3.F_IsSum=1 ';
SET SQL2 = CONCAT(SQL2,' and t2.F_Year>= ', ivF_Year-4,' and t2.F_Year<= ', ivF_Year);
SET SQL1 = CONCAT(SQL1,' group by t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year ');
SET SQL2 = CONCAT(SQL2,' group by t1.F_ID, t2.F_Year ');
#SELECT SQL1;# 輸出拼接sql語句,相當於print
#SELECT SQL2;# 輸出拼接sql語句,相當於print
SET SQLTotal='select AA.F_Year, SUM(AA.F_BuildArea) AS F_BuildArea, SUM(AA.F_EnergyPerson) AS F_EnergyPerson,
SUM(AA.ZBM + BB.ZBM) AS F_TotalZBM,
SUM(AA.TPF_HL + BB.TPF_DL + BB.TPF_RL) AS F_TotalTPF,
SUM(AA.TPF_HL) AS TPF_HL,
SUM(BB.TPF_DL) AS TPF_DL,
SUM(BB.TPF_RL) AS TPF_RL ';
SET SQLTotal = CONCAT(SQLTotal,' from (', SQL1,') AA JOIN (',SQL2,') BB ON AA.F_ID=BB.F_ID AND AA.F_Year=BB.F_Year ');
SET SQLTotal = CONCAT(SQLTotal,' GROUP BY AA.F_Year ');
#SELECT SQLTotal;
SET SQLQuery ='select BBB.F_Year, AAA.F_BuildArea, AAA.F_EnergyPerson,AAA.F_TotalZBM,
CASE WHEN AAA.F_BuildArea=0 THEN 0 ELSE AAA.F_TotalZBM/AAA.F_BuildArea END AS F_PerAreaZBM,
CASE WHEN AAA.F_EnergyPerson=0 THEN 0 ELSE AAA.F_TotalZBM/AAA.F_EnergyPerson END AS F_PerPersonZBM,
AAA.F_TotalTPF,
CASE WHEN AAA.F_BuildArea=0 THEN 0 ELSE AAA.F_TotalTPF/AAA.F_BuildArea END AS F_PerAreaTPF,
CASE WHEN AAA.F_EnergyPerson=0 THEN 0 ELSE AAA.F_TotalTPF/AAA.F_EnergyPerson END AS F_PerPersonTPF ';
SET SQLQuery = CONCAT(SQLQuery,' from (', SQL0,') BBB LEFT JOIN (',SQLTotal,') AAA ON AAA.F_Year=BBB.F_Year ');
SET SQLQuery = CONCAT(SQLQuery,' ORDER BY BBB.F_Year ');
#SELECT SQLQuery;
SET @SQLQuery = SQLQuery;
prepare s1 from @SQLQuery;
EXECUTE s1 ;
deallocate prepare s1 ;
END
儲存過程呼叫方式:
CALL Pro_Get_CO2('2018','','','');
CALL Pro_Get_EnergyData('2017');
CALL Pro_Get_Carbon_OrgType('2014');
CALL Pro_Get_Carbon_Climate('2014');
相關文章
- mysql如何呼叫儲存過程MySql儲存過程
- 呼叫儲存過程儲存過程
- Mysql儲存過程 變數,條件,迴圈語句用法MySql儲存過程變數
- mysql多次呼叫儲存過程的問題MySql儲存過程
- 深入解讀MySQL InnoDB儲存引擎Update語句執行過程MySql儲存引擎
- linux呼叫儲存過程Linux儲存過程
- Winform呼叫儲存過程ORM儲存過程
- EF中使用SQL語句或儲存過程SQL儲存過程
- PostgreSQL獲取建表語句儲存過程SQL儲存過程
- SQL 儲存過程裡呼叫另一個儲存過程SQL儲存過程
- mysql 儲存過程MySql儲存過程
- jsp中呼叫儲存過程JS儲存過程
- Oracle儲存過程乾貨(二):PLSQL控制語句Oracle儲存過程SQL
- MySQL 儲存函式及呼叫MySql儲存函式
- MySQL之儲存過程MySql儲存過程
- MySQL---------儲存過程MySql儲存過程
- mysql儲存過程整理MySql儲存過程
- Sqlsugar呼叫Oracle的儲存過程SqlSugarOracle儲存過程
- Oracle儲存過程乾貨(三):PLSQL迴圈語句Oracle儲存過程SQL
- 金倉資料庫KingbaseES儲存過程 RETURN語句資料庫儲存過程
- mysql儲存過程及日期函式實踐MySql儲存過程函式
- Mysql 儲存過程的使用MySql儲存過程
- Oracle 儲存過程分頁 + Sqlsugar呼叫Oracle儲存過程SqlSugar
- mysql執行sql語句過程MySql
- MySQL儲存過程 (即函式)MySql儲存過程函式
- mySql 儲存過程與函式MySql儲存過程函式
- Laravel 中使用 MySQL 儲存過程LaravelMySql儲存過程
- MySql資料庫——儲存過程MySql資料庫儲存過程
- mysql儲存過程的引數MySql儲存過程
- MySQL 儲存過程和函式MySql儲存過程函式
- MySQL儲存過程和函式MySql儲存過程函式
- mssql 儲存過程呼叫另一個儲存過程中的結果的方法分享SQL儲存過程
- ibatis呼叫oracle儲存過程(極簡版)BATOracle儲存過程
- jdbc使用call呼叫儲存過程報錯JDBC儲存過程
- mysql和orcale的儲存過程和儲存函式MySql儲存過程儲存函式
- MySQL儲存過程中如何使用ROLLBACKMySql儲存過程
- Mysql 5.7儲存過程的學習MySql儲存過程
- MySQL--儲存過程與檢視MySql儲存過程