一個項目中需要在oracle數據庫某個用戶下導出1000多個表,導入到測試庫做數據分析測試。很少遇到需要導出這么多表的情況,通常都是按schema導出,或者整庫導出。考慮到expdp中include參數可以附帶查詢語句,本次遇到的需要導出大量具體表的建議思路:
1) 在數據庫中建一個表exptable,把需要導出表的表明插入到該表中;
2)利用expdp導出命令的include參數附帶select語句查詢exptable表,獲得需要導出的表名;
3)開始導出。
過程記錄如下:
1 在數據庫中建立一個導出列表exptable
create table exptable (id number(10),tablename varchar2(20));
2 使用sql*load工具把需要導出的表的名稱輸入到exptable中:
2.1 把需要導出的表名另存為txt文本,id和表名之間用空格隔開,每行以逗號結尾;
2.2 創建ctl文件:
[oracle@jxsbwxdb1 ~]$ cat a.ctl
load data
infile '/home/oracle/exptable.txt'
append into table jxsi.exptable
fields terminated by ',' trailing nullcols
(id,tablename)
2.3 使用命令sqlldr輸入數據
sqlldr jxsi/****** control=a.ctl
SQL*Loader: Release 11.2.0.4.0 - Production on Wed Feb 26 22:39:04 2020
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Control File: a.ctl
Data File: /home/oracle/exptable.txt
Bad File: exptable.bad
Discard File: none specified
(Allow all discards)
Number to load: ALL
Number to skip: 0
Errors allowed: 50
Bind array: 64 rows, maximum of 256000 bytes
Continuation: none specified
Path used: Conventional
Table JXSI.EXPTABLE, loaded from every logical record.
Insert option in effect for this table: APPEND
TRAILING NULLCOLS option in effect
Column Name Position Len Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
ID FIRST * , CHARACTER
TABLENAME NEXT * , CHARACTER
Table JXSI.EXPTABLE:
1056 Rows successfully loaded.
0 Rows not loaded due to data errors.
0 Rows not loaded because all WHEN clauses were failed.
0 Rows not loaded because all fields were null.
Space allocated for bind array: 33024 bytes(64 rows)
Read buffer bytes: 1048576
Total logical records skipped: 0
Total logical records read: 1056
Total logical records rejected: 0
Total logical records discarded: 0
Run began on Wed Feb 26 22:39:04 2020
Run ended on Wed Feb 26 22:39:04 2020
Elapsed time was: 00:00:00.13
CPU time was: 00:00:00.01
查詢數據確認是否正確導入:
SQL> select count(*) from exptable;
COUNT(*)
----------
1056
3.expdp導出語句
expdp system/****** parfile=exptable.par
cat exptable.par
dumpfile=20200226_exptable_%U.dmp
logfile=20200226_exptable.log
schemas=jxsi
directory=DPDATA
PARALLEL=4
COMPRESSION=all
cluster=N
include=TABLE:"IN (select tablename from exptable where tablename is not null)"
或者:
expdp jxsi/****** directory=DPDATA dumpfile=20200226_exptable_%U.dmp schemas=jxsi include=table:\"in \(select tablename from exptable \)\" logfile=20200226_exptable.log cluster=N compression=all PARALLEL=4
Export: Release 11.2.0.4.0 - Production on Wed Feb 26 22:40:52 2020
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
;;;
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
FLASHBACK automatically enabled to preserve database integrity.
Starting "JXSI"."SYS_EXPORT_SCHEMA_02": jxsi/******** directory=DPDATA dumpfile=20200226_exptable_%U.dmp schemas=jxsi include=table:"in (select tablename from exptable )" logfile=20200226_exptable.log cluster=N compression=all PARALLEL=4
Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 220.7 GB
Processing object type SCHEMA_EXPORT/TABLE/PROCACT_INSTANCE
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/COMMENT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/INDEX/FUNCTIONAL_INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/FUNCTIONAL_INDEX/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/INDEX/BITMAP_INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/BITMAP_INDEX/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/TRIGGER
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/POST_TABLE_ACTION
. . exported "JXSI"."AC43":"AC43_20" 1.890 GB 81857509 rows
. . exported "JXSI"."AC43":"AC43_10" 2.110 GB 94683422 rows
......
. . exported "JXSI"."P_RYQY" 4.921 KB 21 rows
. . exported "JXSI"."QTMXB" 10.15 KB 234 rows
Master table "JXSI"."SYS_EXPORT_SCHEMA_02" successfully loaded/unloaded
******************************************************************************
Dump file set for JXSI.SYS_EXPORT_SCHEMA_02 is:
/tmpbak/DPDATA/20200226_exptable_01.dmp
/tmpbak/DPDATA/20200226_exptable_02.dmp
/tmpbak/DPDATA/20200226_exptable_03.dmp
/tmpbak/DPDATA/20200226_exptable_04.dmp
Job "JXSI"."SYS_EXPORT_SCHEMA_02" successfully completed at Wed Feb 26 23:22:23 2020 elapsed 0 00:41:30
正常導出。
