個人一個PLSQL存儲過程【我】 改版,加入日誌表

建立日誌表sql:node

-- Create table
create table PROCEDURE_LOG
(
  ID      VARCHAR2(100) not null,
  NAME    VARCHAR2(1000),
  CODE    NUMBER,
  MSG     VARCHAR2(3000),
  IN_DATE DATE
)
tablespace CUSTINCOME
  pctfree 10
  initrans 1
  maxtrans 255;
-- Add comments to the table 
comment on table PROCEDURE_LOG
  is '存儲過程日誌表(測試用)';
-- Add comments to the columns 
comment on column PROCEDURE_LOG.ID
  is '主鍵';
comment on column PROCEDURE_LOG.NAME
  is '存儲過程名稱PROCEDURE_NAME';
comment on column PROCEDURE_LOG.CODE
  is '單次執行編碼ONCE_CODE';
comment on column PROCEDURE_LOG.MSG
  is '存儲的信息';
comment on column PROCEDURE_LOG.IN_DATE
  is '插入時間';
-- Create/Recreate primary, unique and foreign key constraints 
alter table PROCEDURE_LOG
  add constraint PRI_KEY_PROCEDURE_LOG primary key (ID)
  using index 
  tablespace CUSTINCOME
  pctfree 10
  initrans 2
  maxtrans 255;

 

 

 

 

 

CREATE OR REPLACE PROCEDURE PRO_REVENUE_STATISTICS

IS
--DECLARE
       --日誌單次code
       log_code number; 
       --存儲過程名稱
       log_name varchar2(100) := 'PRO_REVENUE_STATISTICS';
       --日誌信息
       log_msg varchar2(3000) := '';   

       --插入數據計數器
       ins_counter PLS_INTEGER := 0;
       
       --所有客戶數量
       l_all_cust_num REVENUE_STATISTICS_RES.All_Cust_Num%TYPE;
    
    --所有收入
       l_all_income REVENUE_STATISTICS_RES.All_Income%TYPE;
       --當前帳期月份字符串默認12個月
       l_month_str varchar2(200) := '2019012';
       --當前帳期月份數字
       l_month_num number(20) ;
       --當前所有收入查詢條件,動態sql
       l_month_select varchar2(10000) := '' ;
