近期客戶有需求,導出某些審計數據,供審計人進行覈查,只能導出成文本或excel格式的進行查看,這裏咱們使用sqluldr2工具進行相關數據的導出。html
oracle導出數據爲文本格式比較麻煩,sqluldr2是靈活的強大的oracle文本導出程序,是以前阿里大拿首席dba樓方鑫開發的小工具,oracle有本身的一個sqlldr工具,是將文本載入到oracle庫中用的,而如今這個sqluldr中間加了個u是un的意思,小工具是用oracle的C語言接口寫成,短小精幹運行速度快,工具很強大,並且批量導出效率很是高,使用靈活,多個平臺版本都有對應的工具。sql
命令參數:數據庫
./sqluldr2 SQL*UnLoader: Fast Oracle Text Unloader (GZIP, Parallel), Release 4.0.1 (@) Copyright Lou Fangxin (AnySQL.net) 2004 - 2010, all rights reserved. License: Free for non-commercial useage, else 100 USD per server. Usage: SQLULDR2 keyword=value [,keyword=value,...] Valid Keywords: user = username/password@tnsname sql = SQL file name query = select statement field = separator string between fields record = separator string between records rows = print progress for every given rows (default, 1000000) file = output file name(default: uldrdata.txt) log = log file name, prefix with + to append mode fast = auto tuning the session level parameters(YES) text = output type (MYSQL, CSV, MYSQLINS, ORACLEINS, FORM, SEARCH). charset = character set name of the target database. ncharset= national character set name of the target database. parfile = read command option from parameter file for field and record, you can use '0x' to specify hex character code, \r=0x0d \n=0x0a |=0x7c ,=0x2c, \t=0x09, :=0x3a, #=0x23, "=0x22 '=0x27
[oracle@m1 ~]$vi query.sql SELECT * FROM ( SELECT A.*, rownum r FROM (select aa.* ,bb.aac003,bb.aac058,bb.aac147 from GIOISX.ac10 aa,GIOISX.ac01 bb where 1=1 and aa.aac001=bb.aac001 and aa.batchid=101 order by aa.aac001,aae003,aae140 ) A WHERE rownum < =30) B WHERE r >0
#./sqluldr2 system/welcoii@cfsgal sql=/home/oracle/query.sql mode=APPEND head=yes file=/home/oracle/jgyl_ad50_%B.csv batch=yes rows=5000 0 rows exported at 2019-06-27 16:21:27, size 0 MB. 30 rows exported at 2019-06-27 16:21:27, size 0 MB. output file /home/oracle/jgyl_ad50_1.csv closed at 30 rows, size 0 MB.
sqluldr2 hr/hr123@127.0.0.1:1521/XE query="select * from bb_user_t" head=yes file=D:\sqluldr2\File\tmp001.csv log=D:\sql.log or
log=+D:\sqluldr2\File\tmp003.log 若是有日誌在日誌後追加日誌,不然覆蓋替換
sqluldr2 hr/hr123@127.0.0.1:1521/XE query="select * from bb_user_t" table=temp_004 head=yes file=D:\sqluldr2\File\tmp004.csv
sqluldr2 system/welc1@sl query="select * from GIOISX.AD50 where rownum<20000" table=GIOISX.AD50 mode=APPEND head=yes file=/home/oracle/jgyl_ad50_%B.csv batch=yes rows=5000 0 rows exported at 2019-06-27 16:08:42, size 0 MB. 5000 rows exported at 2019-06-27 16:08:43, size 0 MB. output file /home/oracle/jgyl_ad50_1.csv closed at 5000 rows, size 1 MB. 5000 rows exported at 2019-06-27 16:08:43, size 1 MB. output file /home/oracle/jgyl_ad50_2.csv closed at 5000 rows, size 2 MB. 5000 rows exported at 2019-06-27 16:08:43, size 2 MB. output file /home/oracle/jgyl_ad50_3.csv closed at 5000 rows, size 3 MB. 4999 rows exported at 2019-06-27 16:08:43, size 4 MB. output file /home/oracle/jgyl_ad50_4.csv closed at 4999 rows, size 4 MB.
參數說明:session
user = username/password@tnsnameoracle
sql = SQL file nameapp
query = select statement (選擇語句;query參數若是整表導出,能夠直接寫表名,若是須要查詢運算和where條件,query=「sql文本」,也能夠把複雜sql寫入到文本中由query調用)工具
field = separator string between fields (post
設置導出文件裏的分隔符;fetch
默認是逗號分隔符,經過 field參數指定分隔符;spa
例如如今要改變默認的字段分隔符,用「#」來分隔記錄,導出的命令以下所示:
sqluldr2 test/test sql=tmp.sql field=#
在指定分隔符時,能夠用字符的ASCII代碼(0xXX,大寫的XX爲16進制的ASCII碼值)來指定一個字符,經常使用的字符的ASCII代碼以下:
回車=0x0d,換行=0x0a,TAB鍵=0x09,|=0x7c,&=0x26,雙引號=0x22,單引號=0x27
在選擇分隔符時,必定不能選擇會在字段值中出現的字符)
record = separator string between records (記錄之間的分隔字符串;分隔符 指定記錄分隔符,默認爲回車換行,Windows下的換行)
rows = print progress for every given rows (default, 1000000)
file = output file name(default: uldrdata.txt) (輸出文件名(默認:uldrdata.txt))
log = log file name, prefix with + to append mode (日誌文件名,前綴加+模式)
fast = auto tuning the session level parameters(YES)
text = output type (MYSQL, CSV, MYSQLINS, ORACLEINS, FORM, SEARCH).
charset = character set name of the target database. (目標數據庫的字符集名稱;導出文件裏有中文顯示亂碼,須要設置參數charset=UTF8)
ncharset= national character set name of the target database.
parfile = read command option from parameter file (從參數文件讀取命令選項;能夠把參數放到parfile文件裏,這個參數對於複雜sql頗有用)
read = set DB_FILE_MULTIBLOCK_READ_COUNT at session level
sort = set SORT_AREA_SIZE at session level (UNIT:MB)
hash = set HASH_AREA_SIZE at session level (UNIT:MB)
array = array fetch size
head = print row header(Yes|No)
batch = save to new file for every rows batch (Yes/No) (爲每行批處理保存新文件)
size = maximum output file piece size (UNIB:MB)
serial = set _serial_direct_read to TRUE at session level
trace = set event 10046 to given level at session level
table = table name in the sqlldr control file (「TABLE」選項用於指定將文件導入的目標表的名字,例如咱們將EMP 表的數據導入到EMP_HIS 表中,假設這兩個表的表結構一致,先用以下命令導出數據:
Sqluldr2 … query=」select * from emp」 file=emp.txt table=emp_his ……)
control = sqlldr control file and path.
mode = sqlldr option, INSERT or APPEND or REPLACE or TRUNCATE
buffer = sqlldr READSIZE and BINDSIZE, default 16 (MB)
long = maximum long field size
width = customized max column width (w1:w2:...)
quote = optional quote string (可選引用字符串;引號符 指定非數字字段先後的引號符)
data = disable real data unload (NO, OFF)
alter = alter session SQLs to be execute before unload
safe = use large buffer to avoid ORA-24345 error (Yes|No) (使用大緩衝器避免ORA-24345錯誤;ORA-24345: A Truncation or null fetch error occurred,設置參數safe=yes)
crypt = encrypted user information only (Yes|No)
sedf/t = enable character translation function
null = replace null with given value
escape = escape character for special characters
escf/t = escape from/to characters list
format = MYSQL: MySQL Insert SQLs, SQL: Insert SQLs.
exec = the command to execute the SQLs.
prehead = column name prefix for head line.
rowpre = row prefix string for each line.
rowsuf = row sufix string for each line.
colsep = separator string between column name and value.
presql = SQL or scripts to be executed before data unload.
postsql = SQL or scripts to be executed after data unload.
lob = extract lob values to single file (FILE).
lobdir = subdirectory count to store lob files .
split = table name for automatically parallelization.
degree = parallelize data copy degree (2-128).
for field and record, you can use '0x' to specify hex character code,
\r=0x0d \n=0x0a |=0x7c ,=0x2c, \t=0x09, :=0x3a, #=0x23, "=0x22 '=0x27</span>
參考樓方鑫: