1. 程式人生 > >expdp和impdp的應用-高版本通過dblink匯入到低版本

expdp和impdp的應用-高版本通過dblink匯入到低版本

今天接到要進行資料庫使用者的部分資料遷移需求,需求如下

IMP、WO、INSA開頭的表只要結構,不要資料

B、TEMP、TMP開頭的表不用導

其他表需要匯出資料和表結構,同時要求匯出此使用者下的所有其他物件,如:procedure,sequence等

源資料庫在遠端主機內,我想到的方法是先通過dblink匯入到本地 再將dmp檔案傳輸到

1、本地資料庫和目標資料庫的版本和字符集

檢視版本:select * from v$version;  

本地-Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

目標:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod

2 、在本地資料庫建立database link 

create database link dblink connect to inas identified by inas using orcl01;

3、建立directory--我這裡用本地的system使用者匯出

--我這裡用本地的system使用者匯出

sqlplus / as sysdba

create directory dir as 'F:';

grant all on directory dir to system;

建立目標資料庫使用者--已有的話就不用建立

create user inas_30 identified by inas_30 default tablespace inas temporary tablespace temp;

grant connect,resource,unlimited tablespace to inas_30;

create directory dir as 'F:';

grant all on directory dir to inas30_73;

4、篩選需要匯出資料的表

---篩選表
select a.TABLE_NAME,a.TABLESPACE_NAME from user_tables a
minus
select b.TABLE_NAME,b.TABLESPACE_NAME from user_tables b where b.TABLE_NAME like 'IMP%'
minus
select c.TABLE_NAME,c.TABLESPACE_NAME from user_tables c where c.TABLE_NAME like 'WO%'
minus
select d.TABLE_NAME,d.TABLESPACE_NAME from user_tables d where d.TABLE_NAME like 'INSA%'
minus
select e.TABLE_NAME,e.TABLESPACE_NAME from user_tables e where e.TABLE_NAME like 'B%'
minus
select f.TABLE_NAME,f.TABLESPACE_NAME from user_tables f where f.TABLE_NAME like 'TEMP%'
minus
select g.TABLE_NAME,g.TABLESPACE_NAME from user_tables g where g.TABLE_NAME like 'TMP%'
minus
select h.TABLE_NAME,h.TABLESPACE_NAME from user_tables h where regexp_like (h.TABLE_NAME,'[[:digit:]]+$')
minus
select h.TABLE_NAME,h.TABLESPACE_NAME from user_tables h where regexp_like (h.TABLE_NAME,'[_][J]{0,2}$')

5、匯出和匯入

---匯出(只匯入此使用者下的所有物件的資料結構)

expdp system/[email protected] dumpfile=inas30_1112.dmp logfile=inas30_1112.log directory=dir schemas=inas30 network_link=dblink content=metadata_only version=10.2.0.1.0

---匯入

impdp inas_30/inas_30 dumpfile=inas30_1112.dmp logfile=inas30_1112.log directory=dir remap_schema=inas30:inas_30 table_exists_action=replace

---6、刪除不需要的表

declare
cursor cur_ddl is select e.TABLE_NAME,e.TABLESPACE_NAME from user_tables e where e.TABLE_NAME like 'B%'
union
select f.TABLE_NAME,f.TABLESPACE_NAME from user_tables f where f.TABLE_NAME like 'TEMP%'
union
select g.TABLE_NAME,g.TABLESPACE_NAME from user_tables g where g.TABLE_NAME like 'TMP%';

begin
for cur_d in cur_ddl loop
execute immediate 'drop table '||cur_d.TABLE_NAME||'';
end loop;
end;

-7、-匯出需要資料的表 這裡因為是用了dblink 所以必須為表名備註源資料庫的使用者名稱 如:inas30.CHK_CMD_CONTENT

匯出指令碼:version=10.2.0.1.0 是目標資料庫的版本號,content=data_only代表只匯出資料,如果是metadata_only 代表只要表結構
expdp system/[email protected] dumpfile=inas30_1112_tab.dmp logfile=inas30_1112_tab.log directory=dir network_link=dblink73 content=data_only tables=(inas30.ABI_ABIAPPLY_RULE_DIMENSION,inas30.ABI_ABILITY,inas30.ABI_ABILITYGROUP_APPLY_RULE,inas30.ABI_ABILITY_APPLY_RULE,inas30.ABI_ABILITY_CLASSIFICATION,inas30.ABI_ABILITY_EXEC_ABNORMITY,inas30.ABI_ABILITY_EXEC_ABNO_RULE,inas30.ABI_ABILITY_EXEC_ABNO_SYN_RULE,inas30.ABI_ABILITY_NEBSSERVICE_RELA,inas30.ABI_ABILITY_PARAM_RELA,inas30.ABI_ABILITY_PARAM_RELA_BAK,inas30.ABI_ABILITY_RELA,inas30.ABI_ABILITY_RELA_GROUP,inas30.ABI_ABILITY_RELA_GROUPDEF,inas30.ABI_NEBS_PLATFORM,inas30.ABI_NEBS_PROTOCOL,inas30.ABI_NEBS_SERVICE,inas30.ABI_NEBS_SERVICE_CATALOG,inas30.ABI_NEBS_SERVICE_PLATFORM_MAP,inas30.ABI_PROTOCOL_PARAM,inas30.ABI_WO_INFO_COMPONENTS,inas30.CHK_COMMAND_EQUALS,inas30.CHK_CWON_AUTOFINISH_FAILURE,inas30.CHK_PARAM_NVALEQU_IGNORE,inas30.CHK_WORKORDER,inas30.INAS_FIX_PARAM,inas30.INAS_FIX_TEMPLATE,inas30.INAS_PROTOCOL_PARAM,inas30.INAS_SC_PARAM,inas30.INAS_SERVICE_COMMAND,inas30.INAS_SERVICE_PLATFORM,inas30.INAS_WO_MACRO,inas30.INAS_WO_MACRO_ANALYSIS,inas30.INAS_WO_MACRO_ANALYSIS_F,inas30.INAS_WO_MACRO_CARD_RES,inas30.INAS_WO_MACRO_KZXKFIX,inas30.INAS_WO_PARAM,inas30.INAS_WO_XPATH,inas30.INS_ACTION,inas30.INS_CRM_EVENT,inas30.INS_EVENT,inas30.INS_EVENT_MAP,inas30.INS_OFFERS,inas30.INS_PRODUCT,inas30.INS_PRODUCT_MAP,inas30.INS_PROD_CLASSIFICATION,inas30.INS_PROD_PROPERTY,inas30.INS_PROD_PROP_VALUE,inas30.INS_PROD_PROP_VALUE_REL,inas30.INS_PROD_RESOURCE,inas30.INS_PROD_RES_REL,inas30.INS_PROPERTY_MAP,inas30.INS_SERVICE,inas30.INS_SERVICE_MAP,inas30.INT_INTERFACE,inas30.INT_ITM_RECORD,inas30.LTE_VC_ABILITY_MAP,inas30.SCV_ABI_PRIORITY_BASE,inas30.SCV_WO_PRIORITY_BASE,inas30.SC_ABILITY_ABTY_SOLVE_MODE,inas30.SC_ABILITY_ACTGROUP_RULE,inas30.SC_ABILITY_RELA_STATE,inas30.SC_ABILITY_RELA_TYPE,inas30.SC_ABILITY_TYPE,inas30.SC_BUSI_AREA,inas30.SC_CATALOG,inas30.SC_CWO_NEBSVR_STATE,inas30.SC_DISPATCH_ABNORMITY_STATE,inas30.SC_EXEC_TYPE,inas30.SC_FOREIGN_WO_INFO,inas30.SC_FOREIGN_WO_STATE,inas30.SC_IWO_BIL_ACT_TYPE,inas30.SC_LOCAL_NETWORK,inas30.SC_PROD_INST_GROUP_TYPE,inas30.SC_PROD_INST_STAT,inas30.SC_PROD_RES_SPECIALTY,inas30.SC_PROD_TYPE,inas30.SC_PROPERTY_DATA_TYPE,inas30.SC_PROPERTY_INPUT_TYPE,inas30.SC_STANDARD_CODE,inas30.SC_STANDARD_TYPE,inas30.SC_THREAD_FINISH_MODE,inas30.SC_THREAD_RUN_STATE,inas30.SC_WOI_COMP_IMPL_PARAM,inas30.SC_WOP_DATA_ORIGIN,inas30.SC_WO_ARCH_STATE,inas30.SC_WO_BUICOMP_STATE,inas30.SC_WO_MAN_ACT_OBJ_TYPE,inas30.SC_WO_MAN_ACT_TPYE,inas30.SC_WO_ORIGIN,inas30.SC_WO_REPLY_STATE,inas30.SC_WO_STATE,inas30.SC_WO_TYPE,inas30.TOS_APP_DOMAIN,inas30.TOS_APP_SERVER,inas30.TOS_AREA,inas30.TOS_BUSINESS_OBJ,inas30.TOS_BUTTON,inas30.TOS_CONFIG,inas30.TOS_CONFIG_CLOB,inas30.TOS_LOG,inas30.TOS_LOG_EXCEPTION,inas30.TOS_LOG_JOB,inas30.TOS_LOG_TYPE,inas30.TOS_MENU,inas30.TOS_MYDESKTOP,inas30.TOS_ORG,inas30.TOS_PRIV,inas30.TOS_PUBLIC_INFO,inas30.TOS_ROLE,inas30.TOS_ROLE_PRIV,inas30.TOS_STAFF,inas30.TOS_STAFF_ONLINE,inas30.TOS_STAFF_PRIV,inas30.TOS_VERSION_INFO) version=10.2.0.1.0

