在ITPUB上看到有人提出这个问题,能否利用数据泵导出一个查询结果。事实上数据泵还真的具有这个功能。
这一篇介绍数据泵的QUERY功能。
建立一个简单的测试环境:
SQL> CREATE TABLE T1
2 (ID NUMBER, NAME VARCHAR2(30));
Table created.
SQL> INSERT INTO T1
2 SELECT ROWNUM, TNAME
3 FROM TAB;
66 rows created.
SQL> CREATE TABLE T2
2 (ID NUMBER, NAME VARCHAR2(30));
Table created.
SQL> INSERT INTO T2
2 VALUES (1, 'A');
1 row created.
SQL> INSERT INTO T2
2 VALUES (2, 'B');
1 row created.
SQL> COMMIT;
Commit complete.
如果要导出T1和T2的查询结果,且满足两个条件,就可以考虑使用数据泵的QUERY功能。
第一个条件是查询的全部字段都来自同一张表;
第二个条件是其他表关联当前表字段是唯一的,换句话说,就是当前表和其他表关联后不会产生重复的记录。
SQL> SELECT T1.* FROM T1, T2
2 WHERE T1.ID = T2.ID;
ID NAME
---------- ------------------------------
1 CP_TEST_T
2 CELL
比如上述的查询结果就可以通过数据泵的QUERY参数来实现,首先对查询变形,变成IN或EXISTS的形式:
SQL> SELECT * FROM T1
2 WHERE ID IN
3 (SELECT T2.ID FROM T2 WHERE T2.ID = T1.ID);
ID NAME
---------- ------------------------------
1 CP_TEST_T
2 CELL
下面就可以进行导出了:
[oracle@yans1 ~]$ expdp test/test dumpfile=d_output:t1.dp tables=t1 query='T1:"WHERE ID IN (SELECT ID FROM T2 WHERE T2.ID = ID)"'
Export: Release10.2.0.3.0 - 64bit Production on星期日, 13 6月, 2010 19:52:28
Copyright (c) 2003, 2005, Oracle. All rights reserved.
Connected to: Oracle Database10gEnterprise Edition Release10.2.0.3.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
Starting "TEST"."SYS_EXPORT_TABLE_01": test/******** dumpfile=d_output:t1.dp tables=t1 query=T1:"WHERE ID IN (SELECT ID FROM T2 WHERE T2.ID = ID)"
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 128 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
. . exported "TEST"."T1" 5.234 KB 2 rows
Master table "TEST"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for TEST.SYS_EXPORT_TABLE_01 is:
/home/oracle/t1.dp
Job "TEST"."SYS_EXPORT_TABLE_01" successfully completed at 19:52:32
这种方法比较简单,但是最大的缺点就是限制条件太严格,尤其是所有字段完全来自都一张表,大多数情况都无法满足这个条件。
oracle视频教程请关注:http://u.youku.com/user_video/id_UMzAzMjkxMjE2.html
转载于:https://blog.51cto.com/19880614/1221601