不知道我的存儲過程寫的好不好?

不知道這樣寫存儲過程,是不是不夠優化?
  1. create or replace function F_GET_WORK_DAYS_COLOR   
  2. (   
  3.   in_begin_date  DATE,   
  4.   in_end_date    DATE,   
  5.   in_first_days  number,   
  6.   in_second_days number   
  7.  )   
  8. return varchar2 is  
  9.   
  10. /*=========================================================   
  11. 功能:去掉休息日,去工作日,   
  12. 說明:傳入日期 和 工作日天數,返回到期時間   
  13. 創建:   
  14. 修改:   
  15. 時間:2006-12-12   
  16. =========================================================*/   
  17.   
  18. v_color        varchar(20);   
  19. v_days         number(6);   
  20. --v_exception Exception;   
  21.   
  22.   
  23.   
  24. begin  
  25.   
  26.   
  27.  select count(1)    
  28.    into v_days    
  29.    from ZX_CALENDAR_AND_REST z   
  30.   where z.calendar_day >= trunc(in_begin_date)   
  31.     and z.calendar_day <= trunc(in_end_date)   
  32.     and z.rest_flag = 'N';   
  33.   
  34.   if v_days  > in_first_days and v_days  <= in_second_days then  
  35.      v_color := 'yellow';   
  36.   elsif v_days  > in_second_days  then  
  37.      v_color :=  'red';   
  38.   else  
  39.      v_color := 'white';   
  40.   end if;   
  41.   
  42.   return v_color;   
  43.   
  44. exception   
  45.   when others then  
  46.     rollback;   
  47.    -- p_pub_error_log(v_result, sqlcode, sqlerrm, 'sf_f_note_issue_confirm');   
  48.     return sysdate;   
  49. end;   
  1. create or replace function F_ZX_WORK_SHEET_VERIFY_NOTICES(   
  2.   begin_day   date,   
  3.   end_day     date,   
  4.   v_operator  varchar2,   
  5.   stat_bureau varchar2)   
  6. return integer is  
  7.  ------------------------------------------------------------------------------------------   
  8.  -- 功能:工單通告預警   
  9.  -- 參數:當前日期   
  10.  -- 創建: 2007-1-24   
  11.  -- 修改:   
  12.  ------------------------------------------------------------------------------------------   
  13.   
  14.  v_day     date;   
  15.  v_end_day date;   
  16.  v_busi    varchar2(8);   
  17.  v_result  integer;   
  18. begin  
  19.   v_day := sysdate ;   
  20.   v_busi := stat_bureau;   
  21.   v_end_day := end_day;   
  22.   --初使化v_busi   
  23.     if v_busi = '%' then  
  24.        v_busi := 'TT';   
  25.     end if;   
  26.      --清除已經發布的內容   
  27.   v_result := -10;   
  28.   delete from ZX_WORK_SHEET_VERIFY_NOTICES h   
  29.   where h.start_day   = begin_day and  
  30.         h.end_day     = v_end_day and  
  31.         (h.STAT_BUREAU = v_busi or h.bureau_code = v_busi);    --新加的列,統計單位   
  32.    dbms_output.put_line('liuhui');   
  33.    --插入數據   
  34.    v_result := -20;   
  35. if v_busi = 'TT' then    
  36.   insert into ZX_WORK_SHEET_VERIFY_NOTICES   
  37.   (   
  38.     NOTICE_LEVEL ,   
  39.     NOTICE_DAY   ,   
  40.     SHEET_NUM   ,   
  41.     SHEET_RATE   ,   
  42.     RATE_NUM     ,   
  43.     REMARK      ,   
  44.     BUREAU_CODE ,   
  45.     OPERATOR    ,   
  46.     DATA_TIME   ,   
  47.     START_DAY   ,   
  48.     END_DAY     ,   
  49.     STAT_BUREAU )   
  50.    SELECT DECODE(W.COLOR,'red',1,'yellow',2,3),   
  51.           v_day,   
  52.           W.NUM,   
  53.           0,   
  54.           0,   
  55.           DECODE(W.COLOR,'red','非常嚴重','yellow','嚴重',''),   
  56.           W.BUREAU_CODE,   
  57.           v_operator ,   
  58.           v_day,   
  59.           begin_day ,   
  60.           v_end_day ,   
  61.           v_busi   
  62.     FROM  
  63.    (select count(1)  NUM,   
  64.            z.bureau_code   BUREAU_CODE,   
  65.    max(F_GET_WORK_DAYS_COLOR(z.confirm_date,decode(z.back_verify_date,'',sysdate,z.back_verify_date),3,13))  COLOR   
  66.      from ZX_WORK_SHEET_VERIFY z   
  67.      where F_GET_WORK_DAYS_COLOR(z.confirm_date,decode(z.back_verify_date,'',sysdate,z.back_verify_date),3,13) <> 'white'   
  68.      and z.confirm_sign = 'Y' and z.app_date between begin_day and v_end_day  and (z.error_type <> '0' or z.error_type is not null)   
  69.      group by z.bureau_code,F_GET_WORK_DAYS_COLOR(z.confirm_date,decode(z.back_verify_date,'',sysdate,z.back_verify_date),3,13)   
  70.      ORDER BY COLOR,NUM ) W;   
  71. ELSE  
  72.      
  73.   insert into ZX_WORK_SHEET_VERIFY_NOTICES   
  74.   (   
  75.     NOTICE_LEVEL ,   
  76.     NOTICE_DAY   ,   
  77.     SHEET_NUM   ,   
  78.     SHEET_RATE   ,   
  79.     RATE_NUM     ,   
  80.     REMARK      ,   
  81.     BUREAU_CODE ,   
  82.     OPERATOR    ,   
  83.     DATA_TIME   ,   
  84.     START_DAY   ,   
  85.     END_DAY     ,   
  86.     STAT_BUREAU )   
  87.    SELECT DECODE(W.COLOR,'red',1,'yellow',2,3),   
  88.           v_day,   
  89.           W.NUM,   
  90.           0,   
  91.           0,   
  92.           DECODE(W.COLOR,'red','非常嚴重','yellow','嚴重',''),   
  93.           W.BUREAU_CODE,   
  94.           v_operator ,   
  95.           v_day,   
  96.           begin_day ,   
  97.           v_end_day ,   
  98.           v_busi   
  99.     FROM  
  100.    (select count(1)  NUM,   
  101.            z.bureau_code   BUREAU_CODE,   
  102.    max(F_GET_WORK_DAYS_COLOR(z.confirm_date,decode(z.back_verify_date,'',sysdate,z.back_verify_date),3,13))  COLOR   
  103.      from ZX_WORK_SHEET_VERIFY z   
  104.      where F_GET_WORK_DAYS_COLOR(z.confirm_date,decode(z.back_verify_date,'',sysdate,z.back_verify_date),3,13) <> 'white'   
  105.      and z.confirm_sign = 'Y' and z.app_date between begin_day and v_end_day  and (z.error_type <> '0' or z.error_type is not null)   
  106.      group by z.bureau_code,F_GET_WORK_DAYS_COLOR(z.confirm_date,decode(z.back_verify_date,'',sysdate,z.back_verify_date),3,13)   
  107.      ORDER BY COLOR,NUM ) W    
  108.      WHERE W.BUREAU_CODE = v_busi;   
  109.   end if;   
  110.   ------合計   
  111.   v_result := -30;   
  112. IF v_busi = 'TT' then  
  113.   insert into ZX_WORK_SHEET_VERIFY_NOTICES   
  114.   (   
  115.     NOTICE_LEVEL ,   
  116.     NOTICE_DAY   ,   
  117.     SHEET_NUM   ,   
  118.     SHEET_RATE   ,   
  119.     RATE_NUM     ,   
  120.     REMARK      ,   
  121.     BUREAU_CODE ,   
  122.     OPERATOR    ,   
  123.     DATA_TIME   ,   
  124.     START_DAY   ,   
  125.     END_DAY     ,   
  126.     STAT_BUREAU )   
  127.    SELECT DECODE(t.notice_level,'',3,t.notice_level),   
  128.           v_day,   
  129.           sum(nvl(t.sheet_num,0)),   
  130.           '',   
  131.           max(0),   
  132.           max(t.remark),   
  133.           'TT'  ,   
  134.           v_operator ,   
  135.           v_day,   
  136.           begin_day ,   
  137.           v_end_day ,   
  138.           v_busi   
  139.     from  ZX_WORK_SHEET_VERIFY_NOTICES t   
  140.    where t.start_day = begin_day   
  141.      and t.end_day   = v_end_day   
  142.      and t.bureau_code <> v_busi    
  143.      and t.stat_bureau = v_busi    
  144.    group by rollup(t.notice_level);   
  145.   
  146. end if;   
  147.   
  148.   ----工單超期率 不包括天津的   
  149.     v_result := -40;   
  150. if v_busi = 'TT' then    
  151.     update ZX_WORK_SHEET_VERIFY_NOTICES  B   
  152.        set (B.SHEET_RATE) = (select round(decode((select count(*) from ZX_WORK_SHEET_VERIFY z    
  153.                               where z.app_date between begin_day and v_end_day   
  154.                                 and z.bureau_code = a.bureau_code    
  155.                                 and (z.confirm_date <> date'1900-1-1' or z.confirm_date is not null)),0,0,   
  156.                             (select sum(c.SHEET_NUM) from ZX_WORK_SHEET_VERIFY_NOTICES c   
  157.                               where c.bureau_code  = a.bureau_code    
  158.                                 and a.notice_level = c.notice_level   
  159.                                 and b.start_day    = c.start_day    
  160.                                 and c.end_day      = b.end_day and c.bureau_code <> v_busi and    
  161.                                 c.stat_bureau = v_busi )   
  162.                                 /   
  163.                              (select count(*) from ZX_WORK_SHEET_VERIFY z   
  164.                                where z.app_date between begin_day and v_end_day   
  165.                                  and z.bureau_code  = a.bureau_code    
  166.                                  and (z.confirm_date <> date'1900-1-1' or z.confirm_date is not null))),4)*100   
  167.                                 from ZX_WORK_SHEET_VERIFY_NOTICES a    
  168.                                WHERE A.BUREAU_CODE  = B.BUREAU_CODE    
  169.                                  AND A.NOTICE_LEVEL = B.NOTICE_LEVEL   
  170.                                  and b.start_day    = a.start_day    
  171.                                  and a.end_day      = b.end_day    
  172.                                  and a.bureau_code <> v_busi AND A.STAT_BUREAU = B.STAT_BUREAU);    
  173.                                    
  174. else    
  175.     update ZX_WORK_SHEET_VERIFY_NOTICES  B   
  176.        set (B.SHEET_RATE) = (select round(decode((select count(*) from ZX_WORK_SHEET_VERIFY z    
  177.                               where z.app_date between begin_day and v_end_day   
  178.                                 and z.bureau_code = a.bureau_code    
  179.                                 and (z.confirm_date <> date'1900-1-1' or z.confirm_date is not null)),0,0,   
  180.                             (select sum(nvl(c.SHEET_NUM,0)) from ZX_WORK_SHEET_VERIFY_NOTICES c   
  181.                               where c.bureau_code  = a.bureau_code    
  182.                                 and a.notice_level = c.notice_level   
  183.                                 and b.start_day    = c.start_day    
  184.                                 and c.end_day      = b.end_day    
  185.                                 )   
  186.                                 /   
  187.                              (select count(*) from ZX_WORK_SHEET_VERIFY z   
  188.                                where z.app_date between begin_day and v_end_day   
  189.                                  and z.bureau_code  = a.bureau_code    
  190.                                  and (z.confirm_date <> date'1900-1-1' or z.confirm_date is not null))),4)*100   
  191.                                 from ZX_WORK_SHEET_VERIFY_NOTICES a    
  192.                                WHERE A.BUREAU_CODE  = B.BUREAU_CODE    
  193.                                  AND A.NOTICE_LEVEL = B.NOTICE_LEVEL   
  194.                                  and b.start_day    = a.start_day    
  195.                                  and a.end_day      = b.end_day    
  196.                                  and a.bureau_code = v_busi AND A.STAT_BUREAU = B.STAT_BUREAU);    
  197. end if;   
  198.   -----排名   
  199.     v_result := -50;   
  200. if v_busi = 'TT' then    
  201.      update ZX_WORK_SHEET_VERIFY_NOTICES  B   
  202.         set (B.RATE_NUM) = (select r.row_num   
  203.                            FROM (   
  204.                            SELECT row_number () over (PARTITION by z.NOTICE_LEVEL order by z.SHEET_RATE asc) row_num,   
  205.                                   Z.BUREAU_CODE , Z.NOTICE_LEVEL   
  206.                            FROM ZX_WORK_SHEET_VERIFY_NOTICES Z where  
  207.                              begin_day = z.start_day and v_end_day = z.end_day and z.notice_level <> 3 AND Z.BUREAU_CODE <> v_busi   
  208.                              AND Z.STAT_BUREAU = v_busi    
  209.                            ) r   
  210.                             WHERE r.BUREAU_CODE = B.BUREAU_CODE AND r.NOTICE_LEVEL = B.NOTICE_LEVEL  );    
  211.  else update ZX_WORK_SHEET_VERIFY_NOTICES  B   
  212.         set (B.RATE_NUM) = '';   
  213. end if;   
  214.      commit;   
  215.      return 0;   
  216.    exception   
  217.      when others then  
  218.      begin  
  219.        rollback ;   
  220.      P_PUB_ERROR_LOG(v_result,sqlcode,sqlerrm,'F_ZX_WORK_SHEET_VERIFY_NOTICES');   
  221.      return v_result;   
  222.      end;   
  223. END;   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章