数据库过程留档:基础号码遍历查询月表,题取使用设备号,延伸设备号对应号码情况

create or replace procedure zhyw.SHA_GZGC_YSYK_CS_SZHM_IMEI(P_DAY VARCHAR2 DEFAULT TO_CHAR(SYSDATE - 1,'yyyymmdd')) as

SQL_STRING varchar2(30000);
v_day varchar2(8);
v_day1 varchar2(8);
v_day2 varchar2(8);
v_day5 varchar2(8);
v_day_qy1 varchar2(8);
v_month varchar2(6);
v_month_1 varchar2(6);
v_month_5 varchar2(6);
v_month1 varchar2(6);
v_monthq varchar2(6);
v_monthq_1 varchar2(6);
v_last_day varchar2(8);
v_last_day_2 varchar2(8);
v_last_day2 varchar2(8);
v_last_day3 varchar2(8);
v_monthS varchar2(6);
v_monthS3 varchar2(6);

v_monsr varchar2(6);
v_monsrq varchar2(6);
v_monsrh varchar2(6);

v_monsrq2 varchar2(6);
v_monsrq3 varchar2(6);
v_monsrq4 varchar2(6);
v_monsrq5 varchar2(6);
v_monsrq6 varchar2(6);
v_monsrq12 varchar2(6);
v_P_DAY varchar2(8);
V_TAB1 varchar2(900);
V_ZMLX   VARCHAR2(30);
V_ZMLX_SZ   VARCHAR2(30);
   zdyf   varchar2(10);

