---
title: "自动sqlldr导入脚本"
author: "libin123"
date: "2012-08-31T17:52:51Z"
tags: []
categories: ["操作系统"]
slug: "sqlldr"
draft: false
lastmod: "2012-09-12T15:40:08Z"
id: 1669093
url: "https://dbua.iteye.com/blog/1669093"
---

自己写的通用的sqlldr导入脚本，配置在crontab 里面，每天自动导入数据:

#!/bin/bash  
export ORACLE\_BASE=/home/db/oracle/oracledb  
export ORACLE\_HOME=$ORACLE\_BASE/product/11.2.0/dbhome\_1  
export ORACLE\_SID=BTOG  
export PATH=$ORACLE\_HOME/bin:$PATH  
export NLS\_LANG="SIMPLIFIED CHINESE\_CHINA".AL32UTF8  
username=bimp  
password=bimp  
active\_logfile\_datadir=/opt/b2gbak216/.test/active\_log\_file  
logfile\_prefix\_name=active\_log  
active\_log\_file\_ctl=

insert\_file\_log(){  
local file\_name=$1  
local operator\_code=\`  
sqlplus -S /nolog &lt;&lt;EOF  
conn $username/$password  
set heading off feedback off  
var out\_file\_id number;  
call file\_log\_insert('$file\_name',:out\_file\_id);  
select :out\_file\_id from dual;  
quit  
EOF\`  
echo $operator\_code  
}  
update\_file\_log(){  
local file\_id=$1  
local operate\_status=$2  
local file\_data=$3  
local operator\_code=\`  
sqlplus -S /nolog &lt;&lt;EOF  
conn $username/$password  
set heading off feedback off  
var out\_result\_code number;  
call file\_log\_update($file\_id,'$operate\_status','$file\_data',:out\_result\_code);  
select :out\_result\_code from dual;  
EOF\`  
echo $operator\_code  
}

file\_load(){

local data\_child\_dir=\`ls -t $datadir\`

for data\_dir in $data\_child\_dir;do  
if \[ -d $data\_dir ];then  
all\_data\_file=\`ls $data\_dir\`  
for data\_file in $all\_data\_file;do  
centrol\_file\_load $datadir/$data\_dir/$data\_file $centrol\_ctl $file\_prefix\_name $file\_postfix\_name  
done  
if \[ -z $1 ];then  
exit  
fi  
fi

done  
}  
file\_load\_all(){  
file\_load all  
}

customer\_tran\_file\_load(){  
local datadir=/home/db/oracle/oracledb/flash\_recovery\_area/customer\_tran  
local control\_ctl=/home/db/oracle/oracledb/flash\_recovery\_area/sqlldr/customer\_tran/customer\_tran.ctl  
local file\_prefix\_name=TRAN\_110_  
local file\_postfix\_name=txt

local all\_data\_file=\`ls $datadir|grep "$file\_postfix\_name$"\`  
for data\_file in $all\_data\_file;do  
centrol\_file\_load $datadir/$data\_file $control\_ctl $file\_prefix\_name $file\_postfix\_name  
done  
}

active\_log\_file\_load(){  
all\_data\_file=\`ls $active\_logfile\_datadir\`  
for data\_file in $all\_data\_file;do  
centrol\_file\_load $active\_logfile\_datadir/$data\_file $active\_log\_file\_ctl $logfile\_prefix\_name $file\_postfix\_name  
done  
}  
\# manual load one file,the file path is absolute path  
manual\_file\_load(){  
local data\_file  
local control\_file  
local file\_prefix\_name  
local file\_postfix\_name  
echo -n "please input the data file name(absolute path):"  
read data\_file  
echo -n "please input the control file name:"  
read control\_file  
echo -n "please input the data file prefix name\[]:"  
read file\_prefix\_name  
echo -n "please input the data file postfix name\[]:"  
read file\_postfix\_name  
if \[ -z $file\_prefix\_name ];then  
file\_prefix\_name=all  
fi  
if \[ -z $file\_postfix\_name ];then  
file\_postfix\_name=all  
fi  
centrol\_file\_load $data\_file $control\_file $file\_prefix\_name $file\_postfix\_name  
}  
centrol\_file\_load(){  
if \[ -z $1 ];then  
echo "call centrol\_file\_load function you must input the data file name"  
exit 1  
fi  
if \[ -z $2 ];then  
echo "call centrol\_file\_load function you must input the control file name"  
exit 1  
fi  
if \[ -z $3 ];then  
echo "call centrol\_file\_load function you must input the data file's prefix name"  
exit 1  
fi  
if \[ -z $4 ];then  
echo "call centrol\_file\_load function you must input the data file's postfix name"  
exit 1  
fi  
local l\_data\_file=$1  
local l\_control\_file=$2  
local l\_file\_prefix\_name=$3  
local l\_file\_postfix\_name=$4  
local l\_simple\_data\_file\_name=\`basename $l\_data\_file\`

if \[ -f "$l\_data\_file" ] && \[ -s "$l\_data\_file" ];then  
if \[ ${l\_simple\_data\_file\_name:0:${#l\_file\_prefix\_name}} == $l\_file\_prefix\_name ] || \[ $l\_file\_prefix\_name == "all" ];then  
if \[ ${l\_simple\_data\_file\_name##\*.} == $l\_file\_postfix\_name ] || \[ $l\_file\_postfix\_name == "all" ];then  
#1.insert the datafile to databasea  
v\_file\_id=\`insert\_file\_log $l\_data\_file\`  
if \[ $v\_file\_id != "0" ];then  
if \[ $l\_control\_file != "" ] && \[ -f "$l\_control\_file" ] && \[ -s "$l\_control\_file" ];then  
#2.sqlload read datafile data to database  
load\_data $l\_data\_file $l\_control\_file  
#3.if sqlload the data success,update the file operate\_status from the database  
if \[ -f "$l\_data\_file.bad" ];then  
update\_file\_log $v\_file\_id 0 ${l\_simple\_data\_file\_name:${#l\_file\_prefix\_name}:8}  
else  
update\_file\_log $v\_file\_id 2 ${l\_simple\_data\_file\_name:${#l\_file\_prefix\_name}:8}  
tar -czf $l\_data\_file.tar.gz $l\_data\_file  
mv $l\_data\_file.tar.gz /home/db/oracle/oracledb/flash\_recovery\_area/data\_archive/  
rm -fr $l\_data\_file  
fi  
else  
update\_file\_log $v\_file\_id 0 ${l\_simple\_data\_file\_name:${#l\_file\_prefix\_name}:8}  
echo "file load errors,because the the control file is not exists or control file is empty or control file errors"  
fi  
else  
echo "file load errors,because the file name $l\_data\_file have be in the database or database error"  
fi  
else  
echo "data file load errors,the data file postfix name ${l\_simple\_data\_file\_name:0:${#l\_file\_prefix\_name}} and l\_file\_postfix\_name param value is $l\_file\_postfix\_name not equal"  
fi  
else  
echo "data file load errors,the data file prefix name ${l\_simple\_data\_file\_name##\*.} and l\_file\_prefix\_name param value is $l\_file\_postfix\_name not equal"  
fi  
else  
echo "data file load errors,because the data file $l\_data\_file is not exists or data file is empty"  
fi  
}  
load\_data(){  
if \[ -z $1 ];then  
echo "call load\_data function you must input the data file name"  
exit 1  
fi  
if \[ -z $2 ];then  
echo "the control\_file param is need"  
exit 1  
fi  
if \[ -f $1 ] && \[ -f $2 ];then  
local file\_name=$1  
local control\_file=$2  
sqlldr $username/$password CONTROL="${control\_file}" DATA="${file\_name}" BAD="${file\_name}.bad" DISCARD="${file\_name}.disc" LOG="${file\_name}.log" ERRORS=100 DIRECT=true SKIP=1 rows=10000  
else  
echo "the data\_file $file\_name or control\_file $control\_file is not exists"  
fi  
}

case "$1" in  
customer\_tran\_file\_load)  
customer\_tran\_file\_load  
;;  
active\_log\_file\_load)  
active\_log\_file\_load  
;;  
manual\_file\_load)  
manual\_file\_load  
;;  
insert\_file\_log)  
insert\_file\_log  
;;  
update\_file\_log)  
update\_file\_log  
;;  
load\_data)  
load\_data  
;;  
\*)  
echo $"Usage: $0 {customer\_tran\_file\_load|active\_log\_file\_load|insert\_file\_log|update\_file\_log|load\_data|active\_log\_file\_load|manual\_file\_load}"  
exit 1  
;;  
esac  
exit 0