接下来介绍上篇接触到的存储过程中的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 的使用比较重要我会在 以后单独写一篇)

   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为字段名)

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 中一部分已经提过,纯纯粹为了左右连接写的条件,其他一些就是对数据的筛选

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

输出为一个集合而不是 一个值 所以不用 = 而是用 in

其中

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

  为层次化查询  语句

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

上面为嵌套子查询

 

 



 

 

posted on 2017-07-21 16:21  HelloXF_jeff  阅读(1142)  评论(0编辑  收藏  举报