oracle導出數據到文本、從文本導入數據的詳細步驟
oracle導出數據到文本、從文本導入數據的詳細步驟
經常有需求向表中導入大量的數據,使用insert不靠譜,太慢了,oracle提供了sqlldr的工具
也有時需要講數據導入到文本,oracle的spool可以輕松實現oracle導出數據到txt、txt導入數據到oracle
一、導出數據到txt
這里用all_objects表做測試
sql> desc all_objects; name null? type ----------------------------------------- -------- ---------------------------- owner not null varchar2(30) object_name not null varchar2(30) subobject_name varchar2(30) object_id not null number data_object_id number object_type varchar2(19) created not null date last_ddl_time not null date timestamp varchar2(19) status varchar2(7) temporary varchar2(1) generated varchar2(1) secondary varchar2(1)
拿object_id,object_name做導出、導入測試
這里需要一些設置滿足數據導出的樣式
viexp_table.sql
set line 1000 --設置行的長度 set pagesize 0 --輸出不換頁 set feedback off --默認的當一條sql發出的時候,oracle會給一個反饋,比如說創建表的時候,如果成功命令行會返回類似:table created的反饋,off后不顯示反饋 set heading off --不顯示表頭信息 set trimspool on --如果trimspool設置為on,將移除spool文件中的尾部空 set trims on --去掉空字符 set echo off; --顯示start啟動的腳本中的每個sql命令,缺省為on set colsep '|' --設置分隔符 set termout off --不在屏幕上顯示結果 spool db1.txt --記錄數據到db1.txt select object_id,object_name from all_objects; --導出數據語句 spool off --收集完畢 exit
一切就緒后導出數據
[oracle@centos5 ~]$ sqlplus test/test @exp_table.sql sql*plus: release 10.2.0.4.0 - production on thu jun 13 16:35:14 2013 copyright (c) 1982, 2007, oracle. all rights reserved. connected to: oracle database 10g enterprise edition release 10.2.0.4.0 - 64bit production with the partitioning, olap, data mining and real application testing options disconnected from oracle database 10g enterprise edition release 10.2.0.4.0 - 64bit production with the partitioning, olap, data mining and real application testing options [oracle@centos5 ~]$ sed -i 's/ //g' db1.txt --可選,去除每行開頭部分的空格 [oracle@centos5 ~]$ more db1.txt 20|icol$ 44|i_user1 28|con$ 15|undo$ 29|c_cobj# 3|i_obj# 25|proxy_role_data$
導出后檢查數據的記錄數是否正確
[oracle@centos5 ~]$ cat db1.txt |wc -l 49988 [oracle@centos5 ~]$ sqlplus test/test sql*plus: release 10.2.0.4.0 - production on thu jun 13 16:36:21 2013 copyright (c) 1982, 2007, oracle. all rights reserved. connected to: oracle database 10g enterprise edition release 10.2.0.4.0 - 64bit production with the partitioning, olap, data mining and real application testing options sql> select count(*) from all_objects; count(*) ---------- 49988 --數據正確
二、從txt導入數據到oracle
sqlldr是通過一個control文件設定后,從文本導入數據
建立一張測試表
sql> create table tb_sqlldr (id number,name varchar2(50)); table created.
建立一個control文件
vi tb_sqlldr.ctl
load data infile 'db1.txt' --數據來源文本 append into table tb_sqlldr --數據導入到表tb_sqldr中,導入方式為追加,如果想覆蓋 fields terminated by "|" --4、字段終止于x'09',是一個制表符(tab) (id,name) --定義對應的字段名稱,注意順序
導入數據分成四種模式,可以根據需求選擇:
append // 原先的表有數據 就加在后面
insert // 裝載空表 如果原先的表有數據 sqlloader會停止 默認值
replace // 原先的表有數據 原先的數據會全部刪除
truncate // 指定的內容和replace的相同 會用truncate語句刪除現存數據
執行導入操作
sqlldr userid=test/test control=tb_sqlldr.ctl
差不多5w的數據短短2s解決
執行導入后驗證數據
sql> select count(*) from tb_sqlldr; count(*) ---------- 49988
導入成功
再執行一次導入操作,由于設置為追加
sql> select count(*) from tb_sqlldr; count(*) ---------- 99976
記錄翻倍
sqlldr還有很多參數供選擇,比如log、bad這些,查看幫助即可
[oracle@centos5 ~]$ sqlldr sql*loader: release 10.2.0.4.0 - production on thu jun 13 17:07:26 2013 copyright (c) 1982, 2007, oracle. all rights reserved. usage: sqlldr keyword=value [,keyword=value,...] valid keywords: userid -- oracle username/password control -- control file name log -- log file name bad -- bad file name data -- data file name discard -- discard file name discardmax -- number of discards to allow (default all) skip -- number of logical records to skip (default 0) load -- number of logical records to load (default all) errors -- number of errors to allow (default 50) rows -- number of rows in conventional path bind array or between direct path data saves (default: conventional path 64, direct path all) bindsize -- size of conventional path bind array in bytes (default 256000) silent -- suppress messages during run (header,feedback,errors,discards,partitions) direct -- use direct path (default false) parfile -- parameter file: name of file that contains parameter specifications parallel -- do parallel load (default false) file -- file to allocate extents from skip_unusable_indexes -- disallow/allow unusable indexes or index partitions (default false) skip_index_maintenance -- do not maintain indexes, mark affected indexes as unusable (default false) commit_discontinued -- commit loaded rows when load is discontinued (default false) readsize -- size of read buffer (default 1048576) external_table -- use external table for load; not_used, generate_only, execute (default not_used) columnarrayrows -- number of rows for direct path column array (default 5000) streamsize -- size of direct path stream buffer in bytes (default 256000) multithreading -- use multithreading in direct path resumable -- enable or disable resumable for current session (default false) resumable_name -- text string to help identify resumable statement resumable_timeout -- wait time (in seconds) for resumable (default 7200) date_cache -- size (in entries) of date conversion cache (default 1000) please note: command-line parameters may be specified either by position or by keywords. an example of the former case is 'sqlldr scott/tiger foo'; an example of the latter is 'sqlldr control=foo userid=scott/tiger'. one may specify parameters by position before but not after parameters specified by keywords. for example, 'sqlldr scott/tiger control=foo logfile=log' is allowed, but 'sqlldr scott/tiger control=foo log' is not, even though the position of the parameter 'log' is correct.
關于oracle導出數據到文本、從文本導入數據的詳細步驟的文章就介紹至此,更多相關oracle導出數據到文本內容請搜索碩編程以前的文章,希望以后支持碩編程!
相關文章