根据需求,自动增加下月表分区,供参考/讨论!

[复制链接]
查看11 | 回复2 | 2009-3-4 17:06:25 | 显示全部楼层 |阅读模式
本帖最后由 帅帅的动力 于 2012-5-29 19:13 编辑
由于有20几个分区表,根据时间进行分区,每个月末都要新增分区,为了省事,干脆写个自动的,运行即可!
这部分代码是纸增加一个月的,如果想增加多个月,需改动一个参数即可。下面是代码。
如有更好的方法,可以给我回复啊!一起进步!
可以传一个参数,做成纯静态的,有兴趣的可以试一下啊!
-- Created on 2012-5-28 by ADMINISTRATOR
declare
-- Local variables here
i integer;
cursor partion_cur is
select t1.table_name,p.partition_name ,p.tablespace_name,m.物理文件名 wlwjm, m.大小M daxiao from user_part_tablest1
left join(
select b.table_name,max(b.partition_name) partition_name ,max(b.tablespace_name) tablespace_name from
USER_TAB_PARTITIONS b group by b.table_name) p on t1.table_name=p.table_name
left join(select
b.file_id 文件ID,
b.file_name 物理文件名,
b.tablespace_name 表空间,
b.bytes/1024/1024 大小M,
(b.bytes-sum(nvl(a.bytes,0)))/1024/1024 已使用M,
substr((b.bytes-sum(nvl(a.bytes,0)))/(b.bytes)*100,1,5) 利用率
from dba_free_space a,dba_data_files b
where a.file_id=b.file_id
group by b.file_id, b.tablespace_name,b.file_name,b.bytes
order byb.tablespace_name) m on p.tablespace_name=m.表空间
where t1.table_name!='GSM_WP_DA' order by t1.table_name
;
begin
for tt in partion_cur loop
select count(*) into i from user_tablespaces x where x.TABLESPACE_NAME=tt.tablespace_name;
--判断表空间是否已经存在,如果存在则执行增加数据文件

if i>0 then
--execute immediate 'alter tablespace '||tt.tablespace_name||' add datafile '||replace(tt.wlwjm,to_char(add_months(sysdate,-1),'yyyymm'),to_char(sysdate,'yyyymm'))||' size '||tt.daxiao||'M';
bms_output.put_line('alter tablespace '||tt.tablespace_name||' add datafile '''||replace(tt.wlwjm,to_char(sysdate,'yyyymm'),to_char(add_months(sysdate,1),'yyyymm'))||''' size '||tt.daxiao||'M');
elsif i=0 then --说明表空间现在不存在,则新建表空间
dbms_output.put_line('create tablespace '||replace(tt.tablespace_name,to_char(sysdate,'yyyymm'),to_char(add_months(sysdate,1),'yyyymm'))||' datafile '''||replace(tt.wlwjm,to_char(sysdate,'yyyymm'),to_char(add_months(sysdate,1),'yyyymm'))||''' size '||tt.daxiao||'M default storage
(

initial 64K

next 1M

minextents 1

maxextents unlimited

)');
end if;
--增加表分区
dbms_output.put_line('alter table '||tt.table_name||' add partition '''||replace(tt.partition_name,to_char(sysdate,'yyyy-mm'),to_char(add_months(sysdate,1)))||''' values less than (TO_DATE('''||to_char(add_months(sysdate,1),'yyyy-mm')||'-01 00:00:00'', ''SYYYY-MM-DD HH24:MI:SS'', ''NLS_CALENDAR=GREGORIAN'')) tablespace '||replace(tt.tablespace_name,to_char(sysdate,'yyyymm'),to_char(add_months(sysdate,1),'yyyymm'))||' pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
)');
end loop;
end;

回复

使用道具 举报

千问 | 2009-3-4 17:06:25 | 显示全部楼层
11G 就可以自动增加了
回复

使用道具 举报

千问 | 2009-3-4 17:06:25 | 显示全部楼层
jiqing1004 发表于 2012-5-28 18:27
11G 就可以自动增加了

恩,谢谢啊,我用的还是10G
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行