plsql例子

Posted 上台阶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了plsql例子相关的知识,希望对你有一定的参考价值。

create or replace procedure find_difference(db_link in varchar2) is
/*
比对两套环境建表脚本差异,以224环境为主
当前环境,dblink环境
中间表middle记录当前环境比224环境少的表字段,及差异字段
create table middle as select table_name,column_name,data_type,data_length,nullable ,‘ ‘ as note
from user_tab_columns where 1 = 2 ;
create table logs(id number , table_name varchar2(80)) ;
*/

j number default 0;
v_count number default 0;
v_count_local number default 0;
v_sql varchar2(4000);
v_table1 number default 0;
v_table2 number default 0;
begin
-- 创建中间表/清楚上次数据
select count(1) into v_table1 from user_tables where table_name = ‘MIDDLE‘;
if v_table1 = 0 then
execute immediate ‘ create table middle as select table_name,column_name,data_type,
data_length,nullable ,‘‘ ‘‘ as note from user_tab_columns where 1 = 2 ‘;
else
delete from middle;
commit;
end if;

select count(1) into v_table2 from user_tables where table_name = ‘LOGS‘;
if v_table2 = 0 then
execute immediate ‘create table logs(id number , table_name varchar2(80))‘;
else
delete from logs;
commit;
end if;


declare
TYPE middle_type IS TABLE OF middle%rowtype INDEX BY BINARY_INTEGER;
v_middles middle_type;
v_211 middle_type;
v_middles_local middle_type;
begin
--比对dblink环境多的,以及差异字段
execute immediate ‘ select table_name,
column_name,
data_type,
data_length,
nullable,
‘‘_‘‘
from [email protected]‘||db_link||‘
order by table_name, column_name‘ bulk collect into v_middles;

for i in v_middles.first .. v_middles.last loop

j := j + 1;
insert into logs values (j, v_middles(i).table_name);
commit;

select count(1)
into v_count
from user_tab_columns
where table_name = v_middles(i).table_name
and column_name = v_middles(i).column_name;

if v_count = 0 then
insert into middle
values
(v_middles(i).table_name,
v_middles(i).column_name,
v_middles(i).data_type,
v_middles(i).data_length,
v_middles(i).nullable,
‘当前环境无‘);
commit;
continue;
end if;
--比对差异字段
select table_name,
column_name,
data_type,
data_length,
nullable,
‘‘ bulk collect
into v_211
from user_tab_columns
where table_name = v_middles(i).table_name
and column_name = v_middles(i).column_name;

if v_211(v_211.first).data_type <> v_middles(i).data_type or v_211(v_211.first)
.data_length <> v_middles(i).data_length or v_211(v_211.first)
.nullable <> v_middles(i).nullable then
insert into middle
values
(v_middles (i).table_name,
v_middles (i).column_name,
v_middles (i).data_type,
v_middles (i).data_length,
v_middles (i).nullable,
‘difference‘);
commit;
continue;
end if;
end loop;

--比对当前环境多出字段
select table_name,
column_name,
data_type,
data_length,
nullable,
‘‘ bulk collect into v_middles_local
from user_tab_columns
order by table_name, column_name;

for i in v_middles_local.first .. v_middles_local.last loop

j := j + 1;
insert into logs values (j, v_middles_local(i).table_name);
commit;
execute immediate ‘select count(1) from [email protected]‘||db_link||‘ where table_name = :1 and column_name = :2 ‘
into v_count_local
using in v_middles_local(i).table_name , in v_middles_local(i).column_name ;

if v_count_local = 0 then
insert into middle
values
(v_middles_local(i).table_name,
v_middles_local(i).column_name,
v_middles_local(i).data_type,
v_middles_local(i).data_length,
v_middles_local(i).nullable,
db_link||‘环境无‘);
commit;
continue;
end if;
end loop;

end;

end find_difference;

 

 

 

 

 

 

 

 

CREATE OR REPLACE PROCEDURE BJ_G.zj_msisdn_to_file_whq IS
handle utl_file.file_type;
TYPE ref_cursor_type IS REF CURSOR;
ref_cursor ref_cursor_type;
v_msisdn VARCHAR2(15);
FILE_NAME VARCHAR2(40);
v_sql varchar2(2000);
tk_id varchar2(20);
cc varchar2(20);
cursor user_name is
select * from temp_whq1;
type tablename is ref cursor;
tablenamel tablename;
BEGIN
for i in user_name loop
select i.taskid into tk_id from dual;
select i.prov_sh into cc from dual;
FILE_NAME := cc||‘.txt‘;
handle := UTL_FILE.FOPEN(‘AA‘, FILE_NAME, ‘w‘);
v_sql:=‘select c_msisdn from (select c_msisdn,count(*) c1,c_smscount c2 from ‘||cc||‘.t_batch_mt
where C_taskid = ‘||tk_id||‘ and C_actiontype = 3 and C_status = ‘‘0‘‘
group by c_msisdn,c_smscount) where c1>=c2 intersect
select c_msisdn from ‘||cc||‘.t_batch_mt where C_taskid = ‘||tk_id||‘ and C_actiontype = 9 and C_status = ‘‘0‘‘‘;
open ref_cursor for v_sql;
LOOP
FETCH ref_cursor
INTO v_msisdn ;
EXIT WHEN ref_cursor%NOTFOUND;
UTL_FILE.put_line(handle,
v_msisdn );
END LOOP;
UTL_FILE.fclose(handle);
close ref_cursor;
end loop;
end;
/

 

 

 


declare
cursor user_name is select prov_name,prov_sh from t_province;
aa varchar2(15);
bb varchar2(15);
begin
for i in user_name loop
select i.prov_name into aa from dual;
select i.prov_sh into bb from dual;
execute immediate ‘insert into mytemp33 select ‘||‘‘‘‘||aa||‘‘‘‘||
‘,count(case when a.c_regtime>=to_date(‘‘20150101‘‘,‘‘yyyymmdd‘‘) and a.c_regtime<to_date(‘‘20150201‘‘,‘‘yyyymmdd‘‘) then a.c_msisdn else null end) 一月,
count(case when a.c_regtime>=to_date(‘‘20150201‘‘,‘‘yyyymmdd‘‘) and a.c_regtime<to_date(‘‘20150301‘‘,‘‘yyyymmdd‘‘) then a.c_msisdn else null end) 二月,
count(case when a.c_regtime>=to_date(‘‘20150301‘‘,‘‘yyyymmdd‘‘) and a.c_regtime<to_date(‘‘20150401‘‘,‘‘yyyymmdd‘‘) then a.c_msisdn else null end) 三月,
count(case when a.c_regtime>=to_date(‘‘20150401‘‘,‘‘yyyymmdd‘‘) and a.c_regtime<to_date(‘‘20150501‘‘,‘‘yyyymmdd‘‘) then a.c_msisdn else null end) 四月,
count(case when a.c_regtime>=to_date(‘‘20150501‘‘,‘‘yyyymmdd‘‘) and a.c_regtime<to_date(‘‘20150601‘‘,‘‘yyyymmdd‘‘) then a.c_msisdn else null end) 五月,
count(case when a.c_regtime>=to_date(‘‘20150601‘‘,‘‘yyyymmdd‘‘) and a.c_regtime<to_date(‘‘20150701‘‘,‘‘yyyymmdd‘‘) then a.c_msisdn else null end) 六月,
count(case when a.c_regtime>=to_date(‘‘20150701‘‘,‘‘yyyymmdd‘‘) and a.c_regtime<to_date(‘‘20150801‘‘,‘‘yyyymmdd‘‘) then a.c_msisdn else null end) 七月,
count(case when a.c_regtime>=to_date(‘‘20150801‘‘,‘‘yyyymmdd‘‘) and a.c_regtime<to_date(‘‘20150901‘‘,‘‘yyyymmdd‘‘) then a.c_msisdn else null end) 八月,
count(case when a.c_regtime>=to_date(‘‘20150901‘‘,‘‘yyyymmdd‘‘) and a.c_regtime<to_date(‘‘20151001‘‘,‘‘yyyymmdd‘‘) then a.c_msisdn else null end) 九月,
count(case when a.c_regtime>=to_date(‘‘20151001‘‘,‘‘yyyymmdd‘‘) and a.c_regtime<to_date(‘‘20151101‘‘,‘‘yyyymmdd‘‘) then a.c_msisdn else null end) 十月
from ‘||bb||‘.alluserinfo a‘;
commit;
end loop;
end;

 

 


CREATE OR REPLACE PROCEDURE BJ_G.wu_msisdn_to_file_HP IS
handle utl_file.file_type;
TYPE ref_cursor_type IS REF CURSOR;
ref_cursor ref_cursor_type;
v_msisdn VARCHAR2(15);
FILE_NAME VARCHAR2(40);
v_sql varchar2(2000);
cursor user_name is select * from t_province where prov_sh in (select prov_sh from temp_08);
aa varchar2(10);
bb varchar2(10);
cc varchar2(10);
begin
for i in user_name loop
select i.prov_id into aa from dual;
select i.prov_name into bb from dual;
select i.prov_sh into cc from dual;
FILE_NAME := bb||‘.txt‘;
handle := UTL_FILE.FOPEN(‘AA‘, FILE_NAME, ‘w‘);
if cc not in (‘HN_G‘,‘HEB_G‘,‘HUBEI_G‘,‘JL_G‘,‘YN_G‘,‘SHAN_G‘,‘ZJ_G‘,‘CQ_G‘,‘FJ_G‘,‘GZ_G‘,‘HLJ_G‘,‘XJ_G‘) then
v_sql :=‘select distinct c_msisdn from ‘||cc||‘.t_batch_mt where (c_status!=‘‘0‘‘ or c_status is null)
and c_taskid in (select taskid from temp_08 where prov_sh=‘||‘‘‘‘||cc||‘‘‘‘||‘)‘;
else
v_sql :=‘select distinct c_msisdn from ‘||cc||‘.t_batch_mt where (c_status!=‘‘0‘‘ or c_status is null)
and c_taskid in (select taskid from temp_08 where prov_sh=‘||‘‘‘‘||cc||‘‘‘‘||‘)
and c_msisdn not in (select c_msisdn from ‘||cc||‘.t_batch_mt where c_status in
(select taskid from temp_07 where prov_sh=‘||‘‘‘‘||cc||‘‘‘‘||‘))‘;
end if;
open ref_cursor for v_sql;
LOOP
FETCH ref_cursor
INTO v_msisdn ;
EXIT WHEN ref_cursor%NOTFOUND;
UTL_FILE.put_line(handle,
v_msisdn );
END LOOP;
UTL_FILE.fclose(handle);
end loop;
end;
/

 

 

 

--按日统计
CREATE OR REPLACE PROCEDURE LLTH.APP_DAY_JFTJ IS
handle utl_file.file_type;
TYPE ref_cursor_type IS REF CURSOR;
ref_cursor ref_cursor_type;
tb varchar2(20);
v_msisdn VARCHAR2(15);
FILE_NAME VARCHAR2(40);
v_date VARCHAR2(10);
v_sql VARCHAR2(2000);
v_text VARCHAR2(1000);
v_appkey VARCHAR2(100);
begin
select to_char(sysdate,‘yyyymmdd‘) into v_date from dual;
FILE_NAME :=‘bill_‘||v_date||‘.txt‘;
handle := UTL_FILE.FOPEN(‘JFTJ‘, FILE_NAME, ‘w‘);
v_sql :=‘select distinct o.msisdn||‘‘|‘‘||s.WO_APP_KEY||‘‘|‘‘||trunc(s.service_fee,2)||‘‘|‘‘||o.order_id
from t_order o, t_service s
where to_char(o.order_time,‘‘yyyymmdd‘‘) = to_char(trunc(sysdate)-3,‘‘yyyymmdd‘‘)
and sysdate-3 >= trunc(sysdate,‘‘month‘‘)
and o.order_status in (1, 6, 9)
and s.service_id = o.service_id
and o.is_fee=1
and o.province_id not in (select prov_id from t_cardproduct_auth t where t.prov_isfee=1)‘;
open ref_cursor for v_sql;
LOOP
fetch ref_cursor into v_text;
exit when ref_cursor%NOTFOUND;
UTL_FILE.put_line(handle,v_text);
END LOOP;
UTL_FILE.fclose(handle);
close ref_cursor;
FILE_NAME :=‘msisdn_‘||v_date||‘.txt‘;
handle := UTL_FILE.FOPEN(‘JFTJ‘, FILE_NAME, ‘w‘);
v_sql :=‘select distinct o.msisdn
from t_order o, t_service s
where to_char(o.order_time,‘‘yyyymmdd‘‘) = to_char(trunc(sysdate)-3,‘‘yyyymmdd‘‘)
and sysdate-3 >= trunc(sysdate,‘‘month‘‘)
and o.order_status in (1, 6, 9)
and s.service_id = o.service_id
and o.is_fee=1
and o.province_id not in (select prov_id from t_cardproduct_auth t where t.prov_isfee=1)‘;

open ref_cursor for v_sql;
LOOP
fetch ref_cursor into v_msisdn;
exit when ref_cursor%NOTFOUND;
UTL_FILE.put_line(handle,v_msisdn);
END LOOP;
UTL_FILE.fclose(handle);
close ref_cursor;
FILE_NAME :=‘app_‘||v_date||‘.csv‘;
handle := UTL_FILE.FOPEN(‘JFTJ‘, FILE_NAME, ‘w‘);
v_sql :=‘select distinct appkey from t_service
where to_char(update_time,‘‘yyyymmdd‘‘)=to_char(sysdate,‘‘yyyymmdd‘‘)
and appkey not in (select appkey from t_service where update_time<trunc(sysdate))‘;
open ref_cursor for v_sql;
LOOP
fetch ref_cursor into v_appkey;
exit when ref_cursor%NOTFOUND;
UTL_FILE.put_line(handle,v_appkey);
END LOOP;
UTL_FILE.fclose(handle);
close ref_cursor;
end;
/

 

 

 

--按月统计
CREATE OR REPLACE PROCEDURE LLTH.APP_MON_JFTJ IS
handle utl_file.file_type;
TYPE ref_cursor_type IS REF CURSOR;
ref_cursor ref_cursor_type;
tb varchar2(20);
v_msisdn VARCHAR2(15);
FILE_NAME VARCHAR2(40);
v_date VARCHAR2(10);
v_sql VARCHAR2(2000);
v_text VARCHAR2(1000);
v_appkey VARCHAR2(100);
begin
select to_char(sysdate,‘yyyymmdd‘) into v_date from dual;
FILE_NAME :=‘bill_‘||v_date||‘.txt‘;
handle := UTL_FILE.FOPEN(‘JFTJ‘, FILE_NAME, ‘w‘);
v_sql :=‘select distinct o.msisdn||‘‘|‘‘||s.WO_APP_KEY||‘‘|‘‘||trunc(s.service_fee,2)||‘‘|‘‘||o.order_id
from t_order o, t_service s
where o.order_time < trunc(sysdate)
and o.order_status in (1, 6, 9)
and s.service_id = o.service_id
and o.is_fee=1
and o.province_id not in (select prov_id from t_cardproduct_auth t where t.prov_isfee=1)‘;
open ref_cursor for v_sql;
LOOP
fetch ref_cursor into v_text;
exit when ref_cursor%NOTFOUND;
UTL_FILE.put_line(handle,v_text);
END LOOP;
UTL_FILE.fclose(handle);
close ref_cursor;
FILE_NAME :=‘msisdn_‘||v_date||‘.txt‘;
handle := UTL_FILE.FOPEN(‘JFTJ‘, FILE_NAME, ‘w‘);
v_sql :=‘select distinct o.msisdn
from t_order o, t_service s
where o.order_time < trunc(sysdate)
and o.order_status in (1, 6, 9)
and s.service_id = o.service_id
and o.is_fee=1
and o.province_id not in (select prov_id from t_cardproduct_auth t where t.prov_isfee=1)‘;

open ref_cursor for v_sql;
LOOP
fetch ref_cursor into v_msisdn;
exit when ref_cursor%NOTFOUND;
UTL_FILE.put_line(handle,v_msisdn);
END LOOP;
UTL_FILE.fclose(handle);
close ref_cursor;
FILE_NAME :=‘app_‘||v_date||‘.csv‘;
handle := UTL_FILE.FOPEN(‘JFTJ‘, FILE_NAME, ‘w‘);
v_sql :=‘select distinct appkey from t_service‘;
open ref_cursor for v_sql;
LOOP
fetch ref_cursor into v_appkey;
exit when ref_cursor%NOTFOUND;
UTL_FILE.put_line(handle,v_appkey);
END LOOP;
UTL_FILE.fclose(handle);
close ref_cursor;
end;
/




CREATE OR REPLACE PROCEDURE BJ_G.zj_msisdn_to_file_whq IS
handle utl_file.file_type;
TYPE ref_cursor_type IS REF CURSOR;
ref_cursor ref_cursor_type;
v_msisdn VARCHAR2(15);
FILE_NAME VARCHAR2(40);
v_sql varchar2(2000);
tk_id varchar2(20);
cc varchar2(20);
cursor user_name is
select * from temp_whq1;
type tablename is ref cursor;
tablenamel tablename;
BEGIN
for i in user_name loop
select i.taskid into tk_id from dual;
select i.prov_sh into cc from dual;
FILE_NAME := cc||‘.txt‘;
handle := UTL_FILE.FOPEN(‘AA‘, FILE_NAME, ‘w‘);
v_sql:=‘select c_msisdn from (select c_msisdn,count(*) c1,c_smscount c2 from ‘||cc||‘.t_batch_mt
where C_taskid = ‘||tk_id||‘ and C_actiontype = 3 and C_status = ‘‘0‘‘
group by c_msisdn,c_smscount) where c1>=c2 intersect
select c_msisdn from ‘||cc||‘.t_batch_mt where C_taskid = ‘||tk_id||‘ and C_actiontype = 9 and C_status = ‘‘0‘‘‘;
open ref_cursor for v_sql;
LOOP
FETCH ref_cursor
INTO v_msisdn ;
EXIT WHEN ref_cursor%NOTFOUND;
UTL_FILE.put_line(handle,
v_msisdn );
END LOOP;
UTL_FILE.fclose(handle);
close ref_cursor;
end loop;
end;
/

 


CREATE OR REPLACE PROCEDURE bj_g.p_update_otakey as

l_sql1 varchar2(1000);
l_sql2 varchar2(1000);
l_sql3 varchar2(1000);
l_sql4 varchar2(1000);
l_sql5 varchar2(1000);
l_sql6 varchar2(1000);
l_sql7 varchar2(1000);
l_sql8 varchar2(1000);
l_sql9 varchar2(1000);
l_sql10 varchar2(1000);
uname varchar2(20);
cursor user_name is select prov_sh from bj_g.t_province where prov_sh NOT IN (‘BJ_G‘);

begin
for user_name_l in user_name loop
select user_name_l.prov_sh into uname from dual ;
l_sql1 :=‘update ‘||uname||‘.t_otakey set c_keydata=‘‘CB6631B9337088F74EAC5555CD8CC101‘‘ where c_batchno =‘‘31‘‘ and c_cvendorid=‘‘0B‘‘ and c_functionid= ‘‘0‘‘ and c_index=‘‘0‘‘‘;
execute immediate l_sql1;
l_sql2 :=‘update ‘||uname||‘.t_otakey set c_keydata=‘‘DDB5B0D5CC15B5E7ABD7FF4FCAEE7D09‘‘ where c_batchno =‘‘31‘‘ and c_cvendorid=‘‘0B‘‘ and c_functionid= ‘‘0‘‘ and c_index=‘‘1‘‘‘;
execute immediate l_sql2;
l_sql3 :=‘update ‘||uname||‘.t_otakey set c_keydata=‘‘0D89C4DE52085A5FEE026D7A9B94F63D‘‘ where c_batchno =‘‘31‘‘ and c_cvendorid=‘‘0B‘‘ and c_functionid= ‘‘0‘‘ and c_index=‘‘2‘‘‘;
execute immediate l_sql3;
l_sql4 :=‘update ‘||uname||‘.t_otakey set c_keydata=‘‘F7F17FCEAF49EB4A1F1804D92539C707‘‘ where c_batchno =‘‘31‘‘ and c_cvendorid=‘‘0B‘‘ and c_functionid= ‘‘0‘‘ and c_index=‘‘3‘‘‘;
execute immediate l_sql4;
l_sql5 :=‘update ‘||uname||‘.t_otakey set c_keydata=‘‘869966FC9925A12CC0A9004F432417D2‘‘ where c_batchno =‘‘31‘‘ and c_cvendorid=‘‘0B‘‘ and c_functionid= ‘‘0‘‘ and c_index=‘‘4‘‘‘;
execute immediate l_sql5;
l_sql6 :=‘update ‘||uname||‘.t_otakey set c_keydata=‘‘25081515B242B1888988F48DFC031B08‘‘ where c_batchno =‘‘31‘‘ and c_cvendorid=‘‘0B‘‘ and c_functionid= ‘‘1‘‘ and c_index=‘‘0‘‘‘;
execute immediate l_sql6;
l_sql7 :=‘update ‘||uname||‘.t_otakey set c_keydata=‘‘391EA43E194CBBF200B89D5D56C004E9‘‘ where c_batchno =‘‘31‘‘ and c_cvendorid=‘‘0B‘‘ and c_functionid= ‘‘1‘‘ and c_index=‘‘1‘‘‘;
execute immediate l_sql7;
l_sql8 :=‘update ‘||uname||‘.t_otakey set c_keydata=‘‘F5801E5AF9538E07BDF9152CD880E658‘‘ where c_batchno =‘‘31‘‘ and c_cvendorid=‘‘0B‘‘ and c_functionid= ‘‘1‘‘ and c_index=‘‘2‘‘‘;
execute immediate l_sql8;
l_sql9 :=‘update ‘||uname||‘.t_otakey set c_keydata=‘‘7D1B82F39C9BEE94CA5CADE8658623C7‘‘ where c_batchno =‘‘31‘‘ and c_cvendorid=‘‘0B‘‘ and c_functionid= ‘‘1‘‘ and c_index=‘‘3‘‘‘;
execute immediate l_sql9;
l_sql10 :=‘update ‘||uname||‘.t_otakey set c_keydata=‘‘2F1283032CEBF9DEB0741C6CB3781C78‘‘ where c_batchno =‘‘31‘‘ and c_cvendorid=‘‘0B‘‘ and c_functionid= ‘‘1‘‘ and c_index=‘‘4‘‘‘;
execute immediate l_sql10;
commit;
end loop;
commit;
end;

 

CREATE OR REPLACE PROCEDURE otas.p_create_molog AUTHID current_user as

l_month varchar2(6);
l_nextmonth varchar2(6);
l_sql varchar2(1000);
l_sql1 varchar2(1000);
l_sql2 varchar2(1000);
l_sql3 varchar2(1000);
l_sql4 varchar2(1000);
uname varchar2(20);
cursor user_name is select sf from otas.t_province where sf NOT in(‘LN‘);

v_i varchar2(20);

begin
for user_name_l in user_name loop
select user_name_l.sf into uname from dual ;

for i in 1..12 loop
select lpad(i,2,‘0‘) into v_i from dual;
l_month:=‘2017‘||v_i;
select to_char(add_months(to_date(l_month,‘yyyymm‘),+1),‘YYYYMM‘) into l_nextmonth from dual;

l_sql :=‘CREATE TABLE ‘||uname||‘.T_MOLOG‘||l_nextmonth||‘ as select * from ‘ ||uname||‘.t_molog‘||l_month||‘ where 1<>1‘ ;
execute immediate l_sql;

l_sql1 :=‘CREATE UNIQUE INDEX ‘||uname||‘.T_MOLOG‘||l_nextmonth||‘_PK ON ‘||uname||‘.T_MOLOG‘||l_nextmonth||‘(C_AUTONO)‘;
execute immediate l_sql1;
l_sql2 :=‘ALTER TABLE ‘ ||uname||‘.T_MOLOG‘||l_nextmonth||‘ ADD CONSTRAINT T_MOLOG‘||l_nextmonth||‘_PK PRIMARY KEY (c_autono) using index ‘||uname||‘.T_MOLOG‘||l_nextmonth||‘_PK‘;
execute immediate l_sql2;
l_sql3 :=‘CREATE SEQUENCE ‘||uname||‘.SEQUENCE_MOLOG‘||l_nextmonth||‘ START WITH 1 MAXVALUE 99999999999 MINVALUE 1 NOCYCLE CACHE 20 NOORDER‘;
execute immediate l_sql3;
l_sql4 :=‘CREATE OR REPLACE TRIGGER ‘||uname||‘.TRG_SEQNO_MOLOG‘||l_nextmonth||‘ BEFORE INSERT ON ‘||uname||‘.T_MOLOG‘||l_nextmonth||‘ FOR EACH ROW BEGIN
SELECT ‘||uname||‘.SEQUENCE_MOLOG‘||l_nextmonth||‘.NEXTVAL INTO :NEW.c_autono FROM DUAL;END;‘;
execute immediate l_sql4;
end loop;
DBMS_OUTPUT.PUT_LINE(UNAME);
end loop;
end;

 


--推送总数(不去重)
create table temp_pl(prov varchar2(20),count varchar2(20));

CREATE OR REPLACE PROCEDURE bj_g.push_sucesses as

l_sql varchar2(1000);
uname varchar2(20);
cnt1 varchar2(20);
cursor user_name is select prov_sh from bj_g.t_province;

begin
for user_name_l in user_name loop
select user_name_l.prov_sh into uname from dual ;
execute immediate ‘insert into temp_pl select ‘||‘‘‘‘||uname||‘‘‘‘||‘,count(*) from ‘||uname||‘.t_batch_mt where to_char(c_intime,‘‘yyyymm‘‘)=‘‘201702‘‘‘;
commit;
end loop;
commit;
end;
/

--推送总数-去重-按手机号去重,要的是用户数
create table temp_pl1(prov varchar2(20),count varchar2(20));


CREATE OR REPLACE PROCEDURE bj_g.push_sucesses_1 as
l_sql varchar2(1000);
uname varchar2(20);
cnt1 varchar2(20);
cursor user_name is select prov_sh from bj_g.t_province;

begin
for user_name_l in user_name loop
select user_name_l.prov_sh into uname from dual ;
execute immediate ‘insert into temp_pl1(prov,count) select ‘||‘‘‘‘||uname||‘‘‘‘||‘,count(c_msisdn) from (
select c_msisdn,c_intime from(
select c_msisdn,c_intime,row_number() over(partition by c_msisdn order by c_intime desc) rn
from ‘||uname||‘.t_batch_mt where to_char(c_intime,‘‘yyyymm‘‘)=‘‘201702‘‘ ) where rn=1)‘;
commit;
end loop;
commit;
end;
/

select * from temp_pl1 order by prov;

--应用成功数-不去重

select count(*) from tag.t_information_push where to_char(push_date,‘yyyymm‘)=‘201702‘;


--应用成功数-去重

select count(distinct msisdn) from tag.t_information_push where to_char(push_date,‘yyyymm‘)=‘201702‘;


--订购数

select count(distinct o.msisdn) from llth.t_order o,tag.t_information_push t
where o.order_time >= to_date(‘2017-02-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)
and o.order_time < to_date(‘2017-03-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)
and (o.cancel_time is null or (o.cancel_time>=to_date(‘2017-03-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)))
and o.order_status in (1,2,3,6,9,12)
and o.order_channel=1
and o.msisdn = t.msisdn
and to_char(t.push_date,‘yyyymm‘)=‘201702‘; --数据不准确,,数量是 185163

 

--在sms9885提取双计费订购用户--不去重,然后入库到OTA bj_g的临时表中:

