by http://webgeektutorials.blogspot.com

Monday, May 14, 2012

Oracle DBA Script : Export table data into text file

Creating a ascii text file of oracle table data with pipe delimited columns.

Code:
#!/bin/ksh
#First line in the .txt file is column names
USR=username/password
echo "set feedback off\n set pagesize 0\n
select 'XXTT' || table_name from user_tables ;" | sqlplus -s $USR | grep '^XXTT' | cut -c5- |
while read TABL
do
STR="nothing"
STR1="nothing"
echo "desc $TABL\n" | sqlplus -s $USR | tail +3 | grep "^[A-Z,a-z]" | awk '{print $1}' |
while read COLUM
do
if [ "$STR" = "nothing" ] then
STR=$COLUM
STR1=$COLUM
else
STR="$STR || '|' || $COLUM"
STR1="$STR1|$COLUM"
fi

Done
echo "$STR1" > $TABL.txt
echo "set feedback off\nset pagesize 0\nset linesize 2000\nselect $STR from $TABL;"| sqlplus -s $USR >> $TABL.txt
Done

1 comment:

Anonymous said...

I fail to understand where do i substitute the table name. For example if my table is called "filter" where do I write it in the script.

Post a Comment