Question

I have a user named SDE and the tablespace for this users tables are in SDE_TBS, I would like to move all of these tables to tablespace SDEBUS_DT. For indexes I would like to move to SDEBUS_IX. How would I accomplish this? I have reference this thread, but it does not address my question completely.

Was it helpful?

Solution

What you want to do easily (assuming you have the appropriate permissions) is to invoke the PL/SQL procedure DBMS_REDEFINITION.REDEF_TABLE which you can use to change things such as tablespaces for data and for indexes. Here is a snippet of code for moving a table named MY_DATA_TABLE. To do several, you would need to invoke it once per table.

begin
    DBMS_REDEFINITION.REDEF_TABLE(
        uname=>'SDE'
        , tname=>'MY_DATA_TABLE'
        , table_part_tablespace=>'SDEBUS_DT'
        , index_tablespace=>'SDEBUS_IX'
   );

end;
/

But before you begin, just check to see if your SDE owner has permissions on the tablespaces SDEBUS_DT and SDEBUS_IX first. Also, if those commands are invoked by the SDE user, that user will need CREATE TABLE and CREATE MVIEW privileges.

Now if you want to loop through several tables, run a SQL like the following, which will display the commands to copy/paste. Yes, you could do the following in a PL/SQL loop, but I don't do that often enough to remember which of the 3 or 4 ways to do that, without looking it up!

-- omit the "execute" if you want to run the following output
--  in a BEGIN/END block instead of running each line, one at a time.

select 'execute DBMS_REDEFINITION.REDEF_TABLE(uname=>''' || 
    owner || ''', tname=>' ||
    '''' || table_name || '''' ||
    ', table_part_tablespace=>''SDEBUS_DT'' , index_tablespace=>''SDEBUS_IX'');' as cmd from dba_tables where owner='MARK.STEWART';

CMD
---------------------------------------------------------
execute DBMS_REDEFINITION.REDEF_TABLE(uname=>'MARK.STEWART', tname=>'DBMSHP_RUNS', table_part_tablespace=>'SDEBUS_DT' , index_tablespace=>'SDEBUS_IX');
execute DBMS_REDEFINITION.REDEF_TABLE(uname=>'MARK.STEWART', tname=>'DBMSHP_FUNCTION_INFO', table_part_tablespace=>'SDEBUS_DT' , index_tablespace=>'SDEBUS_IX');

OTHER TIPS

You can also use simple ALTER TABLE/INDEX command

for aTab in (select table_name from dba_tables where owner = 'SDE') loop
   execute immediate 'ALTER TABLE SDE.' ||aTab.table_name|| ' MOVE TABLESPACE SDEBUS_DT';
end loop;

for aInd in (select index_name from dba_indexes where owner = 'SDE') loop
   execute immediate 'ALTER INDEX SDE.' ||aInd.index_name|| ' REBUILD TABLESPACE SDEBUS_IX';
end loop;

But DBMS_REDEFINITION.REDEF_TABLE is more versatile than ALTER TABLE/INDEX, e.g. when user has partitioned tables.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top