Loop through and truncate tables

Hi,

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

--Manish Jha.

  1. why exactly do you need this "`cat ${File_Path}/file_name`" ? why 'cat'?
  2. once you read the line from the file into a valiable 'line_number', how do you use it (variable)?
  3. next time you post code, pls do use vBcodes for ease of reading.

Vgersh,

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.

Thank You,
Madhu

I guess I missed one thing.

TRUNCATE TABLE ${line_number};

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.

Hope this answers your question !

--Manish

the use of 'cat' is UUOC

substitute

for line_number in `cat ${File_Path}/file_name`

with (using ksh/bash)

for line_number in $(< ${File_Path}/file_name)

OR

while read line_number
do
..........
..........
done < "${File_Path}/file_name"

Put your truncate code in a function ad call it for evry line of the file (not tested) :

FILE_NAME=`basename $0 .ksh`

SPOOL_FILE=${INBOUND}/$FILE_NAME.spool
LOG_FILE=${INBOUND}/$FILE_NAME.log

#======================================================
# Function: truncate_table
#=======================================================

truncate_table()
{
   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
   return ${SQL_STATUS}

}

#======================================================
# Main code
#======================================================

#| 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 FILE LIST>"
   echo " "
   exit 1
fi
#------------------------------------------------------
#       Initialize variables
#------------------------------------------------------
TGT_USERID=$1
TGT_PASSWD=$2
TGT_DSN=$3
LIST_FILE=$4

#------------------------------------------------------
# Truncate loop
#------------------------------------------------------

TRUNC_STATUS=0

while read TABLE_NAME
do
   truncate_table || TRUNC_STATUS=1
done < ${LIST_FILE}

exit ${TRUNC_STATUS}

Jean-Pierre.

aigles,
a lil typo:

while read TABLE_NAME

I still wonder what the typo is...

The code provided by Aigles seem to be correct to me...

Please advice.

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.

There was an extra '$; in front of 'TABLE_NAME' - aigles's just corrected it.