oracle導(dǎo)出數(shù)據(jù)到文本、從文本導(dǎo)入數(shù)據(jù)的詳細(xì)步驟
經(jīng)常有需求向表中導(dǎo)入大量的數(shù)據(jù),使用insert不靠譜,太慢了,oracle提供了sqlldr的工具
也有時(shí)需要講數(shù)據(jù)導(dǎo)入到文本,oracle的spool可以輕松實(shí)現(xiàn)oracle導(dǎo)出數(shù)據(jù)到txt、txt導(dǎo)入數(shù)據(jù)到oracle
一、導(dǎo)出數(shù)據(jù)到txt
這里用all_objects表做測(cè)試
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做導(dǎo)出、導(dǎo)入測(cè)試
這里需要一些設(shè)置滿足數(shù)據(jù)導(dǎo)出的樣式
vi exp_table.sql
set line 1000 --設(shè)置行的長(zhǎng)度 set pagesize 0 --輸出不換頁(yè) set feedback off --默認(rèn)的當(dāng)一條sql發(fā)出的時(shí)候,oracle會(huì)給一個(gè)反饋,比如說(shuō)創(chuàng)建表的時(shí)候,如果成功命令行會(huì)返回類(lèi)似:Table created的反饋,off后不顯示反饋 set heading off --不顯示表頭信息 set trimspool on --如果trimspool設(shè)置為on,將移除spool文件中的尾部空 set trims on --去掉空字符 set echo off; --顯示start啟動(dòng)的腳本中的每個(gè)sql命令,缺省為on set colsep '|' --設(shè)置分隔符 set termout off --不在屏幕上顯示結(jié)果 spool db1.txt --記錄數(shù)據(jù)到db1.txt select object_id,object_name from all_objects; --導(dǎo)出數(shù)據(jù)語(yǔ)句 spool off --收集完畢 exit
一切就緒后導(dǎo)出數(shù)據(jù)
[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 --可選,去除每行開(kāi)頭部分的空格 [oracle@centos5 ~]$ more db1.txt 20|ICOL$ 44|I_USER1 28|CON$ 15|UNDO$ 29|C_COBJ# 3|I_OBJ# 25|PROXY_ROLE_DATA$
導(dǎo)出后檢查數(shù)據(jù)的記錄數(shù)是否正確
[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 --數(shù)據(jù)正確
二、從txt導(dǎo)入數(shù)據(jù)到oracle
sqlldr是通過(guò)一個(gè)control文件設(shè)定后,從文本導(dǎo)入數(shù)據(jù)
建立一張測(cè)試表
SQL> create table tb_sqlldr (id number,name varchar2(50)); Table created.
建立一個(gè)control文件
vi tb_sqlldr.ctl
load data infile 'db1.txt' --數(shù)據(jù)來(lái)源文本 append into table tb_sqlldr --數(shù)據(jù)導(dǎo)入到表tb_sqldr中,導(dǎo)入方式為追加,如果想覆蓋 fields terminated by "|" --4、字段終止于X'09',是一個(gè)制表符(tab) (id,name) --定義對(duì)應(yīng)的字段名稱(chēng),注意順序
導(dǎo)入數(shù)據(jù)分成四種模式,可以根據(jù)需求選擇:
APPEND // 原先的表有數(shù)據(jù) 就加在后面
INSERT // 裝載空表 如果原先的表有數(shù)據(jù) sqlloader會(huì)停止 默認(rèn)值
REPLACE // 原先的表有數(shù)據(jù) 原先的數(shù)據(jù)會(huì)全部刪除
TRUNCATE // 指定的內(nèi)容和replace的相同 會(huì)用truncate語(yǔ)句刪除現(xiàn)存數(shù)據(jù)
執(zhí)行導(dǎo)入操作
sqlldr userid=test/test control=tb_sqlldr.ctl
差不多5w的數(shù)據(jù)短短2s解決
執(zhí)行導(dǎo)入后驗(yàn)證數(shù)據(jù)
SQL> select count(*) from tb_sqlldr; COUNT(*) ---------- 49988
導(dǎo)入成功
再執(zhí)行一次導(dǎo)入操作,由于設(shè)置為追加
SQL> select count(*) from tb_sqlldr; COUNT(*) ---------- 99976
記錄翻倍
sqlldr還有很多參數(shù)供選擇,比如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.
到此這篇關(guān)于oracle導(dǎo)出數(shù)據(jù)到文本、從文本導(dǎo)入數(shù)據(jù)的詳細(xì)步驟的文章就介紹到這了,更多相關(guān)oracle導(dǎo)出數(shù)據(jù)到文本內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Oracle 細(xì)粒度審計(jì)(FGA)初步認(rèn)識(shí)
細(xì)粒度審計(jì)(FGA),是在Oracle 9i中引入的,能夠記錄SCN號(hào)和行級(jí)的更改以重建舊的數(shù)據(jù),本文將詳細(xì)介紹,需要的朋友可以參考下2012-12-12Oracle定義DES加密解密及MD5加密函數(shù)示例
本節(jié)主要介紹了Oracle中定義DES加密解密及MD5加密函數(shù),感興趣的朋友可以參考下2014-08-08Oracle 當(dāng)前用戶下所有表的記錄總數(shù)
Oracle 數(shù)據(jù)庫(kù)下 查詢當(dāng)前用戶下所有表的記錄總數(shù)2009-07-07oracle執(zhí)行cmd的實(shí)現(xiàn)方法
裝了一個(gè)oracle db11g,于是想試一下網(wǎng)上流傳的在sqlplus中執(zhí)行cmd的一些命令,也不知怎么的,沒(méi)一個(gè)好用的,可能是網(wǎng)上轉(zhuǎn)來(lái)轉(zhuǎn)去的轉(zhuǎn)錯(cuò)了.2009-04-04Oracle中pivot函數(shù)圖文實(shí)例詳解
pivot操作是一種數(shù)據(jù)處理方法,可以將一個(gè)表中的行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù),這種轉(zhuǎn)換對(duì)于表格數(shù)據(jù)的分析和展示非常有用,下面這篇文章主要給大家介紹了關(guān)于Oracle中pivot函數(shù)的相關(guān)資料,需要的朋友可以參考下2023-05-05