Reverse engineering a given index


#!/bin/ksh
#
# Script: regen_inx.sql
# This shell script is used to reverse engineer an Index creation script
# from the data dictionary.
#
# ==============================
# Get Oracle Userid and Password
# ==============================
#
clear
echo "\n\n"
echo "//////////\\\\\\\\\\\"
echo "Script: regen_inx.sh"
echo "\\\\\\\\\\///////////"
echo "\n"
echo "Enter Your Oracle Userid --> ________\b\b\b\b\b\b\b\b\c"
read ORAID
stty -echo
echo "\nEnter Your Oracle Password --> ________\b\b\b\b\b\b\b\b\c"
read ORAPASS
stty echo
echo "\nEnter the Index to Regenerate --> ______________________________\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\c"
read INDEX
echo "\nEnter Index Owner --> _______________\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\c"
read OWNER
echo "\n\nPlease Wait... Generating CREATE INDEX script"
echo "\n"
#
# ====================
# GENERATE 1ST 3 LINES
# ====================
#
sqlplus ${ORAID}/${ORAPASS} << EOF
set echo off
set feedback off
set heading off
col line2 newl
col line3 newl
spool hist_inx.sql
select 'CREATE '|| decode(uniqueness, 'UNIQUE','UNIQUE ',null) ||
'INDEX ' || owner || '.' || index_name line1,
'on ' || table_owner || '.' || table_name line2,
'(' line3
from sys.dba_indexes
where owner = '${OWNER}'
and index_name = '${INDEX}'
/
spool off
EOF
#
# =================
# GET INDEX COLUMNS
# =================
#
sqlplus ${ORAID}/${ORAPASS} << EOF
spool ind_col.lst
select column_name
from sys.dba_ind_columns
where owner = '${OWNER}'
and index_name = '${INDEX}'
order by column_position
/
spool off
EOF
#
IND_COL_LIST=`cat ind_col.lst`
for IND_COL in ${IND_COL_LIST}
do
echo "${IND_COL}" >> hist_inx.sql
done
echo ")" >> hist_inx.sql
#
# ================
# GET STORAGE INFO
# ================
#
sqlplus ${ORAID}/${ORAPASS} << EOF
spool ind_storage.lst
select tablespace_name || ' ' ||
ini_trans || ' ' ||
max_trans || ' ' ||
pct_free || ' ' ||
initial_extent || ' ' ||
next_extent || ' ' ||
min_extents || ' ' ||
max_extents || ' ' ||
pct_increase || ' '
from sys.dba_indexes
where owner = '${OWNER}'
and index_name = '${INDEX}'
/
spool off
EOF
#
TBLSPC=`cat ind_storage.lst | awk '{print $1}'`
INITRANS=`cat ind_storage.lst | awk '{print $2}'`
MAXTRANS=`cat ind_storage.lst | awk '{print $3}'`
PCTFREE=`cat ind_storage.lst | awk '{print $4}'`
INITEXT=`cat ind_storage.lst | awk '{print $5}'`
NEXTEXT=`cat ind_storage.lst | awk '{print $6}'`
MINEXT=`cat ind_storage.lst | awk '{print $7}'`
MAXEXT=`cat ind_storage.lst | awk '{print $8}'`
PCTINCR=`cat ind_storage.lst | awk '{print $9}'`
echo "TABLESPACE ${TBLSPC}" >> hist_inx.sql
echo "INITRANS ${INITRANS}" >> hist_inx.sql
echo "MAXTRANS ${MAXTRANS}" >> hist_inx.sql
echo "PCTFREE ${PCTFREE}" >> hist_inx.sql
echo "STORAGE ( INITIAL ${INITEXT}" >> hist_inx.sql
echo " NEXT ${NEXTEXT}" >> hist_inx.sql
echo " PCTINCREASE ${PCTINCR}" >> hist_inx.sql
echo " MINEXTENTS ${MINEXT}" >> hist_inx.sql
echo " MAXEXTENTS ${MAXEXT} )" >> hist_inx.sql
echo "/" >> hist_inx.sql
#
# =================
# REMOVE TEMP FILES
# =================
#
#rm -f ind_col.lst
#rm -f ind_storage.lst
echo "DONE. See Ya."