使用DBMS_METADATA.GET_DDL函数包获取一个对象的DDL脚本,并且DDL脚本中不包含创建该DDL的用户信息
适用于:
数据库企业版,版本10gr1~11gr2.文档适用于任何操作系统平台
目标:
如何运用dbms_metadata.get_ddl方法获取创建表的DDL语句,但是在返回的DDL语句中不能指定创建该DDL脚本的用户
方法:
用dbms_metadata.add_transform方法的mofify模式,将句柄传递到存储过程
DBMS_METADATA.SET_REMAP_PARAM (
transform_handle IN NUMBER,
name IN VARCHAR2,
old_value IN VARCHAR2,
new_value IN VARCHAR2,
object_type IN VARCHAR2 DEFAULT NULL);
提供REMAP_SCHEMA作为名称的参数,OLD_VALUE作为用户对象的参数,NEW_VALUE作为一个空字符串参数。最后用DBMS_METADATA.ADD_TRANSFORM方法过滤对象,如下面的例子
set pages 1000
set long 1000
var h number
var ddl_handle number
var modify_handle number
exec :h := DBMS_METADATA.OPEN('TABLE')
exec :modify_handle := DBMS_METADATA.ADD_TRANSFORM(:h,'MODIFY')
exec DBMS_METADATA.SET_REMAP_PARAM(:modify_handle,'REMAP_SCHEMA','SCOTT','')
exec :ddl_handle := DBMS_METADATA.ADD_TRANSFORM(:h,'DDL')
exec DBMS_METADATA.SET_FILTER(:h, 'NAME','EMP')
代码执行成功后,执行select DBMS_METADATA.fetch_clob(:h) from dual;查询语句,将返回如下的查询结果:
CREATE TABLE "EMP"
( "EMPNO" NUMBER(4,0),
"ENAME" VARCHAR2(10),
"JOB" VARCHAR2(9),
"MGR" NUMBER(4,0),
"HIREDATE" DATE,
"SAL" NUMBER(7,2),
"COMM" NUMBER(7,2),
"DEPTNO" NUMBER(2,0),
CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS" ENABLE,
CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO")
REFERENCES "DEPT" ("DEPTNO") ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS"