oracle表导出文本,将Oracle数据库中某张表的内容导出成文本格式文件

这是一个用于将数据库中特定表的内容导出为文本格式的shell脚本,每条记录占一行。脚本会根据unload.ctl配置文件进行操作,包括设置分隔符、输出字段、目标目录、条件和排序。导出后,它还会对比导出的记录数和数据库中的实际记录数,确保数据一致性。如果存在CHAR类型的字段包含/n,/r等特殊字符,可能导致记录数不匹配。
摘要由CSDN通过智能技术生成

#!/bin/ksh

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

#功能:

# 将数据库中某张表的内容导出成为一个文本格式,每条记录一行

#配置:

# 参见当前目录下unload.ctl文件

#用法:

# sqlunldr userid/passwd[@connection]  table_name

#注意:

# 如果表中某域为char(n),包含/n,/r等将会造成导出的数据行数比实际记录数多

#

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

getConfig()

{

if [ -f "./unload.ctl" ];then

. ./unload.ctl

echo "Begin reading unload.ctl."

echo =============================

echo "sep=$sep"

echo "fields=$fields"

#`echo $fields|sed -e "s/ //g" `

echo "destDir=$destDir"

echo "where=$where "

echo "order=$order "

echo =============================

echo "Reading unload.ctl complete."

echo ""

return 0

else

return 1

fi

}

getConfig

if [ $? -ne 0 ];then

echo "Error: Can not find unload.ctl file.Please check it!"

exit 1

fi

if [ "X$1" = "X" ];

then

echo "Usage: $0 "

# exit

echo /c "Userid:"

read userid1

echo /c "Passwd:"

echo off

read passwd

userid=$userid1$passwd

echo on

else

userid=$1

fi

if [ "X$2" = "X" ]; then

echo "No table in user $userid."

echo "Usage: $0 "

exit

else

table=$2

fi

#selectSql=`echo "select $fields from $table" |sed -e "s/,/||/',/'||/g"`

#echo selectSql=$selectSql

echo  "Begin to unload... please wait..."

sqlplus $userid </dev/null

set COLSEP $sep;

set echo off;

set heading off ;

set feedback off;

set pagesize 0 ;

set linesize 1000;

set termout off;

set trimout on;

set trimspool on;

spool wk_${table}.txt;

select $fields from $table $where $order;

spool off;

/

exit

/

!

if [ "$?" -ne 0 ] ;

then

echo "Error:sqlplus $userid error in unload table $table!! "

echo "Please check userid and passwd or database."

exit

fi

cat wk_${table}.txt | sed -e '/^SQL>/d' -e '/^$/d' >$table.txt

if [[ `grep "ORA/-" ${table}.txt`  = "" ]];

then

cat $table.txt|sed -e 's/[[:space:]]//g'>wk_${table}.txt

else

cat $table.txt

err="$table"

fi

if [[  "X$err" = "X" ]];then

echo "Unload table $table complete! "

else

echo "Unload table $err error, please check it!"

exit

fi

echo "Check the correctness..."

cnt1=`sqlplus $userid</dev/null

set echo off;

set heading off ;

set feedback off;

set pagesize 0 ;

set termout off;

set trimout on;

set trimspool on;

spool cnt1.txt;

select count(*) from $table $where;

spool off;

exit;

/

<

cnt1=`cat cnt1.txt|sed -e '/^SQL>/d' -e '/^$/d' -e 's/ //g' `

cnt2=`wc -l ${table}.txt |awk '{print($1)}'`

#echo "/$cnt1=$cnt1"

#echo "/$cnt2=$cnt2"

echo "表中记录=$cnt1 条,生成的文件行数=$cnt2 ."

if [ $cnt1 -eq $cnt2 ];

then

echo "Congratulations,unload successful!"

else

echo "Error:导出记录数与表中记录数不等,请检查表中是否有类型为char(n)的某个域的数据含有/n."

fi

mv wk_$table.txt $table.txtrm -f cnt1.txt############****************************************配置文件#file:unload.ctl#分隔符sep=","#输出字段,不能有空格连接;如果有空格需将=右边用""引起fields="ORG_BIND_ACC_NBR,ACCT_MONTH_ID,ACCT_CYCLE_TMPL_ID,ACCT_CYCLE_ID,PARTITION_ID,SOURCE_TYPE,BUSINESS_KEY,CALL_TYPE,ORG_ACC_NBR_A,ORG_ACC_NBR_B,START_DATE,START_TIME,CALL_DURATION,END_DATE,END_TIME,TOTAL_METER_VALUE,PURE_METER_VALUE,SWITCH_AREA_CODE,SWITCH_ID,ORG_FILE_NAME,CDR_POS_IN_FILE,TRUNK_IN,TRUNK_OUT,CARD_NO,CARD_TYPE,IS_PREFEE,ORG_FEE1+ORG_FEE2+ORG_FEE3 ORG_FEE,VISIT_AREA_CODE_A,SWITCH_TYPE,BILLING_TYPE,OPER_SEQUENCE"#条件,没有条件为空where="where ACCT_MONTH_ID>='200410'"#排序字段,没有条件为空order= #"order by start_date"#输出文件存放目录destDir=.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值