单位一个同事误把Exp导入了system帐号,但是系统已经运行了一段时间,数据需要保留,所以不能用源dmp文件重新初始化..
解决过程如下
1.确定误导入对象的范围
2.表的处理
3.其他对象的处理
4.删除system帐号下误导入的对象
初始化对象,并导入system帐号
-
create table t1 as select rownum n from dual connect by level<10;
-
create table t2 as select rownum n from dual connect by level<10;
-
create table t3 as select rownum n from dual connect by level<10;
-
create table t4 as select rownum n from dual connect by level<10;
-
-
create index inx_t1 on t1(n);
-
create index inx_t2 on t2(n);
-
create index inx_t3 on t3(n);
-
create index inx_t4 on t4(n);
-
-
create view v1 as select * from t1;
C:\Users\lihuilin>imp system/xxxxxx file=C:\tmp\test.dmp full=y
Import: Release 11.2.0.1.0 - Production on 星期六 11月 9 23:01:33 2013
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
经由常规路径由 EXPORT:V11.02.00 创建的导出文件
警告: 这些对象由 LIHUILIN 导出, 而不是当前用户
已经完成 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集中的导入
. 正在将 LIHUILIN 的对象导入到 SYSTEM
. . 正在导入表 "T1"导入了 9 行
. . 正在导入表 "T2"导入了 9 行
. . 正在导入表 "T3"导入了 9 行
. . 正在导入表 "T4"导入了 9 行
成功终止导入, 没有出现警告。
1.确定误导入对象的范围。
-
select object_name,object_type from user_objects where created>sysdate-1;
-
OBJECT_NAME OBJECT_TYPE
-
--------------- -------------------
-
V1 VIEW
-
INX_T4 INDEX
-
T4 TABLE
-
INX_T3 INDEX
-
T3 TABLE
-
INX_T2 INDEX
-
T2 TABLE
-
INX_T1 INDEX
-
T1 TABLE
-
-
已选择9行。
2.表的处理
找到所有误导入的表,然后使用exp将其重新导入正确的帐号。
真实的情况是误导入的表有几百张,所以需要用下面的SQL生成需要导出的表名称。
-
select max(ltrim(tables,',')) tables
-
from
-
(
-
select sys_connect_by_path(object_name,',') tables
-
from
-
(
-
select object_name,
-
object_type,
-
rownum r
-
from user_objects
-
where created >sysdate-1
-
and object_type='TABLE'
-
) v
-
start with r=1
-
connect by r=prior r+1
-
);
-
-
TABLES
----------------------------
T1,T2,T3,T4
C:\Users\lihuilin>exp system/xxxxxx tables=T1,T2,T3,T4 file=c:\tmp\t.dmp
Export: Release 11.2.0.1.0 - Production on 星期六 11月 9 23:31:29 2013
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
已导出 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集
即将导出指定的表通过常规路径...
. . 正在导出表 T1导出了 9 行
. . 正在导出表 T2导出了 9 行
. . 正在导出表 T3导出了 9 行
. . 正在导出表 T4导出了 9 行
成功终止导出, 没有出现警告。
3.其他对象的处理
使用DBMS_METADATA获取创建语句
-
select 'select dbms_metadata.get_ddl('''||object_type||''','''||object_name||''') from dual;'
-
from user_objects where created>sysdate-1 and object_type not in ('TABLE','INDEX');
执行生成的SQL
SQL> select dbms_metadata.get_ddl('VIEW','V1') from dual;
CREATE OR REPLACE FORCE VIEW "SYSTEM"."V1" ("N") AS
select "N" from t1
将生成的DDL在目标帐号执行。
4.删除system帐号的错误数据
-
set echo off
-
set feedback off
-
set newpage none
-
set pagesize 5000
-
set linesize 500
-
set verify off
-
set pagesize 0
-
set term off
-
set trims on
-
set linesize 600
-
set heading off
-
set timing off
-
set verify off
-
set numwidth 38
-
spool drop.sql
-
select 'drop '|| object_type||' '||object_name||decode(object_type,'TABLE',' purge;',';') from user_objects where created>sysdate-1 and object_type not in ('INDEX','LOB');
-
spool off
-
@drop.sql
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-776221/
,如需转载,请注明出处,否则将追究法律责任。