Move a Tablespace

This may be pointless, but maybe some of the SQL may help someone.  It is an attempt at boiling all the steps from another post into one SQL script.  The rename at the end didn’t seem to work for some reason, and I didn’t want to put any more time into it.


declare

  tblName varchar(25);

  tempName varchar(25);

  segmentCount number(10);

  osOutput number(3);

begin

   tblName := 'AAAPP';

   dbms_output.put_line('Moving tablespace: ' || tblName);

   tempName := tblName || 'NEW';

   dbms_output.put_line('Using temporary tablespace: ' || tempName);

   select count(*) into segmentCount from dba_segments

   where tablespace_name = tblName;

   dbms_output.put_line('Currently ' || segmentCount || ' segments');

   execute immediate 'create tablespace ' || tempName ||

       ' datafile ''e:\oradata\HCM90\' || tempName || '.DBF''' ||

       ' size 10m ' ||

       ' extent management local autoallocate ' ||

       ' segment space management auto ';

   execute immediate 'ALTER DATABASE DATAFILE ''e:\oradata\HCM90\' ||

      tempName || '.DBF'' AUTOEXTEND ON NEXT 5M MAXSIZE UNLIMITED'; 

   for index_rec in (select owner, segment_name 

                     from dba_segments 

                     where segment_type = 'TABLE' 

                     and tablespace_name = tblName)

   loop                     

      execute immediate 'alter table ' || index_rec.owner || '.' || 

          index_rec.segment_name || ' move tablespace ' || tempName; 

   end loop;

   for index_rec in (select owner, table_name, column_name 

                     from dba_lobs 

                     where tablespace_name = tblName)

   loop                     

      execute immediate 'alter table ' || index_rec.owner || '.' || 

          index_rec.table_name || ' move lob(' || index_rec.column_name ||

          ') store as (tablespace ' || tempName || ')'; 

   end loop;

   select count(*) into segmentCount from dba_segments

   where tablespace_name = tblName;

   dbms_output.put_line('Now ' || segmentCount || ' segments');

   execute immediate 'alter tablespace ' || tblName || ' offline';

   execute immediate 'drop tablespace ' || tblName;

   execute immediate 'alter tablespace ' || tempName || ' rename to ' || tblName;

   dbms_output.put_line('Renaming ' || tempName || ' to ' || tblName);

   dbms_pipe.pack_message('cmd /c del e:\oradata\HCM90\' || tblName || '.DBF'); 

   osOutput := dbms_pipe.send_message('HOST_PIPE');

   dbms_output.put_line('Deleted e:\oradata\HCM90\' || tblName || '.DBF -- ' || osOutput);

end;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: