ITPub博客

首页 > 数据库 > Oracle > dbms_metadata.get_ddl的用法

dbms_metadata.get_ddl的用法

原创 Oracle 作者:Opendba 时间:2015-10-09 16:27:15 0 删除 编辑
定义:
dbms_metadata包中的get_ddl函数
--GET_DDL: Return the metadata for a single object as DDL.
-- This interface is meant for casual browsing (e.g., from SQLPlus)
-- vs. the programmatic OPEN / FETCH / CLOSE interfaces above.
-- PARAMETERS:
-- object_type - The type of object to be retrieved.
-- name - Name of the object.
-- schema - Schema containing the object. Defaults to
-- the caller's schema.
-- version - The version of the objects' metadata.
-- model - The object model for the metadata.
-- transform. - XSL-T transform. to be applied.
-- RETURNS: Metadata for the object transformed to DDL as a CLOB.

FUNCTION get_ddl ( object_type IN VARCHAR2,
name IN VARCHAR2,
schema IN VARCHAR2 DEFAULT NULL,
version IN VARCHAR2 DEFAULT 'COMPATIBLE',
model IN VARCHAR2 DEFAULT 'ORACLE',
transform. IN VARCHAR2 DEFAULT 'DDL') RETURN CLOB;


示例:
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;

案例:
利用DBMS_METADATA.GET_DDL包得到数据库的对象的ddl脚本。如下(SQLPLUS中执行):
a. 获取单个的建表、视图和建索引的语法
set pagesize 0
set longc 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;

---另附一个提取ROLE的脚本,割接ROLE非常方便------

点击(此处)折叠或打开

  1. --ROLE_GETDDL.sql
  2. --2014.12.13
  3. set long 20000 longchunksize 20000 pagesize 0 linesize 1000 feedback off verify off trimspool on
  4. column ddl format a1000

  5. begin
  6.    dbms_metadata.set_transform_param (dbms_metadata.session_transform, 'SQLTERMINATOR', true);
  7.    dbms_metadata.set_transform_param (dbms_metadata.session_transform, 'PRETTY', true);
  8. end;
  9. /

  10. variable v_role VARCHAR2(30);

  11. exec :v_role := upper('&1');

  12. select dbms_metadata.get_ddl('ROLE', r.role) AS ddl
  13. from dba_roles r
  14. where r.role = :v_role
  15. union all
  16. select dbms_metadata.get_granted_ddl('ROLE_GRANT', rp.grantee) AS ddl
  17. from dba_role_privs rp
  18. where rp.grantee = :v_role
  19. and rownum = 1
  20. union all
  21. select dbms_metadata.get_granted_ddl('SYSTEM_GRANT', sp.grantee) AS ddl
  22. from dba_sys_privs sp
  23. where sp.grantee = :v_role
  24. and rownum = 1
  25. union all
  26. select dbms_metadata.get_granted_ddl('OBJECT_GRANT', tp.grantee) AS ddl
  27. from dba_tab_privs tp
  28. where tp.grantee = :v_role
  29. and rownum = 1
  30. /

  31. set linesize 80 pagesize 14 feedback on verify on

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30264304/viewspace-1813614/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2015-05-28

  • 博文量
    7
  • 访问量
    28565