begin


    v_day        :=to_char(sysdate-1,'yyyymmdd');
    v_day1       :=to_char(sysdate-2,'yyyymmdd');
    v_day2       :=to_char(sysdate-3,'yyyymmdd');
    v_day_qy1       :=to_char(add_months(sysdate-2,-1),'yyyymmdd');
    v_day5       :=to_char(sysdate-5,'yyyymmdd');
    v_month      :=to_char(add_months(sysdate-1,-0),'yyyymm');
    v_month_1    :=to_char(add_months(sysdate-2,-0),'yyyymm');
    v_month_5    :=to_char(add_months(sysdate-5,-1),'yyyymm');
    v_month1     :=to_char(add_months(sysdate-1,1),'yyyymm');
    v_monthq     :=to_char(add_months(sysdate-1,-1),'yyyymm');
    v_monthq_1     :=to_char(add_months(sysdate-2,-1),'yyyymm');
    v_monthS     :=to_char(add_months(sysdate-1,-2),'yyyymm');
    v_monthS3    :=to_char(add_months(sysdate-1,-3),'yyyymm');
    v_last_day   :=to_char(last_day(add_months(sysdate-1,-1)),'yyyymmdd') ;
    v_last_day_2   :=to_char(last_day(add_months(sysdate-2,-1)),'yyyymmdd') ;
    v_last_day2  :=to_char(last_day(add_months(sysdate-1,-2)),'yyyymmdd') ;
    v_last_day3  :=to_char(last_day(add_months(sysdate-1,-3)),'yyyymmdd') ;

    v_monsr      :=substr(P_DAY,1,6);
    v_monsrq      :=to_char(add_months(to_date(substr(P_DAY,1,6),'yyyymm'),-1),'yyyymm') ;
    v_monsrh      :=to_char(add_months(to_date(substr(P_DAY,1,6),'yyyymm'),1),'yyyymm') ;

    v_monsrq2      :=to_char(add_months(to_date(substr(P_DAY,1,6),'yyyymm'),-2),'yyyymm') ;
    v_monsrq3      :=to_char(add_months(to_date(substr(P_DAY,1,6),'yyyymm'),-3),'yyyymm') ;
    v_monsrq4      :=to_char(add_months(to_date(substr(P_DAY,1,6),'yyyymm'),-4),'yyyymm') ;
    v_monsrq5      :=to_char(add_months(to_date(substr(P_DAY,1,6),'yyyymm'),-5),'yyyymm') ;
    v_monsrq6      :=to_char(add_months(to_date(substr(P_DAY,1,6),'yyyymm'),-6),'yyyymm') ;
    v_monsrq12      :=to_char(add_months(to_date(substr(P_DAY,1,6),'yyyymm'),-12),'yyyymm') ;
    v_P_DAY          :=to_char(to_date(P_DAY,'yyyymmdd')-1,'yyyymmdd') ;

    zdyf:=to_char(trunc(to_date(P_DAY,'yyyymmdd'),'mm'),'yyyymm');--统计月

      SQL_STRING := 'insert into shzc.SHA_GZGC_YSYK_CS_SZHM_lccd
      select '''||P_DAY||''',''zhyw.SHA_GZGC_YSYK_CS_SZHM_IMEI'',''开始'',sysdate  from dual  ';
      EXECUTE IMMEDIATE (SQL_STRING);
      commit;

      /*---将孟老师导入数据插入号码库
      SQL_STRING := 'insert into shzc.gagx_sasz_hmmx_cbcd
      select a.邮件转派时间,a.涉案号码,a.类型 from shzc.jbsj_jfbbdr_gagx_szhm_sjh_bd a 
      where not exists (select 1 from shzc.gagx_sasz_hmmx_cbcd b 
      where b.邮件转派时间=a.邮件转派时间
      and b.servnumber=a.涉案号码
      and b.工单类型=a.类型)  ';
      EXECUTE IMMEDIATE (SQL_STRING);*/
      
      SQL_STRING := 'update shzc.gagx_sasz_hmmx_cbcd b set b.servnumber=replace(b.servnumber,''+86'','''')
      where b.servnumber like ''%+86%''  ';
      EXECUTE IMMEDIATE (SQL_STRING);
      commit;
      
      fan_drop_retable(upper('gagx_sasz_hmmx_cbcda'),'SHZC');
      SQL_STRING:='create table shzc.gagx_sasz_hmmx_cbcda as
      select a.subsid,a.servnumber,a.prodid,a.createdate,a.registerorgid,a.ownerorgid,
      a.status,a.statusdate,b.邮件转派时间,b.工单类型,
      row_number() over (partition by b.邮件转派时间,b.servnumber,b.工单类型 order by a.createdate desc ) 排名 
      from zhyw.subscriber a,
      (select distinct b.邮件转派时间,b.servnumber,b.工单类型 from  shzc.gagx_sasz_hmmx_cbcd b ) b
      where a.servnumber=b.servnumber
      and a.createdate<=b.邮件转派时间  ';
      EXECUTE IMMEDIATE (SQL_STRING);
      
      fan_drop_retable(upper('gagx_sasz_hmmx_cbcdb'),'SHZC');
      SQL_STRING:='create table shzc.gagx_sasz_hmmx_cbcdb as
      select * from shzc.gagx_sasz_hmmx_cbcda a where a.排名=1  ';
      EXECUTE IMMEDIATE (SQL_STRING);

      ---用户imei后台表 --提供表名
      fan_drop_retable(upper('gagx_sasz_hmmx_cbcdc'),'SHZC');
      SQL_STRING:='create table shzc.gagx_sasz_hmmx_cbcdc as
      select * from 
      (select a.OBJECT_NAME ,row_number() over (partition by substr(a.OBJECT_NAME,14,6) order by a.OBJECT_NAME desc ) 排名
       From all_objects a 
      where objEct_name like UPPER(''dw_call_imei_2%'')
      and a.OWNER=''ZIBO'' and length(a.OBJECT_NAME)=21
      and (substr(a.OBJECT_NAME,14,8)>=to_char(sysdate-2,''yyyymmdd'')
      or substr(a.OBJECT_NAME,20,2) in (''28'',''29'',''30'',''31''))
      order by a.OBJECT_NAME) a
      where 排名=1 ';
      EXECUTE IMMEDIATE (SQL_STRING);


      ---清空本月数据
      SQL_STRING :=' delete shzc.gagx_sasz_hmmx_cbcd_cd  a where to_char(a.op_time,''yyyymm'')='''||v_monsr||'''  ';
       EXECUTE IMMEDIATE (SQL_STRING);   
       commit;   
       
      -----循环过程--遍历所有表名然后插入基础表,排除已导入过的
    DECLARE   CURSOR ZMLX IS select  OBJECT_NAME from shzc.gagx_sasz_hmmx_cbcdc  ; 
    BEGIN  OPEN ZMLX;
      LOOP
        FETCH ZMLX INTO V_ZMLX;
        EXIT WHEN ZMLX%NOTFOUND;
        SQL_STRING :=' insert into shzc.gagx_sasz_hmmx_cbcd_cd 
        select distinct a.op_time,a.product_no,a.city_id,a.call_duration,a.call_counts,a.imei_tac,a.imei,a.first_time,a.last_time
        from zibo.'||V_ZMLX ||' a,
        shzc.gagx_sasz_hmmx_cbcdb b
        where a.product_no=b.servnumber
        and not exists ( select 1 from shzc.gagx_sasz_hmmx_cbcd_cd t 
        where t.op_time=a.op_time and t.product_no=a.product_no and t.imei=a.imei)  ';
       EXECUTE IMMEDIATE (SQL_STRING);   
       commit;   
        
       SQL_STRING := 'insert into shzc.SHA_GZGC_YSYK_CS_SZHM_lccd
      select '''||P_DAY||''',''shzc.gagx_sasz_hmmx_cbcd_cd'','''||V_ZMLX ||''',sysdate  from dual  ';
      EXECUTE IMMEDIATE (SQL_STRING);
      commit;   
                                
       END LOOP;
     end;
     
      ------另一组经分下发 用户imei使用表
      fan_drop_retable(upper('gagx_sasz_hmmx_cbcdd'),'SHZC');
      SQL_STRING:='create table shzc.gagx_sasz_hmmx_cbcdd as
      select a.OBJECT_NAME from  all_objects a 
      where objEct_name like UPPER(''dw_imei_user_2%'')
      and a.OWNER=''ZIBO'' and length(a.OBJECT_NAME)=19 ';
       EXECUTE IMMEDIATE (SQL_STRING);   
    
    -----循环过程--遍历所有表名然后插入基础表,排除已导入过的
    DECLARE   CURSOR ZMLX IS select  OBJECT_NAME from shzc.gagx_sasz_hmmx_cbcdd ; 
    BEGIN  OPEN ZMLX;
      LOOP
        FETCH ZMLX INTO V_ZMLX;
        EXIT WHEN ZMLX%NOTFOUND;
        SQL_STRING :='insert into shzc.gagx_sasz_hmmx_cbcd_bc 
         select distinct a.op_time,a.user_id,a.product_no,a.city_id,substr(a.imei,1,8) imei_tac,a.imei,a.imei_get_date
        from  zibo.'||V_ZMLX ||' a,
        shzc.gagx_sasz_hmmx_cbcdb b
        where a.user_id=to_char(b.subsid)
        and not exists ( select 1 from shzc.gagx_sasz_hmmx_cbcd_bc t 
        where t.op_time=a.op_time and t.user_id=a.user_id and t.imei=a.imei)   ';
       EXECUTE IMMEDIATE (SQL_STRING);   
       commit;        
       
       SQL_STRING := 'insert into shzc.SHA_GZGC_YSYK_CS_SZHM_lccd
      select '''||P_DAY||''',''shzc.gagx_sasz_hmmx_cbcd_bc'','''||V_ZMLX ||''',sysdate  from dual  ';
      EXECUTE IMMEDIATE (SQL_STRING);
      commit;   
                                
       END LOOP;
     end;
    
      ----用户使用imei集合
    
      fan_drop_retable(upper('gagx_sasz_hmmx_cbcd_bcd'),'SHZC');
      SQL_STRING:='create table shzc.gagx_sasz_hmmx_cbcd_bcd as
      select distinct a.op_time,to_char(b.subsid) subsid,a.product_no,a.imei_tac,substr(a.imei,1,14) imei,
      to_date(substr(a.first_time,1,10),''yyyy-mm-dd'') first_time
      from shzc.gagx_sasz_hmmx_cbcd_cd a,
      (select distinct b.servnumber,b.subsid,b.createdate,b.status,
       nvl(case when b.status in (''US10'',''US30'') then to_date(''2099'',''yyyy'') end,b.statusdate) statusdate
        from  shzc.gagx_sasz_hmmx_cbcdb b where b.createdate is not null ) b
       where a.product_no=b.servnumber
       and a.op_time>=b.createdate
       and a.op_time<b.statusdate
       union all
      select a.op_time,a.user_id,a.product_no,a.imei_tac,substr(a.imei,1,14),a.imei_get_date
      from shzc.gagx_sasz_hmmx_cbcd_bc a where a.imei <>''0''  ';
      EXECUTE IMMEDIATE (SQL_STRING);   
     
     fan_drop_retable(upper('gagx_sasz_hmmx_cbcde'),'SHZC');
      SQL_STRING:='create table shzc.gagx_sasz_hmmx_cbcde as
      select a.subsid,a.product_no,a.imei_tac,a.imei,
      min(a.first_time) first_time, 
      min(a.op_time)  op_time_min,
      max(a.op_time) op_time_max,
      row_number() over (partition by a.subsid order by max(a.op_time) desc ) 排名 
      from shzc.gagx_sasz_hmmx_cbcd_bcd a 
      group by a.subsid,a.product_no,a.imei_tac,a.imei  ';
      EXECUTE IMMEDIATE (SQL_STRING);   
     
     ----建立月排名存档
      SQL_STRING:=' delete shzc.gagx_sasz_hmmx_cbcd_ypm where cycle='''||v_monsr||'''  ';
      EXECUTE IMMEDIATE (SQL_STRING);   
     
      SQL_STRING:=' insert into  shzc.gagx_sasz_hmmx_cbcd_ypm 
      select '''||v_monsr||''' cycle, a.* from shzc.gagx_sasz_hmmx_cbcde a  ';
      EXECUTE IMMEDIATE (SQL_STRING);   
     commit;    
     
      ---清空本月数据
      SQL_STRING :=' delete shzc.gagx_sasz_hmmx_cbcd_ycd  a where to_char(a.op_time,''yyyymm'')='''||v_monsr||'''  ';
       EXECUTE IMMEDIATE (SQL_STRING);   
       commit;   
     
     -----循环过程--遍历所有表名然后插入基础表,排除已导入过的
    DECLARE   CURSOR ZMLX IS select  OBJECT_NAME from shzc.gagx_sasz_hmmx_cbcdc  ; 
    BEGIN  OPEN ZMLX;
      LOOP
        FETCH ZMLX INTO V_ZMLX;
        EXIT WHEN ZMLX%NOTFOUND;
        SQL_STRING :='insert into shzc.gagx_sasz_hmmx_cbcd_ycd 
    select distinct a.op_time,a.product_no,a.city_id,a.call_duration,a.call_counts,a.imei_tac,a.imei,a.first_time,a.last_time
    from zibo.'||V_ZMLX ||' a,
    (select distinct b.imei from shzc.gagx_sasz_hmmx_cbcde b ) b
    where substr(a.imei,1,14)=b.imei
    and not exists ( select * from shzc.gagx_sasz_hmmx_cbcd_ycd t 
    where t.op_time=a.op_time and t.product_no=a.product_no and t.imei=a.imei)  ';
       EXECUTE IMMEDIATE (SQL_STRING);   
       commit;     
       
        SQL_STRING := 'insert into shzc.SHA_GZGC_YSYK_CS_SZHM_lccd
      select '''||P_DAY||''',''shzc.gagx_sasz_hmmx_cbcd_ycd'','''||V_ZMLX ||''',sysdate  from dual  ';
      EXECUTE IMMEDIATE (SQL_STRING);
      commit;   
                                   
       END LOOP;
     end;
     
     -----循环过程--遍历所有表名然后插入基础表,排除已导入过的
    DECLARE   CURSOR ZMLX IS select  OBJECT_NAME from shzc.gagx_sasz_hmmx_cbcdd ; 
    BEGIN  OPEN ZMLX;
      LOOP
        FETCH ZMLX INTO V_ZMLX;
        EXIT WHEN ZMLX%NOTFOUND;
        SQL_STRING :='insert into shzc.gagx_sasz_hmmx_cbcd_ybc 
         select distinct a.op_time,a.user_id,a.product_no,a.city_id,substr(a.imei,1,8) imei_tac,a.imei,a.imei_get_date
        from  zibo.'||V_ZMLX ||' a,
        (select distinct b.imei from shzc.gagx_sasz_hmmx_cbcde b )  b
        where substr(a.imei,1,14)=b.imei
        and not exists ( select 1 from shzc.gagx_sasz_hmmx_cbcd_ybc t 
        where t.op_time=a.op_time and t.user_id=a.user_id and t.imei=a.imei)  ';
       EXECUTE IMMEDIATE (SQL_STRING);   
       commit;    
       
       SQL_STRING := 'insert into shzc.SHA_GZGC_YSYK_CS_SZHM_lccd
      select '''||P_DAY||''',''shzc.gagx_sasz_hmmx_cbcd_ybc'','''||V_ZMLX ||''',sysdate  from dual  ';
      EXECUTE IMMEDIATE (SQL_STRING);
      commit;   
                                    
       END LOOP;
     end;
     
        fan_drop_retable(upper('gagx_sasz_hmmx_cbcdf'),'SHZC');
        SQL_STRING:='create table  shzc.gagx_sasz_hmmx_cbcdf as
        select distinct a.product_no from shzc.gagx_sasz_hmmx_cbcd_ycd a ';
        EXECUTE IMMEDIATE (SQL_STRING);
      
        fan_drop_retable(upper('gagx_sasz_hmmx_cbcdg'),'SHZC');
        SQL_STRING:='create table  shzc.gagx_sasz_hmmx_cbcdg as
        select b.subsid,b.servnumber,b.prodid,b.createdate,b.registerorgid,
        b.ownerorgid,b.status,b.statusdate
        from zhyw.subscriber b,
         shzc.gagx_sasz_hmmx_cbcdf a
        where b.servnumber=a.product_no
        and b.status not in (''US26'',''US28'')  ';
        EXECUTE IMMEDIATE (SQL_STRING);
     
        fan_drop_retable(upper('gagx_sasz_hmmx_cbcd_ybcd'),'SHZC');
        SQL_STRING:='create table  shzc.gagx_sasz_hmmx_cbcd_ybcd as
        select distinct a.op_time,to_char(b.subsid) subsid,a.product_no,a.imei_tac,substr(a.imei,1,14) imei,
        to_date(substr(a.first_time,1,10),''yyyy-mm-dd'') first_time
        from shzc.gagx_sasz_hmmx_cbcd_ycd a,
        (select distinct b.servnumber,b.subsid,b.createdate,b.status,
        nvl(case when b.status in (''US10'',''US30'') then to_date(''2099'',''yyyy'') end,b.statusdate) statusdate
        from  shzc.gagx_sasz_hmmx_cbcdg b where b.createdate is not null ) b
        where a.product_no=b.servnumber
        and a.op_time>=b.createdate
        and a.op_time<b.statusdate 
        union all
        select a.op_time,to_char(b.subsid) user_id,a.product_no,a.imei_tac,substr(a.imei,1,14),a.imei_get_date
        from shzc.gagx_sasz_hmmx_cbcd_ybc a ,
        (select distinct b.servnumber,b.subsid,b.createdate,b.status,
        nvl(case when b.status in (''US10'',''US30'') then to_date(''2099'',''yyyy'') end,b.statusdate) statusdate
         from  shzc.gagx_sasz_hmmx_cbcdg b where b.createdate is not null ) b
         where a.product_no=b.servnumber
         and a.op_time>=b.createdate
         and a.op_time<b.statusdate
         and a.imei <>''0'' 
         and a.user_id not like ''533%'' 
         union all
         select a.op_time,a.user_id,a.product_no,a.imei_tac,substr(a.imei,1,14),a.imei_get_date
        from shzc.gagx_sasz_hmmx_cbcd_ybc a where a.imei <>''0'' and a.user_id  like ''533%'' ';
        EXECUTE IMMEDIATE (SQL_STRING);
        
     
     SQL_STRING := 'insert into shzc.SHA_GZGC_YSYK_CS_SZHM_lccd
      select '''||P_DAY||''',''zhyw.SHA_GZGC_YSYK_CS_SZHM_IMEI'',''结束'',sysdate  from dual  ';
      EXECUTE IMMEDIATE (SQL_STRING);
      commit;
     

  exception

  --增加异常处理 提高代码健壮性
  when others then
    shc_add_exception('xxxx', 'xxxx'); --异常处理



end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值