I was working on the truncate oracle table shell script. The script is supplied with one table name as a parameter and I would like to change it so that it is passed a file name with a list of tables. And the script needs to truncate tables one by one by looping through the file. In other words, pass the file name with list of tables as a parameter rather than one table name.
Would some body please suggest how to incorporate that in the current script...Any ideas are highly appreciated!
Thank you in advance...
FILE_NAME=`basename $0 .ksh`
SPOOL_FILE=${INBOUND}/$FILE_NAME.spool
LOG_FILE=${INBOUND}/$FILE_NAME.log
#| check for correct number of parameters
if [ $# -ne 4 ]
then
echo " "
echo " Incorrect number of parameters entered..."
echo " Correct usage: " $0 "<TGT USERID> <TGT PASSWD> <TGT DSN> <TABLE NAME>"
echo " "
exit 1
fi
#------------------------------------------------------
# Initialize variables
#------------------------------------------------------
TGT_USERID=$1
TGT_PASSWD=$2
TGT_DSN=$3
TABLE_NAME=$4
DT_START=`date`
echo "-----------------------------------------" >> $LOG_FILE
echo "Execution Started at :"$DT_START>>$LOG_FILE
echo "Connecting to Oracle...">>$LOG_FILE
tab=`$ORACLE_HOME/bin/sqlplus -s /nolog <<EOF >>$LOG_FILE
CONNECT ${TGT_USERID}/${TGT_PASSWD}@${TGT_DSN}
set lines 250
set trims off
set wrap off
set head off
set pages 0
spool $SPOOL_FILE
WHENEVER SQLERROR EXIT SQL.SQLCODE;
TRUNCATE TABLE ${TABLE_NAME};
spool off;
exit
EOF`
SQL_STATUS=$?
if [ ${SQL_STATUS} -ne 0 ]
then
echo "Error in the SQL...." >>${LOG_FILE}
else
echo "Successfully truncated ${TABLE_NAME} table..." >>${LOG_FILE}
fi
DT_END=`date`
echo "Execution Ended at :"$DT_END>>$LOG_FILE
echo "--------------------------------------------">>$LOG_FILE
exit ${SQL_STATUS}
for line_number in `cat ${File_Path}/file_name`
do
## Reads each line from file_name( Table Name) and truncate that table in database
return=`${ORACLE_HOME}/bin/sqlplus -s ${TGT_USERID}/${TGT_PASSWD} <<END
set pagesize 0 feedback off verify off heading off echo off
WHENEVER SQLERROR EXIT SQL.SQLCODE;
TRUNCATE TABLE ${TABLE_NAME};
EXIT;
END`
if [[ $(echo $return | grep 'ORA-' | wc -l) != 0 ]]
then
echo "Oracle errors occurred."
exit -1
fi
done
resultcode=$?
if [[ $resultcode != 0 ]] then
echo "Error"
exit -1
fi
I also looked at Manish's code and the variable that is declared in the beginning is not being used anywhere in the code. You had also mentioned that it is not required to do a cat on the file name. Is there a better way to do it? Please advice.
cat ${File_Path}/file_name is for reading the file in which table names are stored and for loop is to read those name line by line. line_number would hold the value of the table name. So finally the code would read the table names from file and truncate each table in database one by one.
But the only thing is script returns successful even though it failed to truncate one of the tables. Our Oracle database was just down when I executed and the script still executed as successful.