Summary of the use of DBMS_METADATA.GET_DDL
In the daily work, often someone through the tool to see the definition of objects, in fact, sometimes it is annoying to view. In the work, also some colleagues asked me how to get the object definition, today I make a summary to everyone, hope to help.
Gets the package for the object definition: Dbms_metadata, where the GET_DDL function is the function that gets the object
The GET_DDL function returns a DDL statement that creates the original data for the object, as described in the parameter
1, Object_type---The object type of the DDL statement that needs to return the original data
2, Name---object names
3, Schema---Object of the schema, the default view of the current user's schema
4, Version---the original data versions of the object
5, Model---the original data type defaults to Oracle
6, transform. -Xsl-t transform. To be applied.
7, RETURNS: The original data of the object default to CLOB type return
Among them, we often use the first three items.
The GET_DDL function definition in the Dbms_metadata package:
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;
Attention:
1. If you need to format the following using Sqlplus, you will need to set up a long, or you will not be able to display the full SQL
2, the parameter should use the upper case, otherwise will not be able to check
Set Linesize 180
Set pages 999
Set Long 90000
1, view the definition of database table:
Select Dbms_metadata.get_ddl (' TABLE ', ' tablename ', ' USERNAME ') from dual;
2, view the index of SQL
Select Dbms_metadata.get_ddl (' INDEX ', ' indexname ', ' USERNAME ') from dual;
3. View the SQL that created the primary key
SELECT Dbms_metadata. GET_DDL (' CONSTRAINT ', ' constraintname ', ' USERNAME ') from DUAL;
4. View SQL for creating foreign keys
SELECT Dbms_metadata. GET_DDL (' Ref_constraint ', ' ref_constraintname ', ' USERNAME ') from DUAL;
5, view the SQL to create the view
SELECT Dbms_metadata. GET_DDL (' VIEW ', ' VIEWNAME ', ' USERNAME ') from DUAL;
6, view the user's SQL
SELECT Dbms_metadata. GET_DDL (' USER ', ' USERNAME ') from DUAL;
7, view the role of SQL
SELECT Dbms_metadata. GET_DDL (' role ', ' rolename ') from DUAL;
8, view the table space of SQL
SELECT Dbms_metadata. GET_DDL (' tablespace ', ' tablespacename ') from DUAL;
9. Get materialized View SQL
Select Dbms_metadata.get_ddl (' Materialized VIEW ', ' mvname ') from DUAL;
10. Get Remote connection definition SQL
SELECT dbms_metadata.get_ddl (' Db_link ', ' dblinkname ', ' USERNAME ') stmt from dual
11, get the user under the trigger SQL
Select Dbms_metadata. GET_DDL (' TRIGGER ', ' triggername ', ' USERNAME ') from DUAL;
12, get the user under the sequence
Select Dbms_metadata. GET_DDL (' SEQUENCE ', ' sequencename ') from DUAL;
13. Get the function under the user
Select Dbms_metadata. GET_DDL (' FUNCTION ', ' functionname ', ' USERNAME ') from DUAL
14, get the definition of package
Select Dbms_metadata. GET_DDL (' PACKAGE ', ' packagename ', ' USERNAME ') from dual
15. Get Stored Procedure
Select Dbms_metadata. GET_DDL (' PROCEDURE ', ' procedurename ', ' USERNAME ') from dual
16. Get package Body definition
Select Dbms_metadata. GET_DDL (' PACKAGE body ', ' packagebodyname ', ' USERNAME ') from dual
17, get the definition of the remote database object
SELECT Dbms_lob. Substr@dblinkname (Dbms_metadata. Get_ddl@dblinkname (' TABLE ', ' tablename ', ' USERNAME ') from Dual@dblinkname
18, get the definition of multiple objects
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 ';
This statement can be changed, you can get a lot of statements out
19. Common Mistakes
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
Solution: Run $ORACLE _home/rdbms/admin/catmeta.sql