Oracle利用DBMS_METADATA.GET_DDL获取对象DDL语句

 

dbms_metadata.get_ddl

1.得到一个表或索引的ddl语句
SELECT DBMS_METADATA.GET_DDL(‘TABLE‘,‘DEPT‘,‘SCOTT‘) FROM DUAL;

select dbms_metadata.get_ddl(‘INDEX‘,‘PK_DEPT‘,‘SCOTT‘) from dual;

 

2.得到一个用户下的所有表,索引,存储过程的ddl

SELECT DBMS_METADATA.GET_DDL(U.OBJECT_TYPE, u.object_name)
FROM USER_OBJECTS u
where U.OBJECT_TYPE IN (‘TABLE‘,‘INDEX‘,‘PROCEDURE‘);

3.得到所有表空间的ddl语句

SELECT DBMS_METADATA.GET_DDL(‘TABLESPACE‘, TS.tablespace_name)
FROM DBA_TABLESPACES TS;

4.得到所有创建用户的ddl

SELECT DBMS_METADATA.GET_DDL(‘USER‘,U.username)
FROM DBA_USERS U;

应用:

a. 获取单个的建表、视图和建索引的语法

set pagesize 0
set long 90000
set feedback off
set echo off
spool DEPT.sql
select dbms_metadata.get_ddl(‘TABLE‘,‘TAB_NAME‘,‘SCOTT‘) from dual;
select dbms_metadata.get_ddl(‘VIEW‘,‘VIEW_NAME‘,‘SCOTT‘) from dual;
select dbms_metadata.get_ddl(‘INDEX‘,‘IDX_NAME‘,‘SCOTT‘) from dual;
spool off;

b.获取一个SCHEMA下的所有建表、视图和建索引的语法,以scott为例:
set pagesize 0
set long 90000
set feedback off
set echo off
spool schema.sql
connect scott/tiger;
SELECT DBMS_METADATA.GET_DDL(‘TABLE‘,u.table_name) FROM USER_TABLES u;
SELECT DBMS_METADATA.GET_DDL(‘VIEW‘,u.VIEW_name) FROM USER_VIEWS u;
SELECT DBMS_METADATA.GET_DDL(‘INDEX‘,u.index_name) FROM USER_INDEXES u;
spool off;

c. 获取某个SCHEMA的建全部存储过程的语法
set pagesize 0
set long 90000
set feedback off
set echo off
spool procedures.sql
select DBMS_METADATA.GET_DDL(‘PROCEDURE‘,u.object_name) from user_objects u where object_type = ‘PROCEDURE‘;
spool off;

d. 获取某个SCHEMA的建全部函数的语法
set pagesize 0
set long 90000
set feedback off
set echo off
spool function.sql
select DBMS_METADATA.GET_DDL(‘FUNCTION‘,u.object_name) from user_objects u where object_type = ‘FUNCTION‘;
spool off;

获取某个用户的所有索引的DDL

select dbms_metadata.get_ddl(‘INDEX‘,index_name,owner) from dba_indexes a where a.owner=‘SCOTT‘;

select dbms_metadata.get_ddl(‘INDEX‘,index_name,owner) from dba_indexes a where a.owner not like ‘%SYS%‘;

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。