create
or replace procedure
pro_add_tab_partitions
(v_rece_code out varchar2,
v_rece_msg out varchar2)
isv_row_count
number := 0;
v_sql varchar2(2000);
v_dt varchar2(20);
v_dt2 varchar2(20);
v_p_dt varchar2(20);
v_max_part date;
v_next_mday date;
type type_tbl is table of varchar2(100) index
by binary_integer;
v_tbl_name type_tbl;
begin
v_row_count := v_row_count + sql%rowcount;
--建立月分割槽
--將需要增加分割槽的月表加在此處
select table_name bulk collect
into v_tbl_name
from user_tables
where table_name in ('t_pro_update_log');
for i in v_tbl_name.first .. v_tbl_name.last loop
--取出分割槽表中最大分割槽值
select to_date(max(substr(partition_name, 2)), 'yyyymm')
into v_max_part
from user_tab_partitions
where table_name = v_tbl_name(i);
--當前時間下月末最後一天
select add_months(trunc(last_day(sysdate)), 1)
into v_next_mday
from dual;
--迴圈執行
while v_max_part < v_next_mday loop
v_max_part := add_months(v_max_part, 1);
v_p_dt := to_char(v_max_part, 'yyyymm');
v_dt := to_char(add_months(v_max_part, 1), 'yyyymmdd');
v_sql := 'alter table ' || v_tbl_name(i) || ' add partition p' ||
v_p_dt || ' values less than (to_date(' || chr(39) || v_dt ||
chr(39) || ',' || chr(39) || 'yyyymmdd' || chr(39) || '))';
execute immediate v_sql;
endloop;
endloop;
--建立日分割槽
--在日分割槽加的
select table_name bulk collect
into v_tbl_name
from user_tables
where table_name in ('rel_transaction_to_aep_msg','rel_transaction_to_cmpp_msg','rel_transaction_to_cmpp_report','log_aep_deliver','log_aep_report','log_aep_submit','log_aep_submit_response','log_cmpp_deliver','log_cmpp_deliver_response','log_cmpp_report','log_cmpp_report_response','log_cmpp_submit','log_cmpp_submit_response','log_sms_transaction');
for i in v_tbl_name.first .. v_tbl_name.last loop
--取出分割槽表中最大分割槽值
select to_date(max(substr(partition_name, 2)), 'yyyymmdd')
into v_max_part --獲取最大分割槽值
from user_tab_partitions
where table_name = v_tbl_name(i);
--當前時間下月末最後一天
select add_months(trunc(last_day(sysdate)), 1)
into v_next_mday
from dual;
--迴圈執行
while v_max_part < v_next_mday loop
v_max_part := v_max_part + 1;
v_dt := to_char(v_max_part, 'yyyymmdd');
--new
add20160923
begin
by hj
v_dt2 := to_char(v_max_part + 1, 'yyyymmdd');
--end
v_sql := 'alter table ' || v_tbl_name(i) || ' add partition p' || v_dt ||
' values less than (to_date(' || chr(39) || v_dt2 || chr(39) || ',' ||
chr(39) || 'yyyymmdd' || chr(39) || '))';
execute immediate v_sql;
endloop;
endloop;
v_rece_code := 'finish';
v_rece_msg := 'success';
--插入日誌
pro_update_log(to_char(sysdate, 'yyyymmdd'),
'自動增加分割槽', --目標表名稱
'','pro_add_tab_partitions', --目標儲存過程名稱
v_rece_code,
v_rece_msg,
v_row_count);
--異常丟擲
exception
when others then
v_rece_code := 'fail';
v_rece_msg := substr(sqlerrm, 1, 100);
--異常處理
pro_update_log(to_char(sysdate, 'yyyymmdd'),
'自動增加分割槽', --目標表名稱
'','pro_add_tab_partitions', --目標儲存過程名稱
v_rece_code,
v_rece_msg,
v_row_count);
end pro_add_tab_partitions;
create
or replace procedure
pro_update_log
(v_time varchar2,
v_func_desc varchar2,
v_table_name varchar2,
v_pro_name varchar2,
v_rece_code varchar2,
v_rece_msg varchar2,
v_row_count number)
isbegin
insert
into
t_pro_update_log
(id,
time,
func_desc,
table_name,
pro_name,
rece_code,
rece_msg,
create_time,
row_count)
values
(seq_t_pro_update_log.nextval,
v_time,
v_func_desc,
v_table_name,
v_pro_name,
v_rece_code,
v_rece_msg,
sysdate,
v_row_count);
commit;
end;
在ORACLE儲存過程中建立臨時表
create procedure pro asstr varchar2 100 begin str create global temporary table tablename col1 varchar2 10 col2 number on mit preserve rows execute im...
在ORACLE儲存過程中建立臨時表
create procedure pro asstr varchar2 100 begin str create global temporary table tablename col1 varchar2 10 col2 number on commit preserve rows execute...
在ORACLE儲存過程中建立臨時表
在oracle儲存過程中建立臨時表 儲存過程裡不能直接使用ddl語句,所以只能使用動態sql語句來執行 on commit delete rows 說明臨時表是事務指定,每次提交後oracle將截斷表 刪除全部行 on commit preserve rows 說明臨時表是會話指定,當中斷會話時or...