一、sqlldr導入txt
1.預備
a).txt文件
這里要保存成無簽名的UTF-8
b).oracle建表
2.編寫控制文件input_test.ctl
LOAD DATA
CHARACTERSET 'UTF8' --字符集設定
INFILE 'd:\input_test.txt' --要導入的文本數據路徑,可寫多個
REPLACE into TABLE input_test --清空原有數據再導入方式 追加導入 用append into table t_name
fields terminated by X'09' --以制表符分隔
trailing nullcols --允許空列導入
(col1,col2)
當col1為日期類型時,在控制文件中的字段里把col1改成
col1 "to_date(:col1,'''yyyy-mm-dd hh24:mi:ss''')"即可解決。
注:
infile 'd:\input_test.txt'表示需要裝載的數據文件的路徑
append into table test 數據載入的表:
(1)append 表示表中有數據,加在后面
(2)INSERT 表示裝入空表,有數據則停止。默認值
(3)REPLACE 原先表中如果有數據,會被刪除
(4)TRUNCATE 如果要載入的數據與現在的數據相同,載入的數據替換現存的數據。
fields terminated by ',‘
表示數據用是','分隔的,用by X'09',即16進制的"09"代表TAB制表符,常用於excel轉換的tab制表符文件的數據的導入。常用分隔符還有'|'
多語種可設置字符集編碼為:CHARACTERSET 'UTF8'
3.DOS下執行
sqlldr system/psw@db, control=c:\input\input_test.ctl log=c:\input\input_test.log bad=c:\input\input_test.bad
有效的關鍵字:
userid -- ORACLE username/password
@db -- 鏈接適配器名稱(Net Manager配置的鏈接名稱)
control – 控制文件
log – 記錄的日志文件
bad – 壞數據文件
data – 數據文件
discard – 丟棄的數據文件
discardmax – 允許丟棄數據的最大值 (全部默認)
skip -- Number of logical records to skip (默認0)
load -- Number of logical records to load (全部默認)
errors – 允許的錯誤記錄數 (默認50)
rows -- Number of rows in conventional path bind array or between direct path data saves(每次提交的記錄數,默認: 常規路徑 64, 所有直接路徑)
bindsize -- Size of conventional path bind array in bytes(默認256000)
每次提交記錄的緩沖區的大小(字節為單位,默認256000)
silent --禁止輸出信息 (header,feedback,errors,discards,partitions)
direct – 使用直通路徑方式導入 (默認FALSE)
parfile -- parameter file: name of file that contains parameter specifications
parallel -- 並行導入 (默認FALSE)
file -- File to allocate extents from
skip_unusable_indexes -- disallow/allow unusable indexes or index partitions(默認FALSE)
skip_index_maintenance -- do not maintain indexes, mark affected indexes as unusable(默認FALSE)
readsize -- Size of Read buffer (默認1048576)
與bindsize成對使用,其中較小者會自動調整到較大者。sqlldr先計算單條記錄長度,乘以rows,如小於bindsize,不會試圖擴張rows以填充bindsize;如超出,則以bindsize為准。
external_table -- use external table for load; NOT_USED, GENERATE_ONLY, EXECUTE(默認NOT_USED)
columnarrayrows -- Number of rows for direct path column array(默認5000)
streamsize -- Size of direct path stream buffer in bytes(默認256000)
multithreading -- use multithreading in direct path
resumable -- enable or disable resumable for current session(默認FALSE)
resumable_name -- text string to help identify resumable statement
resumable_timeout -- wait time (in seconds) for RESUMABLE(默認7200)
date_cache -- size (in entries) of date conversion cache(默認1000)
4.寫成.bat批處理
上述3步已完成了txt導入,在windows下還可將sqlldr命令寫成批處理文件,雙擊執行.
@echo off
echo input_test
pause --暫停,建議加入,以免錯誤雙擊執行
@rem
sqlldr system/psw@db, control=c:\input\input_test.ctl log=c:\input\input_test.log bad=c:\input\input_test.bad
@rem
@rem sqlldr system/psw@db, control=c:\input\input_test.ctl rows=100000
pause
二、sqlldr導出txt
利用spool 導出txt
1.寫output.sql
sqlplus system/psw@db as sysdba --連接oracle
CHARACTERSET al32UTF8 --設置編碼集
set trimspool on --打開池
spool c:\output\output.txt --池輸出路徑
set pagesize 0 --頁設置
set heading off --關閉表頭
set linesize 32767 --最大行顯
select '#'||col1||'#,#'||col2||'#,#'||col3||'#‘ --設置需要的列格式。此例,列間以以逗號分隔,列內容用#引起。
from test_data;
exit; --退出sqlplus
spool off --關閉池
pause
注:上述命令在dos下直接敲命令執行亦可。
2.寫成.bat批處理
再寫bat調用上面的outputsql文件,如下:
cd/
set NLS_LANG=.AL32UTF8 --設置字符集utf8
chcp 65001 --轉換編碼頁utf8
@echo off
echo data output
pause
@rem
sqlplus system/psw@db as sysdba @c:\dmp_sql\output.sql
@rem
pause
ctl文件的例子:
OPTIONS(skip_index_maintenance=TRUE,direct=true,BINDSIZE=20971520,READSIZE=20971520,ERRORS=-1,ROWS=500000)
--unrecoverable
LOAD DATA
--CHARACTERSET AL32UTF8
INFILE 'c:\input\Barcode.txt' ---------數據文件,即txt文件
Append INTO TABLE Demoaa.TMS_BRANCHCODE -----表名
FIELDS TERMINATED BY X'09' ------數據用制表符分割
trailing nullcols
(ID ------表中字段
,Branch_plant
,SO_Number
,Trip_Number
,Shippment_Date "to_date(:Shippment_Date,'''yyyy-mm-dd hh24:mi:ss''')"
,Sold_to
,Sold_to_Name
,Ship_to
,Ship_to_Name
,BarCode_Info
,Barcode_Seg_1
,Barcode_Seg_2
,Barcode_Seg_3
,Barcode_Seg_4
,Barcode_Seg_5
,Last_updated_time "to_date(:Last_updated_time,'''yyyy-mm-dd hh24:mi:ss''')"
)