BEGIN
       
       --給日誌單次code賦值
       select  SEQ_REVENUE_STATISTICS_RES.NEXTVAL into log_code from dual;
       
       --計算所有客戶數
       SELECT sum(CUST_TOTAL) count into l_all_cust_num from provi_cust_total;
       --計算截止到當前帳期的總收入
       --計算當前最大月份201909
       SELECT MAX(ACCOUNT_DAY) into l_month_str FROM ct_lan_node_inc;
       l_month_num := TO_NUMBER(SUBSTR(l_month_str,5));
       --DBMS_OUTPUT.PUT_LINE('l_month_num:'||l_month_num);
       
       FOR moth_index IN 1 .. (l_month_num)
       LOOP
           l_month_select := l_month_select || 'SUM(TY_' || moth_index || ')+';
       END LOOP;
       --去掉最後一個加號
       l_month_select := substr(l_month_select,0,length(l_month_select)-1); 
       --拼接查詢
       l_month_select := 'SELECT /*+ PARALLEL(12) */(' || l_month_select ||') FROM EDA_CUST_INC';
       --執行動態sql查詢字符串
       EXECUTE IMMEDIATE l_month_select into l_all_income;
       
       --獲取省份遊標
       FOR prov_rec IN (SELECT REGION_ID,CUST_TOTAL,REGION_NBR,REGION_NAME FROM PROVI_CUST_TOTAL p LEFT JOIN common_region c ON p.region_id = c.common_region_id)
       LOOP
           <<prov_loop>>
           --獲取市遊標
           FOR city_rec IN (SELECT COMMON_REGION_ID,REGION_NBR, REGION_NAME FROM COMMON_REGION WHERE PAR_REGION_ID = prov_rec.REGION_ID)
           LOOP
               --獲取產品遊標
               FOR product_rec IN (select NAME,CODE FROM PRODUCT_CAT)
               LOOP
                   --獲取行業遊標
                   FOR industry_rec  IN (  select INDUSTRY_TYPE_ID,
                                           PAR_INDUSTRY_TYPE_ID,
                                           INDUSTRY_TYPE_GRADE,
                                           INDUSTRY_TYPE_CODE,
                                           INDUSTRY_TYPE_NAME
                                      from industry_type)
                   LOOP
                       --獲取身份證信息
                       FOR identity_index IN 1..2
                       LOOP
                          DECLARE
                              --定義單條查詢結果遊標
                              CURSOR revenues_cur IS
                                    SELECT /*+ PARALLEL(12) */
                                       COUNT(e.CUST_ID) AUDIT_CUST_NUM,COUNT(DISTINCT(e.PARTY_ID)) AUDIT_CUST_PARTY_NUM,
                                       SUM(TY_1) TY_1 ,SUM(TY_2) TY_2,SUM(TY_3) TY_3,SUM(TY_4) TY_4,SUM(TY_5) TY_5,SUM(TY_6) TY_6,SUM(TY_7) TY_7,SUM(TY_8) TY_8,SUM(TY_9) TY_9,SUM(TY_10) TY_10,SUM(TY_11) TY_11,SUM(TY_12) TY_12,
                                       SUM(LY_1) LY_1 ,SUM(LY_2) LY_2,SUM(LY_3) LY_3,SUM(LY_4) LY_4,SUM(LY_5) LY_5,SUM(LY_6) LY_6,SUM(LY_7) LY_7,SUM(LY_8) LY_8,SUM(LY_9) LY_9,SUM(LY_10) LY_10,SUM(LY_11) LY_11,SUM(LY_12) LY_12
                                    FROM EDA_CUST_INC e
                                    WHERE 1=1
                                     AND   e.STD_PRVNCE_CD = prov_rec.REGION_NBR
                                     AND   e.STD_LATN_CD =  city_rec.REGION_NBR
                                     AND   e.PROD_TYPE = product_rec.CODE
                                     AND   EXISTS (SELECT 1
                                                    FROM CUST_CP_MERGE M
                                                   WHERE e.CUST_ID = M.CUST_ID
                                                     AND e.STD_LATN_CD = M.STD_LATN_CD)
                                     AND e.PARTY_ID IS NOT NULL AND UPPER(e.PARTY_ID) != 'NULL'
                                     AND EXISTS (SELECT 1
                                          FROM PARTY P
                                         WHERE e.PARTY_ID = p.PARTY_ID
                                           AND p.IDENTITY_TYPE = identity_index)
                                     and exists (select 1
                                          from party_org po
                                         where e.party_id = po.party_id
                                              and po.INDUSTRY_TYPE_ID = industry_rec.INDUSTRY_TYPE_ID);
                              --定義單條查詢結果變量
                              revenue_rec revenues_cur%ROWTYPE;
                          BEGIN
                               --打開遊標
                               OPEN revenues_cur;
                               --從遊標取數據給變量賦值
                               FETCH revenues_cur INTO revenue_rec;
                               --關閉單條遊標
                               CLOSE revenues_cur;
                               
                                --插入結果表
                               INSERT INTO REVENUE_STATISTICS_RES2
                                    (
                                     ID,
                                     PROVINCE_REGION_ID,
                                     CITY_REGION_ID,
                                     REGION_NAME,
                                     PROD_TYPE,
                                     INDUSTRY_TYPE_ID,
                                     INDUSTRY_TYPE_CODE,
                                     INDUSTRY_TYPE_NAME,
                                     PAR_INDUSTRY_TYPE_ID,
                                     INDUSTRY_TYPE_GRADE,
                                     IDENTITY_TYPE,
                                     IN_DATE,
                                     AUDIT_CUST_NUM,
                                     AUDIT_CUST_PARTY_NUM,
                                     ALL_CUST_NUM,
                                     ALL_INCOME,
                                     TY_1,
                                     TY_2,
                                     TY_3,
                                     TY_4,
                                     TY_5,
                                     TY_6,
                                     TY_7,
                                     TY_8,
                                     TY_9,
                                     TY_10,
                                     TY_11,
                                     TY_12,
                                     LY_1,
                                     LY_2,
                                     LY_3,
                                     LY_4,
                                     LY_5,
                                     LY_6,
                                     LY_7,
                                     LY_8,
                                     LY_9,
                                     LY_10,
                                     LY_11,
                                     LY_12
                                    )
                                VALUES(
                                     SEQ_REVENUE_STATISTICS_RES.NEXTVAL,
                                     prov_rec.REGION_ID,
                                     city_rec.COMMON_REGION_ID,
                                     city_rec.region_name,
                                     product_rec.code,
                                     industry_rec.INDUSTRY_TYPE_ID,
                                     industry_rec.INDUSTRY_TYPE_CODE,
                                     industry_rec.INDUSTRY_TYPE_NAME,
                                     industry_rec.PAR_INDUSTRY_TYPE_ID,
                                     industry_rec.INDUSTRY_TYPE_GRADE,
                                     identity_index,
                                     sysdate,
                                     revenue_rec.AUDIT_CUST_NUM,
                                     revenue_rec.AUDIT_CUST_PARTY_NUM,
                                     l_all_cust_num,
                                     l_all_income,
                                     revenue_rec.TY_1,
                                     revenue_rec.TY_2,
                                     revenue_rec.TY_3,
                                     revenue_rec.TY_4,
                                     revenue_rec.TY_5,
                                     revenue_rec.TY_6,
                                     revenue_rec.TY_7,
                                     revenue_rec.TY_8,
                                     revenue_rec.TY_9,
                                     revenue_rec.TY_10,
                                     revenue_rec.TY_11,
                                     revenue_rec.TY_12,
                                     revenue_rec.LY_1,
                                     revenue_rec.LY_2,
                                     revenue_rec.LY_3,
                                     revenue_rec.LY_4,
                                     revenue_rec.LY_5,
                                     revenue_rec.LY_6,
                                     revenue_rec.LY_7,
                                     revenue_rec.LY_8,
                                     revenue_rec.LY_9,
                                     revenue_rec.LY_10,
                                     revenue_rec.LY_11,
                                     revenue_rec.LY_12
                                );
                                --COMMIT;
                                --計數器變量自增
                                ins_counter := ins_counter+1;
                                --判斷數量,若是數量到5000條提交一下事務
                                /*IF MOD(ins_counter,5000)=0
                                THEN
                                   COMMIT;
                                END IF;*/
                              --結束內部塊
                              END;
                           --EXIT  prov_loop;--退出到最外循環
                         END LOOP identity_loop;
                   END loop industry_loop;
               END loop product_loop;
           END LOOP city_loop;
           --DBMS_OUTPUT.PUT_LINE('--開始提交事務:'||ins_counter);
           insert into PROCEDURE_LOG values(SEQ_REVENUE_STATISTICS_RES.NEXTVAL,log_name,log_code,'開始提交事務:'||ins_counter,sysdate);
           --提交事務,插入數據和日誌
           COMMIT;
           --DBMS_OUTPUT.PUT_LINE('--結束提交事務:'||ins_counter);
           insert into PROCEDURE_LOG values(SEQ_REVENUE_STATISTICS_RES.NEXTVAL,log_name,log_code,'結束提交事務:'||ins_counter,sysdate);
           --提交事務當即執行插入日誌
           COMMIT;
       END LOOP prov_loop;
       --DBMS_OUTPUT.PUT_LINE('所有結束:'||ins_counter);
       insert into PROCEDURE_LOG values(SEQ_REVENUE_STATISTICS_RES.NEXTVAL,log_name,log_code,'所有結束:'||ins_counter,sysdate);
       --提交事務當即執行插入日誌
       COMMIT;
    
END PRO_REVENUE_STATISTICS;
相關文章
相關標籤/搜索