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

时间: 2024-11-04 00:19:31

plsql例子的相关文章

Oracle SQL语句大全(二)

avg(sal) desc;     查找部门中平均薪水打印2000的员工并按部门号进行排序,查询分组后的平均薪水必须大于1500,查询结果按平均薪水从低到高排列  49.select ename from emp where sal>(select avg(sal) from emp);    查找出员工中薪水位于部门平均薪水之上的所有员工  50.select ename,sal from emp join(select max(sal) max_sal from emp group by 

plsql函数返回数组集合例子

1 create or replace type t_test as object( 2 id integer, 3 rq date, 4 mc varchar2(60) 5 ); 6 7 create or replace type t_test_table as table of t_test; 8 9 create or replace function f_test_array(n in number default null) return t_test_table 10 as 11

plsql一个函数的例子

------陈渝文统计报表优化一下午 1 create or replace type t_test as object 2 ( 3 HDID varchar2(60), 4 省份 varchar2(60), 5 派驻检察室数目 varchar2(60), 6 检所1 varchar2(60), 7 检所2 varchar2(60), 8 检所3 varchar2(60), 9 检所4 varchar2(60) 10 ) 11 / 12 create or replace type t_test

plsql的错误信息与异常处理

7 错误消息与异常处理 7.1 异常的介绍 (1) 处理异常分为三个步骤:  A 声明异常  B 引发异常  C 处理异常 (2) 异常的特征  A 错误类型: ORA-xxxxx  运行时错误               PLS-xxxxx  编译错误  B 错误代码:xxxxx  C 错误的文本描述 案例1:编译错误的案例PLS SQL> create or replace procedure p1 is  2  begin  3  null;  4  end;  5  / Procedur

Oracle 中PLSQL的ftp应用

CREATE OR REPLACE PACKAGE BODY ftp AS -- -------------------------------------------------------------------------- -- Name : http://www.oracle-base.com/dba/miscellaneous/ftp.pkb -- Author : Tim Hall -- Description : Basic FTP API. For usage notes se

PLSQL中的三种参数模式IN、OUT、IN OUT

1.IN模式 IN模式是参数的默认模式,这种模式就是在程序运行的时候已经具有值,在程序体中值不会改变. 错误例子: 1 create or replace procedure in_proc ( 2 p_a in number , -- 形参, 这里的值是调用处传递的实参 3 p_b in number 4 )as 5 begin 6 p_a := 10 ; --实参被传递进来以后,在这个程序体中值就不可能被改变了 7 p_b := 20 ; 8 DBMS_OUTPUT.PUT_LINE(p_a

Oracle之PLSQL

PL/SQL Developer是一个集成开发环境,专门开发面向Oracle数据库的应用.PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL)它是Oracle数据库对SQL语句的扩展.在普通SQL语句的使用上增加了编程语言的特点,所以PL/SQL把数据操作和查询语句组织在PL/SQL代码的过程性单元中,通过逻辑判断.循环等操作实现复杂的功能或者计算,使SQL语言具有过程处理能力.PL/SQL 只有 Oracle 数据库有. 一个PL/SQL程序结构

Oracle PLSQL:

PLSQL: 符号: 赋值: := 基本示例: set serveroutput on //设置输出打开. declare --说明部分 begin --程序 dbms_output.put_line('Hello World'); exception end; / 引用型变量: --查询7839的姓名和薪水 --查询7839的姓名和薪水 set serveroutput on declare --定义变量保存姓名和薪水 --pename varchar2(20); --psal number;

【PLSQL】Oracle中的异常

一.摘要 在PLSQL程序开发过程中,很重要的部分就是对程序异常的监控和处理,包括如何触发异常,何时进行处理,如何进行处理,是否将程式中的所有异常集中在一起,通过公共异常处理的procedure或function,如果没有完善的程式处理机制,很难说该程式是一只健壮的程式,当程式遇到很多类型或者量很多资料时,系统若没有异常处理必然会导致程式的出错 当预判到了某些异常,需要对预判到的异常进行合适相应的处理,是否抛出异常还是忽略还是其他 当然程式没有预判到或者是一些未知的异常遇到,所以异常处理模块也要