我有一个属于表空间报表的分区表.我想把它移动到表空间记录.
一种可能性是删除表并在新的表空间中重新创建它,但这对我来说不是一个选择,因为表中有数据需要在移动中生存.
我首先检查分区是否真的属于表空间报告:
SELECT * FROM user_tab_partitions WHERE table_name = 'REQUESTLOG';
然后我刚刚试过:
ALTER TABLE requestLog MOVE TABLESPACE record;
但是这给我错误ORA-145111“无法对分区对象执行操作”.
然后我发现我可以移动单独的分区:
ALTER TABLE requestLog MOVE PARTITION "2009-12-29" TABLESPACE report;
但是由于表中有60个分区(基于日期),并且因为我可能需要为几个系统执行此操作,所以我想循环遍历所有的分区名称,将每个分区移动到新的表空间.我尝试过,但不能让sql工作.
即使将所有现有分区移动到新的表空间,创建新分区时仍然存在问题.新的分区仍然在旧的表空间报表中创建.如何更改,以便在新的表空间记录中创建新的分区?
解决方法
您必须考虑可能无效的索引 – 为了涵盖关于重置默认表空间的问题,我认为这是您要实现的完整过程:
1)移动分区(根据zürigschnäzlets答案的PL / sql循环)
这些是我在匿名块包装器中使用的程序,它定义了a_tname,a_destTS,vTname和vTspName – 他们应该给出一般的想法:
procedure mvTabPart (a_tname in varchar2,a_destTS in varchar2) is cursor pCur(vTname varchar2,vTspName varchar2) is select table_name,partition_name from user_tab_partitions where table_name = vTname and tablespace_name not like vTspName order by partition_position desc; begin for pRow in pCur(a_tname,a_destTS) loop sqlStmnt := 'alter table '||pRow.table_name|| ' move partition '||pRow.partition_name|| ' tablespace '||a_destTS; execute immediate sqlStmnt; end loop; end mvTabPart;
2)设置表默认分区表空间,从而创建新的分区:
procedure setDefTabPart (a_tname in varchar2,a_destTS in varchar2) is cursor tCur(vTname varchar2) is select table_name from user_part_tables where table_name = vTname; begin for tRow in tCur(a_tname) loop sqlStmnt := 'alter table '||tRow.table_name|| ' modify default attributes '|| ' tablespace '||a_destTS; execute immediate sqlStmnt; end loop; end setDefNdxPart;
3)设置索引默认分区表空间,以便创建新的索引分区(如果有的话):
procedure setDefNdxPart (a_tname in varchar2,a_destTS in varchar2) is cursor iCur(vTname varchar2) is select index_name from user_part_indexes where index_name in (select index_name from user_indexes where table_name = vTname); begin for iRow in iCur(a_tname) loop sqlStmnt := 'alter index '||iRow.index_name|| ' modify default attributes '|| ' tablespace '||a_destTS; execute immediate sqlStmnt; end loop; end setDefNdxPart;
4)重建任何需要重建并且不在所需表空间中的分区索引:
procedure mvNdxPart (a_tname in varchar2,a_destTS in varchar2) is cursor ndxCur(vTname varchar2,vTspName varchar2) is select i.index_name index_name,ip.partition_name partition_name from user_ind_partitions ip,user_indexes i where i.index_name = ip.index_name and i.table_name = vTname and i.partitioned = 'YES' and (ip.tablespace_name not like vTspName or ip.status not like 'USABLE') order by index_name,partition_name ; begin for ndxRow in ndxCur(a_tname,a_destTS) loop sqlStmnt := 'alter index '||ndxRow.index_name|| ' rebuild partition '||ndxRow.partition_name|| ' tablespace '||a_destTS; execute immediate sqlStmnt ; end loop; end mvNdxPart;
5)重建任何全局索引
procedure mvNdx (a_tname in varchar2,vTspName varchar2) is select index_name from user_indexes where table_name = vTname and partitioned = 'NO' and (tablespace_name not like vTspName or status like 'UNUSABLE') order by index_name ; begin for ndxRow in ndxCur(a_tname,a_destTS) loop sqlStmnt := 'alter index '||ndxRow.index_name|| ' rebuild tablespace '||a_destTS; execute immediate sqlStmnt ; end loop; end mvNdx;