Loop through and truncate tables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop through and truncate tables
# 1  
Old 10-25-2006
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...


Code:
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}

# 2  
Old 10-25-2006
try this

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.
# 3  
Old 10-25-2006
Quote:
Originally Posted by Manish Jha
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.
# 4  
Old 10-26-2006
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
# 5  
Old 10-26-2006
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
# 6  
Old 10-26-2006
Quote:
Originally Posted by Manish Jha
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
Code:
for line_number in `cat ${File_Path}/file_name`

with (using ksh/bash)
Code:
for line_number in $(< ${File_Path}/file_name)

OR

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

# 7  
Old 10-26-2006
Put your truncate code in a function ad call it for evry line of the file (not tested) :
Code:
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.

Last edited by aigles; 10-26-2006 at 11:15 AM.. Reason: Correction of while command; Thanks vgersh99.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

truncate a string from the end

hi, guys. I have a question. If I have a long string like this: 8.0K:/home/test/brownj How can I get a substring which starts from the last slash to the end of the string, so in this case, it will be brownj Thank you very much for you time in advance -Keyang (4 Replies)
Discussion started by: daikeyang
4 Replies

2. Shell Programming and Scripting

Truncate table

Hi In unix able to connect to oracle database and create table ,when rerun ,if table exist ,truncate that table.Any idea how to do that a.sh ---- sqlplus -s datadmin/password <<EOF create table xx(col1 number, col2... ); exit; EOF I... (1 Reply)
Discussion started by: mohan705
1 Replies

3. UNIX for Dummies Questions & Answers

How can i truncate filenmes?

I am using FC6 just in case it matters, though i hope it doesn't. If i have a file or some files that i want to truncate the filename of, so that it is only a certain number of characters in length, how would i do that on the command line? Also, just to make it more interesting, say i... (11 Replies)
Discussion started by: Calum
11 Replies

4. UNIX for Advanced & Expert Users

*** Truncate certain field ***

I have a file in which I need to truncate 15th field to have only one character like Put --> P and if i have no value in 15th field, it should be "O" (Other) would really appreciate the reponses, thnx in advance:b: (2 Replies)
Discussion started by: sannmayaz
2 Replies

5. Shell Programming and Scripting

Converting tables of row data into columns of tables

I am trying to transpose tables listed in the format into format. Any help would be greatly appreciated. Input: test_data_1 1 2 90% 4 3 91% 5 4 90% 6 5 90% 9 6 90% test_data_2 3 5 92% 5 4 92% 7 3 93% 9 2 92% 1 1 92% ... Output:... (7 Replies)
Discussion started by: justthisguy
7 Replies

6. Shell Programming and Scripting

Truncate File contain

I have one file which first line is blank and second line has some data. $cat filename output: 30-MAY-07 I want to store 30-MAY-07 value in one variable. for that I wrote var="`head -2 filename`" It will give that result but I want to truncate the first line which is blank. plz help. (2 Replies)
Discussion started by: rinku
2 Replies

7. Shell Programming and Scripting

Truncate directory path

Is it possibe to use sed for the following? I would like to truncate the output of a directory path if it's over 3 directory levels deep. For example: /dir1/dir2/dir3 -- NO change required but, /dir1/dir2/dir3/dir4 would output as ~/dir4 Thanks. (4 Replies)
Discussion started by: here2learn
4 Replies

8. Shell Programming and Scripting

How to truncate as filesize?

Hello everybody it's me again. I have a procces that is writing in a 'file1' automatically but i want to truncate 'file1' to a filesize 'x' that mean if the 'file1' size is 'x' i want to delete the first lines while the last lines are being writed, that have sence? in the process are an... (1 Reply)
Discussion started by: Lestat
1 Replies

9. UNIX for Dummies Questions & Answers

truncate wtmp

I have AIX5.1 I have been trying to learn how to truncate the /var/adm/wtmp file. I have seen several things on google actually but don't quite understand. I also searched your forums but couldn't find it. one says this ">/var/adm/wtmp Is that all I do? I have a seperate question also. I was... (1 Reply)
Discussion started by: rocker40
1 Replies

10. UNIX for Dummies Questions & Answers

Truncate what is It?

what does this command do ? as in does this command just make sure everything in the file is executed? or does it flush the file? Actually this is used on a file in a progress database but I believe it is a unix command? (2 Replies)
Discussion started by: rocker40
2 Replies
Login or Register to Ask a Question