postgresql 9.4儲存過程示例
阿新 • • 發佈:2019-02-07
create or replace function product_analyze_day_stat(p_stat_day timestamp) returns void as $$ /** *貨品分析日報表 */ declare v_db_info varchar(1000) := '172.17.209.165;bi_edw;u_retail_gms'; v_function_name varchar(100) := 'product_analyze_day_stat'; v_step integer := 1; v_start_time timestamp; v_end_time timestamp; v_curr_price record; begin --1.初始化工作 begin if p_stat_day is null then v_start_time := date_trunc('day',now()); v_end_time := v_start_time + interval '1 day'; else v_start_time := date_trunc('day',p_stat_day); v_end_time := v_start_time + interval '1 day'; end if; --刪除重複統計的資料 delete from item_analysis_report iar where iar.stat_day = v_start_time; perform write_log(v_db_info,v_function_name,1,'初始化工作'); exception when others then perform write_log(v_db_info,v_function_name,1,concat('初始化工作異常:',sqlerrm)); end; --2.基礎資料規總(訂貨單位、商品編碼、大類、季節、年份、風格) begin insert into item_analysis_report(stat_day,order_unit_no,item_no,item_code,item_name,category_no,sell_season,years,style) select v_start_time,ou.order_unit_no,rmt.item_no,rmt.code,rmt.name,rmt.category_no,rmt.sell_season,rmt.years,rmt.style from retail_mdm_order_unit ou,retail_mdm_order_brand_store_rel obsr,retail_mdm_item rmt where ou.order_unit_no = obsr.order_unit_no and obsr.brand_no = rmt.brand_no; perform write_log(v_db_info,v_function_name,2,'基礎資料規總'); exception when others then perform write_log(v_db_info,v_function_name,2,concat('基礎資料規總異常:',sqlerrm)); end; --3.牌價計算 begin update item_analysis_report iar set iar.tag_price = ( select rmpql.tag_price from retail_mps_price_quotation_list rmpql where rmpql.organ_id = iar.order_unit_no and rmpql.brand_no = iar.brand_no ); perform write_log(v_db_info,v_function_name,3,'牌價計算'); exception when others then perform write_log(v_db_info,v_function_name,3,concat('牌價計算異常:',sqlerrm)); end; --4.現價計算,折扣 begin for v_curr_price in( select iar.id,rppcl.price from retail_pms_price_change_list rppcl, item_analysis_report iar where rppcl.organ_id = iar.order_unit_no and rppcl.brand_no = iar.brand_no ) loop update item_analysis_report iar set iar.tag_price = coalesce(v_curr_price.price,iar.tag_price), iar.disc_rate = round(coalesce(v_curr_price.price,iar.tag_price)/coalesce(iar.tag_price,1),4) where iar.id = v_curr_price.id; end loop; perform write_log(v_db_info,v_function_name,4,'現價計算,折扣'); exception when others then perform write_log(v_db_info,v_function_name,4,concat('現價計算,折扣異常:',sqlerrm)); end; --5.上櫃日 begin perform write_log(v_db_info,v_function_name,5,'上櫃日'); exception when others then perform write_log(v_db_info,v_function_name,5,concat('上櫃日異常:',sqlerrm)); end; --6.訂貨量、補貨量 begin perform write_log(v_db_info,v_function_name,6,'訂貨量、補貨量'); exception when others then perform write_log(v_db_info,v_function_name,6,concat('訂貨量、補貨量異常:',sqlerrm)); end; --7.進貨量 begin perform write_log(v_db_info,v_function_name,7,'進貨量'); exception when others then perform write_log(v_db_info,v_function_name,7,concat('進貨量異常:',sqlerrm)); end; --8.轉入量、入在途、轉出量、退殘量 begin perform write_log(v_db_info,v_function_name,8,'轉入量、轉出量、退殘量'); exception when others then perform write_log(v_db_info,v_function_name,8,concat('轉入量、轉出量、退殘量異常:',sqlerrm)); end; --9.退殘量 begin perform write_log(v_db_info,v_function_name,9,'退殘量'); exception when others then perform write_log(v_db_info,v_function_name,9,concat('退殘量異常:',sqlerrm)); end; --10.銷售量 begin perform write_log(v_db_info,v_function_name,10,'銷售量'); exception when others then perform write_log(v_db_info,v_function_name,10,concat('銷售量異常:',sqlerrm)); end; --11.周銷售 begin perform write_log(v_db_info,v_function_name,11,'周銷售'); exception when others then perform write_log(v_db_info,v_function_name,11,concat('周銷售異常:',sqlerrm)); end; --12.財務存 begin perform write_log(v_db_info,v_function_name,12,'財務存'); exception when others then perform write_log(v_db_info,v_function_name,12,concat('財務存異常:',sqlerrm)); end; --13.周銷售 begin perform write_log(v_db_info,v_function_name,13,'周銷售'); exception when others then perform write_log(v_db_info,v_function_name,13,concat('周銷售異常:',sqlerrm)); end; --14.第一次折扣日、第二次折扣日、最後一次折扣日、一次折價、二次折價、最後一次折價 begin perform write_log(v_db_info,v_function_name,14,'第一次折扣日、第二次折扣日、最後一次折扣日、一次折價、二次折價、最後一次折價'); exception when others then perform write_log(v_db_info,v_function_name,14,concat('第一次折扣日、第二次折扣日、最後一次折扣日、一次折價、二次折價、最後一次折價異常:',sqlerrm)); end; exception when others then perform write_log(v_db_info,v_function_name,99,sqlerrm); end; $$ language plpgsql; create or replace function write_log(p_db_info varchar,p_function_name varchar,step_num integer,step_desc varchar) returns void as $$ /** *過程執行日誌+異常日誌 */ declare begin insert into t_runtime_step_log(db_info,function_name,run_step,step_desc) values(p_db_info,p_function_name,step_num,step_desc); end; $$ language plpgsql;