set serveroutput on
set lines 300
DECLARE
V_CURRENT_TABLESPACE VARCHAR2(50);
V_DEST_TABLESPACE VARCHAR2(50);
BEGIN
V_CURRENT_TABLESPACE := '¤t_tablespace';
V_DEST_TABLESPACE := '&destination_tablespace';
FOR LIST_OWNER IN (SELECT DISTINCT OWNER FROM DBA_SEGMENTS WHERE TABLESPACE_NAME=V_CURRENT_TABLESPACE)
DBMS_OUTPUT.PUT_LINE('MOVING TABLES:');
DBMS_OUTPUT.PUT_LINE('------------------');
FOR LIST_TABLES IN (SELECT SEGMENT_NAME, SEGMENT_TYPE FROM DBA_SEGMENTS WHERE OWNER=LIST_OWNER.OWNER AND TABLESPACE_NAME=V_CURRENT_TABLESPACE AND SEGMENT_TYPE='TABLE')
LOOP
DBMS_OUTPUT.PUT_LINE('MOVING ' || LIST_TABLES.SEGMENT_TYPE || '' || LIST_TABLES.SEGMENT_NAME || '...');
V_SQL := 'ALTER TABLE "' || LIST_OWNER.OWNER || '"."' || LIST_TABLES.SEGMENT_NAME || '" MOVE TABLESPACE ' || V_DEST_TABLESPACE;
EXECUTE IMMEDIATE V_SQL;
END LOOP;
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('MOVING LOBS:');
DBMS_OUTPUT.PUT_LINE('------------------');
FOR LIST_LOBS IN (SELECT TABLE_NAME, COLUMN_NAME FROM DBA_LOBS WHERE OWNER=LIST_OWNER.OWNER AND TABLESPACE_NAME=V_CURRENT_TABLESPACE)
LOOP
DBMS_OUTPUT.PUT_LINE('MOVING LOB FROM TABLE ' || LIST_LOBS.TABLE_NAME || ' TO TABLESPACE ' || V_DEST_TABLESPACE || '...');
V_SQL := 'ALTER TABLE ' || LIST_OWNER.OWNER || '.' || LIST_LOBS.TABLE_NAME || ' MOVE LOB(' || LIST_LOBS.COLUMN_NAME || ') STORE AS (TABLESPACE ' || V_DEST_TABLESPACE || ')';
EXECUTE IMMEDIATE V_SQL;
END LOOP;
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('MOVING INDEXES:');
DBMS_OUTPUT.PUT_LINE('------------------');
FOR LIST_INDEXES IN (SELECT SEGMENT_NAME, SEGMENT_TYPE FROM DBA_SEGMENTS WHERE OWNER=LIST_OWNER.OWNER AND TABLESPACE_NAME='USERS' AND SEGMENT_TYPE='INDEX')
LOOP
DBMS_OUTPUT.PUT_LINE('MOVING ' || LIST_INDEXES.SEGMENT_TYPE || '' || LIST_INDEXES.SEGMENT_NAME || '...');
V_SQL := 'ALTER INDEX ' || LIST_OWNER.OWNER || '."' || LIST_INDEXES.SEGMENT_NAME || '" REBUILD TABLESPACE ' || V_DEST_TABLESPACE;
EXECUTE IMMEDIATE V_SQL;
END LOOP;
DBMS_OUTPUT.PUT_LINE('');
END LOOP;
END;
/
set lines 300
DECLARE
V_CURRENT_TABLESPACE VARCHAR2(50);
V_DEST_TABLESPACE VARCHAR2(50);
BEGIN
V_CURRENT_TABLESPACE := '¤t_tablespace';
V_DEST_TABLESPACE := '&destination_tablespace';
FOR LIST_OWNER IN (SELECT DISTINCT OWNER FROM DBA_SEGMENTS WHERE TABLESPACE_NAME=V_CURRENT_TABLESPACE)
DBMS_OUTPUT.PUT_LINE('MOVING TABLES:');
DBMS_OUTPUT.PUT_LINE('------------------');
FOR LIST_TABLES IN (SELECT SEGMENT_NAME, SEGMENT_TYPE FROM DBA_SEGMENTS WHERE OWNER=LIST_OWNER.OWNER AND TABLESPACE_NAME=V_CURRENT_TABLESPACE AND SEGMENT_TYPE='TABLE')
LOOP
DBMS_OUTPUT.PUT_LINE('MOVING ' || LIST_TABLES.SEGMENT_TYPE || '' || LIST_TABLES.SEGMENT_NAME || '...');
V_SQL := 'ALTER TABLE "' || LIST_OWNER.OWNER || '"."' || LIST_TABLES.SEGMENT_NAME || '" MOVE TABLESPACE ' || V_DEST_TABLESPACE;
EXECUTE IMMEDIATE V_SQL;
END LOOP;
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('MOVING LOBS:');
DBMS_OUTPUT.PUT_LINE('------------------');
FOR LIST_LOBS IN (SELECT TABLE_NAME, COLUMN_NAME FROM DBA_LOBS WHERE OWNER=LIST_OWNER.OWNER AND TABLESPACE_NAME=V_CURRENT_TABLESPACE)
LOOP
DBMS_OUTPUT.PUT_LINE('MOVING LOB FROM TABLE ' || LIST_LOBS.TABLE_NAME || ' TO TABLESPACE ' || V_DEST_TABLESPACE || '...');
V_SQL := 'ALTER TABLE ' || LIST_OWNER.OWNER || '.' || LIST_LOBS.TABLE_NAME || ' MOVE LOB(' || LIST_LOBS.COLUMN_NAME || ') STORE AS (TABLESPACE ' || V_DEST_TABLESPACE || ')';
EXECUTE IMMEDIATE V_SQL;
END LOOP;
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('MOVING INDEXES:');
DBMS_OUTPUT.PUT_LINE('------------------');
FOR LIST_INDEXES IN (SELECT SEGMENT_NAME, SEGMENT_TYPE FROM DBA_SEGMENTS WHERE OWNER=LIST_OWNER.OWNER AND TABLESPACE_NAME='USERS' AND SEGMENT_TYPE='INDEX')
LOOP
DBMS_OUTPUT.PUT_LINE('MOVING ' || LIST_INDEXES.SEGMENT_TYPE || '' || LIST_INDEXES.SEGMENT_NAME || '...');
V_SQL := 'ALTER INDEX ' || LIST_OWNER.OWNER || '."' || LIST_INDEXES.SEGMENT_NAME || '" REBUILD TABLESPACE ' || V_DEST_TABLESPACE;
EXECUTE IMMEDIATE V_SQL;
END LOOP;
DBMS_OUTPUT.PUT_LINE('');
END LOOP;
END;
/