将数据库中的表中的数据导出为csv文本

#!/bin/bash
#========Usage: sh exp.sh 264  Or ./exp.sh 264  import data from records in tables which was inserted in 265 to 264 days before sysdate  ======#

if (($# == 0));then
echo '===========you need to  input an integer parameter ============='
exit 1
fi

echo '===========beginning of exporting data from tables==================='

#prev_date
prev_date=`date -d "-$1 day" +%Y-%m-%d`

end_day=$(($1-1))
start_day=$1

export ORACLE_HOME=/u01/app/11.2.0/db_home1
export LD_LIBRARY_PATH=$ORACLE_HOME/lib

#program path
prog_path=/home/oracle/lxm/imp_exp

#output file base path
output_file_base_path=${prog_path}/file

#log path
output_log_base_path=${prog_path}/logs

#db link string
db_srv="ddjk/czty_ddjk@ddcx"


arr=(t_sagw_callreleasepush_msg t_smb_sub_msg  t_smb_unsub_msg t_sagw_cdrpush_msg)

for var in ${arr[@]};  
do  
        output_file=${output_file_base_path}/${var}/${var}_${prev_date}".txt"
        output_log=${output_log_base_path}/${var}/${var}".log"
        echo $output_file
        if [ ! -f $output_file ];then
                touch $output_file
        fi  
        echo $output_log
        if [ ! -f $output_log ];then
                touch $output_log
        fi  
      # sql="select * from ${var} t where t.time<sysdate-${end_day} and  t.time>=sysdate-${start_day}"
        sql="select * from ${var} t where t.time < to_date(to_char(sysdate,'yyyy/mm/dd'), 'yyyy/mm/dd')-${end_day} and  t.time>=to_date(to_char(sysdate,'yyyy/mm/dd'), 'yyyy/mm/dd')-${start_day}"

        $prog_path/sqluldr2_linux64_10204.bin user=$db_srv query="$sql" file=$output_file field=0x2c record=0x0a log=$output_log charset=ZHS16GBK txt=unl
done
echo '===========end of exporting data from tables==================='

附件列表

     

    posted @ 2017-11-23 10:13  岳麓丹枫  阅读(857)  评论(0编辑  收藏  举报