2.3 脚本
1. 将表中数据记录导出为字段值用分隔符'|'分开的.dat文件
#!/bin/ksh
##################################################################
## 名称: unloadtable
## 功能: 本 shell 用于将表中数据记录导出
## 导出为字段值用分隔符'|'分开的.dat文件
## 编者:
## 日期: 2006.03.18
##################################################################
if [ $# -ne 3 ]
then
echo "usage:unloadtable tablename username password."
exit 0
fi
##准备工作
echo "set heading off " >/tmp/$1.col
echo "set pagesize 0" >>/tmp/$1.col
echo "set linesize 800 " >>/tmp/$1.col
echo "set feedback off " >>/tmp/$1.col
echo "set tab off " >>/tmp/$1.col
echo "select column_name||',' from user_tab_columns where lower(table_name)='$1' order by
column_id; " >> /tmp/$1.col
##产生 select 语句
echo "set heading off " >/tmp/$1.sel
echo "set pagesize 0" >>/tmp/$1.sel
echo "set linesize 800 " >>/tmp/$1.sel
echo "set feedback off " >>/tmp/$1.sel
echo "set tab off " >>/tmp/$1.sel
echo "select " >>/tmp/$1.sel
echo `sqlplus -s $2/$3 < /tmp/$1.col` |sed "s/,/||'|'||/g" |sed "s/||$//g"|sed "s/date/\"date\"/g"
>>/tmp/$1.sel
##生成 dat文件
#echo "from $1;\n/" >>/tmp/$1.sel 由于 / 导致多执行一次 select
echo "from $1;\n" >>/tmp/$1.sel
sqlplus -s $2/$3 < /tmp/$1.sel >$1_tmp.dat
#awk '{if(FNR!=1) print $0}' $1_tmp.dat >$1.dat FNR 选项使得第一条记录选不出
awk '{print $0}' $1_tmp.dat >$1.dat
rm -f $1_tmp.dat
2. 将数据导入到相应表中
#!/bin/ksh
##################################################################
## 名称:loadtable
## 功能:本 shell 用于将已经准备好的.dat数据文件导入相应的表中
## .dat 文件各个字段值用分隔符'|'分开。
## 编者:
## 日期: 2006.03.18
##################################################################
if [ $# -ne 3 ]
then
echo "usage:loadtable tablename username password."
exit 0
fi
##准备工作
echo "set heading off " >/tmp/$1.colsql
echo "set pagesize 0" >>/tmp/$1.colsql
echo "set linesize 800 " >>/tmp/$1.colsql
echo "set feedback off " >>/tmp/$1.colsql
echo "set tab off " >>/tmp/$1.colsql
echo "select column_name||',' from user_tab_columns where lower(table_name)='$1' order by
column_id; " >> /tmp/$1.colsql
##产生 ctl文件
echo "load data" >/tmp/$1.ctl
echo "infile *" >>/tmp/$1.ctl
echo "into table $1" >>/tmp/$1.ctl
echo "fields terminated by '|'" >>/tmp/$1.ctl
echo `sqlplus -s $2/$3 < /tmp/$1.colsql` |sed "s/,$/)/g" |sed "s/^/(/g" >>/tmp/$1.ctl
##开始导入数据
echo "truncate table $1;" >/tmp/$1.sql
sqlplus $2/$3 < /tmp/$1.sql
sqlldr $2/$3 data=$1.dat control=/tmp/$1.ctl log=/tmp/$1.log