维护的ORACLE 10G数据库中分区表几十上百个,当需要添加新分区的时候非常麻烦,就写了下面两个
简易的SQL语句来生成批量添加分区的脚本,如在新增分区时想索引放在另外表空间,对每个索引要先
执行下面语句:
--ALTER INDEX IDX_TABXX MODIFY DEFAULT ATTRIBUTES TABLESPACE P_IND_03_P2011;
按天分区:
select 'alter table '
||'&OWNER_NAME'
||'.'
||'&TAB_NAME'
||' split partition '
||'&MAX_PARTNAME'
||' at ('
||''''
||to_char((to_date('&START_PART_DT','yyyymmdd')+level ),'yyyymmdd')
||''''
||') INTO (PARTITION '
||'&TAB_NAME'
||'_'
||to_char((to_date('&START_PART_DT','yyyymmdd')+level-1 ),'yyyymmdd')
||' tablespace '
||'&TBS_NAME'
||',partition '
||'&MAX_PARTNAME'
||' );'
from dual CONNECT BY LEVEL <=
(SELECT (TO_DATE('&END_DT','YYYYMMDD')+1 - TO_DATE('&START_PART_DT','YYYYMMDD')) AS CNT FROM DUAL)
按月分区:
select 'alter table '
||'&OWNER_NAME'
||'.'
||'&TAB_NAME'
||' split partition '
||'&MAX_PARTNAME'
||' at ('
||''''
||TO_CHAR(add_months(TO_DATE('&START_PART_DT', 'YYYYMMDD'), level),'YYYYMMDD')
||''''
||') INTO (PARTITION '
||'&TAB_NAME'
||'_'
||TO_CHAR(add_months(TO_DATE('&START_PART_DT', 'YYYYMMDD'), level-1),'YYYYMM')
||' tablespace '
||'&TBS_NAME'
||',partition '
||'&MAX_PARTNAME'
||' );'
from dual CONNECT BY LEVEL < 13