Anda di halaman 1dari 3

create or replace PROCEDURE ( i_tbl_name IN VARCHAR2 , o_retun_code OUT NUMBER , o_return_msg OUT VARCHAR2 ) AS l_constraint_name VARCHAR2(30); l_index_name VARCHAR2(30);

l_constraint_type VARCHAR2(30); l_new_constraint VARCHAR2(30); l_count INTEGER; l_exp_count INTEGER; l_comp_col_name VARCHAR2(2000); l_col_name VARCHAR2(30); l_col_exp VARCHAR2(1000); l_exp_fun_name VARCHAR2(1000); l_dummy_char VARCHAR2(1); l_sql_stmt VARCHAR2(4000); l_index_type VARCHAR2(30); l_new_index VARCHAR2(30); CURSOR cur_constraints is select constraint_name from user_constraints where table_name = upper(i_tbl_name) and constraint_type ='P';

"CREATE_INDX"

CURSOR cur_const_details is select column_name from user_constraints join user_cons_columns using (table_name,constraint_name) where table_name = upper(i_tbl_name) and constraint_name = l_constraint_name; CURSOR cur_indexes is select index_name , decode(INDEX_TYPE,'BITMAP',INDEX_TYPE,'DOMAIN',INDEX_TYPE,'F UNCTION-BASED NORMAL',INDEX_TYPE,'FUNCTION-BASED BITMAP',INDEX_TYPE,'') INDEX_TY PE, DECODE(uniqueness,'UNIQUE',uniqueness,'') uniqueness from user_indexes where table_name = upper(i_tbl_name) and index_type not in ('LOB') and index_name not in (select index_name from user_constraints where table_name = upper(i_tbl_name) and constraint_type = 'P' and index_name is not null); CURSOR cur_indx_details is select column_name from user_indexes i, user_ind_columns ic where i.table_name = ic.table_name and i.index_name = ic.index_name and i.table_name = upper(i_tbl_name) and i.index_name=l_index_name; CURSOR cur_indx_expression is select column_expression from user_ind_expressions where index_name = l_index_name and table_name = upper(i_tbl_name); BEGIN

FOR c1 in cur_constraints LOOP l_constraint_type := ' PRIMARY KEY '; l_constraint_name := c1.constraint_name; l_new_constraint := l_constraint_name || '_N'; l_count := 0; l_comp_col_name := ''; FOR c2 in cur_const_details LOOP l_col_name := c2.column_name; IF l_count > 0 THEN l_comp_col_name := l_comp_col_name || ','; END IF; l_comp_col_name := l_comp_col_name || l_col_name; l_count := l_count + 1; END LOOP; BEGIN SELECT INTO FROM WHERE AND 'Y' l_dummy_char USER_CONSTRAINTS CONSTRAINT_NAME = l_new_constraint TABLE_NAME = i_tbl_name || '_NEW';

EXCEPTION WHEN NO_DATA_FOUND THEN l_sql_stmt := 'ALTER TABLE ' || i_tbl_name || '_NEW' || ' ADD (CONSTRAIN T ' || l_new_constraint || l_constraint_type || ' (' || l_comp_co l_name || '))' ; EXECUTE IMMEDIATE l_sql_stmt; END; END LOOP; FOR c1 in cur_indexes LOOP l_index_type := c1.uniqueness || c1.index_type; l_index_name := c1.index_name; l_new_index := c1.index_name || '_N'; l_count := 0; l_exp_count := 0; l_comp_col_name := ''; l_exp_fun_name := ''; FOR c2 in cur_indx_details LOOP l_col_name := c2.column_name; IF l_count > 0 THEN l_comp_col_name := l_comp_col_name || ','; END IF; l_comp_col_name := l_comp_col_name || l_col_name; l_count := l_count + 1; END LOOP;

FOR c3 in cur_indx_expression LOOP l_col_exp := c3.column_expression; IF l_exp_count > 0 THEN l_exp_fun_name := l_exp_fun_name || ','; END IF; l_exp_fun_name := l_exp_fun_name || l_col_exp; l_exp_count := l_exp_count + 1; END LOOP; BEGIN SELECT INTO FROM WHERE AND 'Y' l_dummy_char USER_INDEXES INDEX_NAME = l_new_index TABLE_NAME = i_tbl_name || '_NEW';

EXCEPTION WHEN NO_DATA_FOUND THEN IF l_index_type = 'DOMAIN' AND i_tbl_name = 'MLS_DATA' then l_sql_stmt := 'CREATE INDEX ' || l_new_index ||' ON ' || i_tbl _name || '_NEW' || ' (' || l_comp_col_name || ') ' ||' INDEXTYPE IS MDSYS.SPATIAL_INDEX parameters ('' LAYER_G TYPE=POINT TABLESPACE=tbsi_sdo sdo_rtr_pctfree=15 '') parallel 8' ; ELSIF l_index_type = 'FUNCTION-BASED NORMAL' AND i_tbl_name = 'MLS_DATA' then l_sql_stmt := 'CREATE INDEX ' || l_new_index ||' ON ' || i_tbl _name || '_NEW' || ' (' || l_exp_fun_name || ') parallel 8 ' ; ELSIF l_index_type= 'FUNCTION-BASED BITMAP' AND i_tbl_name = 'MLS_DATA' then l_sql_stmt := 'CREATE BITMAP INDEX ' || l_new_index ||' ON ' || i_tbl_n ame || '_NEW' || ' (' || l_exp_fun_name || ') parallel 8' ; else l_sql_stmt := 'CREATE ' || l_index_type || ' INDEX ' || l_new_index | | ' ON ' || i_tbl_name || '_NEW' || ' ('||l_comp_col_name||') parallel 8' ; END IF; -dbms_output.put_line(l_sql_stmt); EXECUTE IMMEDIATE l_sql_stmt; END; END LOOP; EXCEPTION WHEN OTHERS THEN o_retun_code := 1; o_return_msg := 'Error: (' || TO_CHAR(SQLCODE) || ') ' || SUBSTR(SQLERRM,1,380 0) ||l_new_index ; RETURN; END CREATE_INDX;