Skip to content

Commit b9af27f

Browse files
committed
Use to rebuild all the indexes on a given table
Gathers all the indexes on a given table and rebuilds them in another tablespace
1 parent 82c5faa commit b9af27f

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Diff for: rebuild_indexes.sql

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
REM Script Name : rebuild_indexes.sql
2+
REM Author : Craig Richards
3+
REM Created : 20 May 2013
4+
REM Last Modified :
5+
REM Version : 1.0
6+
REM
7+
REM Modifications :
8+
REM
9+
REM Description : This procedure generates and runs the codes to rebuild the indexes for a given table
10+
11+
CREATE OR REPLACE PROCEDURE rebuild_indexes (p_owner IN VARCHAR2:=NULL, p_table_name IN VARCHAR2:=NULL, p_tblspc IN VARCHAR2:=NULL)
12+
AS
13+
14+
-- Variable Declaration
15+
16+
ERROR EXCEPTION;
17+
rebuild_indexes VARCHAR2(2000);
18+
19+
BEGIN
20+
21+
-- Check the inputs exist
22+
23+
IF p_owner IS NULL OR NVL(length(trim(p_owner)),0)=0 THEN raise error;
24+
ELSIF p_table_name IS NULL OR NVL(length(trim(p_table_name)),0)=0 THEN raise error;
25+
ELSIF p_tblspc IS NULL OR NVL(length(trim(p_tblspc)),0)=0 THEN raise error;
26+
END IF;
27+
28+
-- Check the Tablespace
29+
30+
IF p_tblspc = 'REORG' THEN
31+
tp_resize_reorg(2000);
32+
END IF;
33+
34+
DBMS_OUTPUT.PUT_LINE(' The Commands that were executed wereis :' || CHR(10));
35+
36+
FOR indexes_to_rebuild IN
37+
(SELECT index_name FROM dba_indexes WHERE owner = p_owner AND table_name = p_table_name)
38+
LOOP
39+
rebuild_indexes := 'ALTER INDEX ' ||p_owner||'.'||indexes_to_rebuild.index_name|| ' rebuild tablespace '|| p_tblspc;
40+
DBMS_OUTPUT.PUT_LINE(rebuild_indexes);
41+
EXECUTE IMMEDIATE rebuild_indexes;
42+
END LOOP;
43+
44+
EXCEPTION
45+
WHEN error THEN
46+
DBMS_OUTPUT.PUT_LINE (CHR(10) || 'ORA-77777 : You need to pass the OWNER, TABLE NAME, TABLESPACE TO REBUILD TO');
47+
DBMS_OUTPUT.PUT_LINE ('ie exec tp_rebuild_indexes(''SCOTT'',''EMP'',''REORG'');');
48+
END tp_rebuild_indexes;
49+
/
50+
51+

0 commit comments

Comments
 (0)