select mdn from service_users a,services b where a.service_id=b.id
and service_id in (666,668,688,888)
and sub_date BETWEEN TO_DATE (‘2017-02-01 00:00:00‘, ‘yyyy-mm-dd hh24:mi:ss‘)
AND TO_DATE (‘2017-02-28 23:59:59‘, ‘yyyy-mm-dd hh24:mi:ss‘)
and (unsub_date >= TO_DATE (‘2017-03-01 00:00:00‘, ‘yyyy-mm-dd hh24:mi:ss‘) or unsub_date is null);

create table temp_sjf(mdn varchar2(11));

 

 

批量推送订购数:


temp_auto_user:自动更新 parentid值: 参考消息 37 知音 41 特惠礼包35 环球时报 36


declare
uname varchar2(20);
v_prov_id varchar2(10);
cursor user_name is
select * from t_province_bak;
date_a varchar2(20);
date_b varchar2(20);
begin
for user_name_l in user_name loop
select user_name_l.prov_sh into uname from dual;
select user_name_l.prov_id into v_prov_id from dual;
for i in 1..28 loop
if i<10 then
date_a:=‘2017020‘||to_char(i);
else
date_a:=‘201702‘||to_char(i);
end if;
execute immediate ‘insert into temp_auto_user(msisdn,exporttime) select c_msisdn,c_sentdate
from (select distinct c_msisdn,c_sentdate from ‘||uname||‘.t_mtlog201702
where C_parentid in (‘‘35‘‘,‘‘36‘‘,‘‘41‘‘,‘‘37‘‘)
and C_menuid=1
and to_char(c_sentdate,‘‘yyyymmdd‘‘) =‘||‘‘‘‘||date_a||‘‘‘‘||‘)‘;
commit;
end loop;
end loop;
end;
/

 

temp_batch_update_user: 批量更新

CREATE OR REPLACE procedure LLTH.wu_msisdn_to_file_A3
is
cursor user_name is
select t1.prov_id,t1.prov_sh,t.taskid from temp_07 t,t_province_bak t1 where t.prov_sh=t1.prov_sh;
dt_l date;
date_b varchar2(20);
v_sql varchar2(2000);
aa varchar2(20);
bb varchar2(20);
begin
for i in user_name loop
select i.prov_id into aa from dual;
select i.prov_sh into bb from dual;
execute immediate ‘insert into temp_batch_update_user(msisdn,exporttime) select c_msisdn,date_a
from (select c_msisdn,to_char(date_a,‘‘yyyy/mm/dd hh24:mi:ss‘‘) date_a from (
select t.c_msisdn,c_intime as date_a,row_number()over(partition by c_msisdn order by c_intime) rn from ‘||bb||‘.t_batch_mt t
where t.c_taskid in (select taskid from temp_07 where prov_sh=‘||‘‘‘‘||bb||‘‘‘‘||‘)
and t.c_status=‘‘0‘‘) where rn=1)‘;
commit;
end loop;
end;
/


订购数---不去重:

create table tempzj1 as select * from temp_batch_update_user where msisdn not in (select msisdn from temp_auto_user);

select count(o.msisdn) from llth.t_order o --,llth.tempzj1 u
where o.order_time> to_date(‘2017-02-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)
and o.order_time< to_date(‘2017-03-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)
and (o.cancel_time is null or (o.cancel_time>=to_date(‘2017-03-01 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)))
and o.order_status in (1,2,3,6,9,12)
and o.order_channel=1
and o.msisdn in(select msisdn from llth.tempzj1 u);

------------数量是16010


select count(mdn) from temp_sjf
where mdn in (select msisdn from llth.tempzj1 u);

 

 








































































































































































































































































































































































































































































































































































以上是关于plsql例子的主要内容,如果未能解决你的问题,请参考以下文章

plsql一个函数的例子

oracle左连接查询和右连接查询随便举个例子谢谢!

如何在PLSQL过程中做简单的数组

使用 PLSQL 检索节点内容

Navicat下Oracle数据泵的使用简单例子

oracle和plsql developer编码设置