oracle 数据批量导出工具

#!/bin/ksh
#############################################
#
# 模块: putoutdata.sh
# 作者: XXXXXX
# 描述: 根据一个数据库或表名来卸载数据并生成对应的数据文件
# 文件名为 表名.unl
# 参数 1 = 用户名/密码[@实例名]
# 参数 2 = 表名
# 参数 3 = 查询条件 "省略where"[可选]
# 参数 4 = 文件名前缀且  输出文件名为 前缀_表名.unl[可选]
# 参数 5 = 文件中需要替换的内容
#  有参数5时,必须配合参数4一起使用 参数4 为被替换的内容 参数5 为替换的内容
# 例2.unload user/password rate
#   将 rate 中的全部数据导入文件中,
#     输出文件为 rate .unl
# 例3.unload user/password rate " code='6'"
#   将 rate 中 code= 679的数据导入文件中,
#     输出文件为 rate.unl
# 例4.unload user/password rate " code='6'" 9
#   将 prem_rate 中 code= 6的数据导入文件中,
#     输出文件为 9_rate.unl
# 例5.:unload user/password rate " pol_code='6'" 7 1
#     将 rate 中 code= 6的数据导入文件中,且将 所有6替换为 7
#     输出文件为 1_rate.unl

##########################################

lv_sep="|" # --分隔符,可以修改成自己想要的分隔符,如"|"

##获取记录,拼入unl文件中
f_get_records()
{
    rm -f lv_$table.txt
    SQLS2="select * from $table where 1=1 ${sqlwhere};"
    sqlplus -s ${userid} <<-!!! >/dev/null
        set colsep ${lv_sep};
        set echo off;
        set feedback off;
        set heading off;
        set pagesize 0;
        set linesize 32767;
        set termout off;
        set trimout on;
        set trimspool on;
        spool lv_$table.txt;
        ${SQLS2}
        spool off;
    !!!
    
    
    if [ -f lv_$table.txt ]
    then
    cat lv_$table.txt | grep -v "^SQL>"|sed -e "s/ *$//g"|sed "s/$/|/g"|sed -e "s/ *|/|/g"|sed -e "s/| */|/g" >${newname}${table}.unl
    if [[ `grep "ORA-" ${newname}$table.unl` = "" ]]; then
    echo "unload table ${newname}${table}....\t\t\t\t\t ok"
    else
    cat ${newname}${table}.unl
    err="$err ${newname}${table}"
    fi
    else
    echo $0 error
    fi
    rm -f lv_$table.txt
}

##将拼入unl文件中的特定内容替换为指定的数据
f_fix_records()
{
    if [ -f $table.unl ]
    then
    cat $table.unl | grep ${des} |sed "s/${des}|/${newtext}|/g" >${newtext}_${table}.unl
    if [[ `grep "ORA-" $table.unl` = "" ]]; then
    echo "unload table ${table}....\t\t\t\t\t ok"
    else
    cat ${table}.unl
    err="$err ${table}"
    fi
    else
    echo $0 error
    fi
    rm -f $table.unl
}

## 主程序入口
lv_no=$#

case ${lv_no} in
2)##导出整张表的数据
    userid=$1
    table=$2
    f_get_records;
    ;;
3)##导出条件中的数据
    userid=$1
    table=$2
    sqlwhere=" and $3"
    newname=""
    f_get_records;
;;
4)##导出条件中的数据,并在文件前加前缀
    userid=$1
    table=$2
    sqlwhere=" and $3"
    newname="$4_"
    f_get_records;
    ##将空文件放入一个单独文件夹
    curdir=$pwd
    if [ ! -d "$4" ];
     then
     mkdir $4;
    fi
    for file in `ls $curdir`
    do
            if [ ! -s $file ]
            then
               mv ./$file  ./$4;    
            fi        
    done    
    ;;
5)##导出条件中的数据,将文件中的特定数据替换为指定数据
    userid=$1
    table=$2
    sqlwhere=" and $3"
    des=$4
    newtext=$5
    f_get_records;
    f_fix_records;
    ##将空文件放入一个单独文件夹
    curdir=$pwd
        if [ ! -d "$newtext" ];
        then
            mkdir ${newtext}
        fi
    for file in `ls $curdir`
    do
            if [ ! -s $file ]
            then
               mv ./$file  ./$newtext;    
            fi        
    done
;;
*)
    echo "Usage: $0 "
    exit
    ;;
esac

 

 

#########################################结束操作

if [[ "X$err" = "X" ]];then
echo "Unload Complete!,Thank you!"
else
echo "Unload Table $err error, please check it!"
fi

 

转载于:https://my.oschina.net/u/2822116/blog/693113

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值