ORACLE資料匯入Shell程式

2021-04-02 10:01:42 字數 2757 閱讀 6642

#!/usr/bin/ksh

#oracle資料載入shell

if [ $# -ne 3 ]                   #如果引數不等於3個

then

echo

"load.ori user/passwd table_name data_file"

exit

fiif [ ! -f $3

]                    #如果第三個引數不是檔案

then

echo

"$3 : file no found"

exit

fisqlplus $1 < >/dev/null         #使用desc把錶結構資訊寫到/tmp/tmp_table.lst中

spool /tmp/temp_table.lst;

desc

$2spool off;

exit

!if [ "$?" -ne 0 ]                 #如果執行後不等於0,說明出錯

then

echo "error:sqlplus $1

error in generate control file for table $2

!"echo "please check userid and passwd or oracle_sid."

exit

fiif [ -f /tmp/temp_table.lst ]     #如果/tmp/temp_table.lst是乙個檔案

then

lv_line_num=`cat /tmp/temp_table.lst | wc -l`    #得到檔案行數

lv_line_num=`expr $ - 2`             #減去2行

#從起始到lv_line_num行的內容定向到另一檔案(去掉末尾2行內容)

head -$ /tmp/temp_table.lst >/tmp/temp_table        

lv_line_num=`expr $ - 3`             #減去3行

#從末尾到lv_line_num行的內容定向到另一檔案(去掉開頭3行內容)

tail -$ /tmp/temp_table >/tmp/temp_table.lst 

#使用awk命令(列印第乙個引數),讀取檔案中的第一列定向到另一檔案,得到表字段

cat /tmp/temp_table.lst|awk '' >/tmp/temp_table

#組合匯入命令,以"|"分開檔案中的每列內容

lv_str="load data infile '$3

' badfile 'bad_$2

.bad' truncate into table $2

fields terminated by /"|/""

echo $

echo $ > /tmp/temp_table.lst     #把此段命令定向到一檔案

echo "("      >> /tmp/temp_table.lst     #此段命令後面換行加上乙個"("

ed /tmp/temp_table < >/dev/null        #編輯字段檔案

$                                       #到最後一行

1,.s/$/,/                                #從第一行到當前行,把末尾($)替換成(,)

w                                        #寫入到檔案中

$                                        #到最後一行

.,.s/,$//                                #把最後的(,)替換成空,去掉最後這個(,)

w                                        #寫入到檔案中q!

cat /tmp/temp_table>>/tmp/temp_table.lst #追加到剛才那段命令語句後面

echo ")"      >> /tmp/temp_table.lst     #加上")"

else

echo "$0

error :not find table describe file."

exit

filv_rows=10000

lv_bindsize=8192000

lv_readsize=8192000

#使用sqlldr執行上面組合好的語句,匯入資料

sqlldr

$1 control=/tmp/temp_table.lst rows=10000 bindsize=8192000 readsize=8192000 log=/tmp/$

.log bad=/tmp/$

.bad direct=true

if [ "$?" != "0" ]

then

echo "load failed."

else

echo "load success."

fi#上述control內容語句舉例為

load data infile 'table.unl' badfile 'bad_tabel.bad' truncate into table table fields terminated by /"|/"

(fields1,

fields2,

fields3

)使用sqlldr就可以匯入資料庫,內容檔案以"|"來區分各欄位

Oracle匯入資料

oracle匯入資料 在sql plus中 1.c test.sql 2.imp 3.impdp server端 a.conn sys sys as sysdba b.creat directory dumpin idr as d dump c.grant read,write on directo...

oracle匯入資料

create the user create user user identified by 123456 default tablespace users temporary tablespace temp profile default password expire grant revoke ...

oracle匯入資料

oracle匯入資料時出現的問題 alter user spowner default tablespace users ora 00959 表空間 tbs arch risk 不存在 create tablespace tbs arch risk datafile c oracle product...