oracle存儲過程及sql優化-(二)

接下來比較重要,我會先貼出一個存儲過程,根據這個存儲過程講解sql

PROCEDURE AP_CXBB_GT3_SBFGL_SBFYJSQC 
(OUT_RECORD OUT SYS_REFCURSOR,
  
 PI_XH PLS_INTEGER,
 PV_ZGSWJG  VARCHAR2 --管理單位
 --PI_HSND    PLS_INTEGER, --彙算年度
 --QR_HYDM    PLS_INTEGER --行業
 ) AS
   VI_HYXH PLS_INTEGER ;
 

BEGIN
  VI_HYXH:=NVL(PI_XH ,0); 
  DELETE FROM TMP_GT3_SBFGL_WJSTJB;
  
    if VI_HYXH >= 6 AND VI_HYXH <= 36 then
   insert into TMP_GT3_sbfgl_WJSTJB
    SELECT  NSR.NSRSBH,
                    NSR.NSRMC,
                    NSR.SCJYDZ,
                    case
                      when NSRKZ.FDDBRYDDH is not null then
                       '法人' || NSRKZ.FDDBRYDDH
                      else
                       ''
                    end || case
                      when NSRKZ.SWDLRLXDH is not null then
                       ',稅務代理人' || NSRKZ.SWDLRLXDH
                      else
                       ''
                    end || case
                      when NSRKZ.BSRYDDH is not null then
                       ',辦稅人' || NSRKZ.BSRYDDH
                      else
                       ''
                    end AS FDDBRGDDH,
                    F.SWJGMC,
                    G.SWJGMC
      FROM AP_CXBB_GT3_SBFAQYYIJSTJ_cs B ,
           H_DJ_NSRXX           NSR,
           H_DJ_NSRXX_KZ        NSRKZ,
           DM_GY_SWJG_JH F,
           DM_GY_SWJG_JH G           
     WHERE NSR.ZGSWJ_DM = F.SWJG_DM(+)
       AND B.SWJGDM = G.SWJG_DM(+)
       AND B.DJXH = NSR.DJXH(+)
       AND B.DJXH = NSRKZ.DJXH(+)
      AND ( B.SWJGDM IN
               (SELECT SWJG_DM
                  FROM DM_GY_SWJG_JH V
                 START WITH V.SWJG_DM IN (PV_ZGSWJG)
                CONNECT BY PRIOR V.SWJG_DM = V.SJSWJG_DM))
       
        AND (VI_HYXH = 0 OR
               NSR.HY_DM IN
               (SELECT HY_DM
                   FROM DM_HY_JH
                  WHERE ZL IN
                        (SELECT HYDM FROM TEMP_HYDM_JH_HS WHERE XH = VI_HYXH)))
        
       ;
ELSE 
   
  insert into TMP_GT3_sbfgl_WJSTJB
    SELECT  NSR.NSRSBH,
                    NSR.NSRMC,
                    NSR.SCJYDZ,
                    case
                      when NSRKZ.FDDBRYDDH is not null then
                       '法人' || NSRKZ.FDDBRYDDH
                      else
                       ''
                    end || case
                      when NSRKZ.SWDLRLXDH is not null then
                       ',稅務代理人' || NSRKZ.SWDLRLXDH
                      else
                       ''
                    end || case
                      when NSRKZ.BSRYDDH is not null then
                       ',辦稅人' || NSRKZ.BSRYDDH
                      else
                       ''
                    end AS FDDBRGDDH,
                    F.SWJGMC,
                    G.SWJGMC
      FROM AP_CXBB_GT3_SBFAQYYIJSTJ_cs B ,
           H_DJ_NSRXX           NSR,
           H_DJ_NSRXX_KZ        NSRKZ,
           DM_GY_SWJG_JH F,
           DM_GY_SWJG_JH G           
     WHERE NSR.ZGSWJ_DM = F.SWJG_DM(+)
       AND B.SWJGDM = G.SWJG_DM(+)
       AND B.DJXH = NSR.DJXH(+)
       AND B.DJXH = NSRKZ.DJXH(+)
       AND ( B.SWJGDM IN
               (SELECT SWJG_DM
                  FROM DM_GY_SWJG_JH V
                 START WITH V.SWJG_DM IN (PV_ZGSWJG)
                CONNECT BY PRIOR V.SWJG_DM = V.SJSWJG_DM))
        AND (VI_HYXH = 0 OR
               NSR.HY_DM IN
               (SELECT HY_DM
                   FROM DM_HY_JH
                  WHERE DL IN
                        (SELECT HYDM FROM TEMP_HYDM_JH_HS WHERE XH = VI_HYXH)))
          
       ;
       END IF ;
  

  OPEN OUT_RECORD FOR
    select * from TMP_GT3_sbfgl_WJSTJB A;
  ------------------------------------------------------------------------------------------

END AP_CXBB_GT3_SBFGL_SBFYJSQC;

代碼前部分函數

PROCEDURE AP_CXBB_GT3_SBFGL_SBFYJSQC 
(OUT_RECORD OUT SYS_REFCURSOR,
  
 PI_XH PLS_INTEGER,
 PV_ZGSWJG  VARCHAR2 --管理單位
 --PI_HSND    PLS_INTEGER, --彙算年度
 --QR_HYDM    PLS_INTEGER --行業
 ) AS
   VI_HYXH PLS_INTEGER ;
 

首先咱們定義了 一個存儲過程 AP_CXBB_GT3_SBFGL_SBFYJSQCspa

附註:建立 存儲過程 PROCEDURE的語句爲

create or replace PROCEDURE

如上:咱們定義了SYS_REFCURSOR遊標類型的輸出變量OUT_RECORD , PLS_INTEGER整數類型輸入參數PI_XH, 和VARCHAR2字符串類型的輸入參數PV_ZGSWJG 代理

還有一個PLS_INTEGER類型變量 VI_HYXH  code

VI_HYXH:=NVL(PI_XH ,0); 

 

接着咱們對變量賦值,注意 orale賦值符號爲 :=   而不是 = ,其中nvl爲判斷參數是否空並作處理的內置函數 blog

從begin 到end的代碼塊實現的業務流程我簡單介紹下字符串

1.刪除臨時表數據class

2.將須要的數據存到臨時表變量

3.將臨時表數據放到遊標輸出select

在寫業務複雜的存儲過程時,咱們時常會用到臨時表,遊標。

 

 

 

下一章介紹該存儲過程的 sql語句

相關文章
相關標籤/搜索