酉卒之子

导航

oracle with和insert结合使用

需求是这样的,先在一个从句中根据sub_code查询dis_code和reg_code,

再把这;两个值作为insert value的一部分,差到rate表里,好了,这里提供一种常规做法,和一种用with实现的做法

常规做法:

insert into HW_RATE
    (ID,
     SUB_TYPE,
     DISTRICT_CODE,
     REGION_CODE,
     SUB_AREA_CODE,
     CREATE_DATE,
     CREATE_USER)
    select sys_guid(),
           2,
           o.district_code bu,
           o.region_code re,
           '60487',
           sysdate,
           'ww00068'
      from eis_user.org_office o
     where o.sub_area_code = '60487'
       and o.status = 1
       and o.year = extract(year from sysdate)
       and o.sub_type = 2

用with的用法:

insert into HW_RATE
    (ID,
     SUB_TYPE,
     DISTRICT_CODE,
     REGION_CODE,
     SUB_AREA_CODE,
     CREATE_DATE,
     CREATE_USER) 
     with areaInfo as
    (select o.district_code bu, o.region_code re
       from eis_user.org_office o
      where o.sub_area_code = '60487'
        and o.status = 1
        and o.year = extract(year from sysdate)
        and o.sub_type = 2)
    select sys_guid(),
           2,
           areaInfo.bu,//这里直接with 临时表别名.子句查询结果别名,即可
           areaInfo.re,
           '60487',
           sysdate,
           'ww00068'
      from areaInfo

唯一需要注意的就是,with语句后面直接只能跟select语句,稍微换下思路就可以吧insert和with结合使用了

posted on 2017-12-18 19:52  酉卒之子  阅读(12457)  评论(0编辑  收藏  举报