---匯入指令碼

匯入指令碼: parallel=4 啟四個並行,table_exists_action=replace 如果表存在 先drop再建立並匯入,remap_schema=inas30:inas_30 將源資料庫inas30下的表匯入到inas_30使用者下
impdp inas30_73/inas30_73 dumpfile=inas30_1112_tab.dmp logfile=inas30_1112_tab.log directory=DUMP_INAS30_73 tables=(inas30.ABI_ABIAPPLY_RULE_DIMENSION,inas30.ABI_ABILITY,inas30.ABI_ABILITYGROUP_APPLY_RULE,inas30.ABI_ABILITY_APPLY_RULE,inas30.ABI_ABILITY_CLASSIFICATION,inas30.ABI_ABILITY_EXEC_ABNORMITY,inas30.ABI_ABILITY_EXEC_ABNO_RULE,inas30.ABI_ABILITY_EXEC_ABNO_SYN_RULE,inas30.ABI_ABILITY_NEBSSERVICE_RELA,inas30.ABI_ABILITY_PARAM_RELA,inas30.ABI_ABILITY_PARAM_RELA_BAK,inas30.ABI_ABILITY_RELA,inas30.ABI_ABILITY_RELA_GROUP,inas30.ABI_ABILITY_RELA_GROUPDEF,inas30.ABI_NEBS_PLATFORM,inas30.ABI_NEBS_PROTOCOL,inas30.ABI_NEBS_SERVICE,inas30.ABI_NEBS_SERVICE_CATALOG,inas30.ABI_NEBS_SERVICE_PLATFORM_MAP,inas30.ABI_PROTOCOL_PARAM,inas30.ABI_WO_INFO_COMPONENTS,inas30.CHK_COMMAND_EQUALS,inas30.CHK_CWON_AUTOFINISH_FAILURE,inas30.CHK_PARAM_NVALEQU_IGNORE,inas30.CHK_WORKORDER,inas30.INAS_FIX_PARAM,inas30.INAS_FIX_TEMPLATE,inas30.INAS_PROTOCOL_PARAM,inas30.INAS_SC_PARAM,inas30.INAS_SERVICE_COMMAND,inas30.INAS_SERVICE_PLATFORM,inas30.INAS_WO_MACRO,inas30.INAS_WO_MACRO_ANALYSIS,inas30.INAS_WO_MACRO_ANALYSIS_F,inas30.INAS_WO_MACRO_CARD_RES,inas30.INAS_WO_MACRO_KZXKFIX,inas30.INAS_WO_PARAM,inas30.INAS_WO_XPATH,inas30.INS_ACTION,inas30.INS_CRM_EVENT,inas30.INS_EVENT,inas30.INS_EVENT_MAP,inas30.INS_OFFERS,inas30.INS_PRODUCT,inas30.INS_PRODUCT_MAP,inas30.INS_PROD_CLASSIFICATION,inas30.INS_PROD_PROPERTY,inas30.INS_PROD_PROP_VALUE,inas30.INS_PROD_PROP_VALUE_REL,inas30.INS_PROD_RESOURCE,inas30.INS_PROD_RES_REL,inas30.INS_PROPERTY_MAP,inas30.INS_SERVICE,inas30.INS_SERVICE_MAP,inas30.INT_INTERFACE,inas30.INT_ITM_RECORD,inas30.LTE_VC_ABILITY_MAP,inas30.SCV_ABI_PRIORITY_BASE,inas30.SCV_WO_PRIORITY_BASE,inas30.SC_ABILITY_ABTY_SOLVE_MODE,inas30.SC_ABILITY_ACTGROUP_RULE,inas30.SC_ABILITY_RELA_STATE,inas30.SC_ABILITY_RELA_TYPE,inas30.SC_ABILITY_TYPE,inas30.SC_BUSI_AREA,inas30.SC_CATALOG,inas30.SC_CWO_NEBSVR_STATE,inas30.SC_DISPATCH_ABNORMITY_STATE,inas30.SC_EXEC_TYPE,inas30.SC_FOREIGN_WO_INFO,inas30.SC_FOREIGN_WO_STATE,inas30.SC_IWO_BIL_ACT_TYPE,inas30.SC_LOCAL_NETWORK,inas30.SC_PROD_INST_GROUP_TYPE,inas30.SC_PROD_INST_STAT,inas30.SC_PROD_RES_SPECIALTY,inas30.SC_PROD_TYPE,inas30.SC_PROPERTY_DATA_TYPE,inas30.SC_PROPERTY_INPUT_TYPE,inas30.SC_STANDARD_CODE,inas30.SC_STANDARD_TYPE,inas30.SC_THREAD_FINISH_MODE,inas30.SC_THREAD_RUN_STATE,inas30.SC_WOI_COMP_IMPL_PARAM,inas30.SC_WOP_DATA_ORIGIN,inas30.SC_WO_ARCH_STATE,inas30.SC_WO_BUICOMP_STATE,inas30.SC_WO_MAN_ACT_OBJ_TYPE,inas30.SC_WO_MAN_ACT_TPYE,inas30.SC_WO_ORIGIN,inas30.SC_WO_REPLY_STATE,inas30.SC_WO_STATE,inas30.SC_WO_TYPE,inas30.TOS_APP_DOMAIN,inas30.TOS_APP_SERVER,inas30.TOS_AREA,inas30.TOS_BUSINESS_OBJ,inas30.TOS_BUTTON,inas30.TOS_CONFIG,inas30.TOS_CONFIG_CLOB,inas30.TOS_LOG,inas30.TOS_LOG_EXCEPTION,inas30.TOS_LOG_JOB,inas30.TOS_LOG_TYPE,inas30.TOS_MENU,inas30.TOS_MYDESKTOP,inas30.TOS_ORG,inas30.TOS_PRIV,inas30.TOS_PUBLIC_INFO,inas30.TOS_ROLE,inas30.TOS_ROLE_PRIV,inas30.TOS_STAFF,inas30.TOS_STAFF_ONLINE,inas30.TOS_STAFF_PRIV,inas30.TOS_VERSION_INFO) remap_schema=inas30:inas30_73 table_exists_action=replace parallel=4

 

若大量報此錯誤:

ORA-02291: 違反完整約束條件 (INAS30_73.FK_TOS_BUSI_REFERENCE_SC_STAND) - 未找到
父項關鍵字

意思是有些表的外來鍵表資料還沒有匯入,再匯入一次即可