GET_DDL函数返回创建对象的原数据的DDL语句,参数说明:
1、OBJECT_TYPE ---需要返回原数据的DDL语句的对象类型
2、NAME --- 对象名称
3、SCHEMA ---对象所在的SCHEMA,默认为当前用户所在所SCHEMA
4、VERSION ---对象原数据的版本
5、MODEL ---原数据的类型默认为ORACLE
6、TRANSFORM. - XSL-T TRANSFORM. TO BE APPLIED.
7、RETURNS: 对象的原数据默认以CLOB类型返回
DBMS_METADATA包中的GET_DDL函数定义:
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、如果使用SQLPLUS需要进行下列格式化,特别需要对LONG进行设置,否则无法显示完整的SQL
2、参数要使用大写,否则查询会有问题可能无法显示
SET HEADING OFF
SET ECHO OFF
SET FLUSH OFF
SET PAGESIZE 9000
SET LINESIZE 80
SET LONG 100000
1、查看数据库表的定义写法:
SELECT DBMS_METADATA.GET_DDL('TABLE','TABLENAME','USERNAME') FROM DUAL;
2、查看索引的SQL
SELECT DBMS_METADATA.GET_DDL('INDEX','INDEXNAME','USERNAME') FROM DUAL;
3、查看创建主键的SQL
SELECT DBMS_METADATA.GET_DDL('CONSTRAINT','CONSTRAINTNAME','USERNAME') FROM DUAL;
4、查看创建外键的SQL
SELECT DBMS_METADATA.GET_DDL('REF_CONSTRAINT','REF_CONSTRAINTNAME','USERNAME') FROM DUAL;
5、查看创建视图的SQL
SELECT DBMS_METADATA.GET_DDL('VIEW','VIEWNAME','USERNAME') FROM DUAL;
6、查看用户的SQL
SELECT DBMS_METADATA.GET_DDL('USER','USERNAME') FROM DUAL;
7、查看角色的SQL
SELECT DBMS_METADATA.GET_DDL('ROLE','ROLENAME') FROM DUAL;
8、查看表空间的SQL
SELECT DBMS_METADATA.GET_DDL('TABLESPACE','TABLESPACENAME') FROM DUAL;
9、获取物化视图SQL
SELECT DBMS_METADATA.GET_DDL('MATERIALIZED VIEW','MVNAME') FROM DUAL;
10、获取远程连接定义SQL
SELECT DBMS_METADATA.GET_DDL('DB_LINK','DBLINKNAME','USERNAME') STMT FROM DUAL
11、获取用户下的触发器SQL
SELECT DBMS_METADATA.GET_DDL('TRIGGER','TRIGGERNAME','USERNAME) FROM DUAL;
12、获取用户下的序列
SELECT DBMS_METADATA.GET_DDL('SEQUENCE','SEQUENCENAME') FROM DUAL;
13、获取用户下的函数
SELECT DBMS_METADATA.GET_DDL('FUNCTION','FUNCTIONNAME','USERNAME') FROM DUAL
14、获取包的定义
SELECT DBMS_METADATA.GET_DDL('PACKAGE','PACKAGENAME','USERNAME') FROM DUAL
15、获取存储过程
SELECT DBMS_METADATA.GET_DDL('PROCEDURE','PROCEDURENAME','USERNAME') FROM DUAL
16、获取包体定义
SELECT DBMS_METADATA.GET_DDL('PACKAGE BODY','PACKAGEBODYNAME','USERNAME') FROM DUAL
17、获取远程数据库对象的定义
SELECT DBMS_LOB.SUBSTR@DBLINKNAME(DBMS_METADATA.GET_DDL@DBLINKNAME('TABLE', 'TABLENAME', 'USERNAME')) FROM DUAL@DBLINKNAME
18、获取多个对象的定义
SELECT DBMS_METADATA.GET_DDL(O.OBJECT_TYPE, O.OBJECT_NAME,O.OWNER)
FROM DBA_OBJECTS O
WHERE O.OBJECT_TYPE IN ('TABLE','INDEX','PROCEDURE','FUNCTION') AND ONWER = 'ONWERNAME';
19、常见错误
SQL> SELECT DBMS_METADATA.GET_DDL('TABLE','TABLENAME','USERNAME') FROM DUAL;
ERROR:
ORA-19206: INVALID VALUE FOR QUERY OR REF CURSOR PARAMETER
ORA-06512: AT "SYS.DBMS_XMLGEN", LINE 83
ORA-06512: AT "SYS.DBMS_METADATA", LINE 345
ORA-06512: AT "SYS.DBMS_METADATA", LINE 410
ORA-06512: AT "SYS.DBMS_METADATA", LINE 449
ORA-06512: AT "SYS.DBMS_METADATA", LINE 615
ORA-06512: AT "SYS.DBMS_METADATA", LINE 1221
ORA-06512: AT LINE 1
NO ROWS SELECTED
解决办法:运行 $ORACLE_HOME/RDBMS/ADMIN/CATMETA.SQL
阅读(1667) | 评论(0) | 转发(0) |