温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

dbms_metadata.get_ddl的用法

发布时间:2020-08-17 23:58:59 来源:ITPUB博客 阅读:172 作者:不一样的天空w 栏目:关系型数据库

--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;

==========================================================

9i  中可以利用DBMS_METADATA.GET_DDL包得到数据库的对象的ddl脚本。如下(SQLPLUS中执行):

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;


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI