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

接下來介紹上篇接觸到的存儲過程當中的sql語句sql

 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)))
        
       ;

 

如上這是一句 insert  插入數據的語句(oracle支持一個集合,因此後面能夠跟select 或 with 語句 ,with 的使用比較重要我會在 之後單獨寫一篇)oracle

   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

上面是select語句的一部分,能夠看出咱們提取的須要的數據從哪幾張表(NSR.SCJYDZ  中 nsr 是 表的別名,SCJYDZ爲字段名)spa

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(+)

上面是全部用到的表及相關錶鏈接代理

H_DJ_NSRXX 是表名 ,NSR是其別名
從 NSR.ZGSWJ_DM = F.SWJG_DM(+) 能夠看出 nsr 左鏈接表 F(根據 SWJG_DM 字段) 
這裏強調下 oracle中使用鏈接方式有兩種 ,一種是通用的 left out join ,inner join ,full out join 等
另外一種就是這裏用到的oracle特有的 (+) 方式。
對鏈接不熟悉的能夠百度oracle左鏈接,右鏈接,內鏈接

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)))

接着就是where 條件語句 where 中一部分已經提過,純純粹爲了左右鏈接寫的條件,其餘一些就是對數據的篩選code

 (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)

輸出爲一個集合而不是 一個值 因此不用 = 而是用 inblog

其中it

 START WITH V.SWJG_DM IN (PV_ZGSWJG)
                CONNECT BY PRIOR V.SWJG_DM = V.SJSWJG_DM)

  爲層次化查詢  語句class

START WITH
CONNECT BY PRIOR
爲了方便樹形 結構的數據 取 節點 數據

(SELECT HY_DM
                   FROM DM_HY_JH
                  WHERE ZL IN
                        (SELECT HYDM FROM TEMP_HYDM_JH_HS WHERE XH = VI_HYXH))

上面爲嵌套子查詢百度

相關文章
相關標籤/搜索