Sunday, April 13, 2008

How to Get DDL from Database Objects.

The DBMS_METADATA package provides a way for you to retrieve metadata from the database dictionary.
There are different subprograms to help us to get scripts of creation objects which helps to get an idea about the attribute of the objects.

Several subprograms of DBMS_METADATA Package are,

1)GET_DDL /GET_XML (used to fetch named objects, especially schema objects (tables, views)).

2)GET_DEPENDENT_DDL/GET_DEPENDENT_XML (used to fetch dependent objects (audits, object grants)).

3)GET_GRANTED_DDL/GET_GRANTED_XML (used to fetch granted objects (system grants, role grants)).


Example:
--------------
1)Fetch the DDL for all Complete Tables in the Current Schema, Filter Out Nested Tables and Overflow Segments:


SET LONG 2000000
SET PAGESIZE 0
EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM
(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false);
SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name)
FROM USER_ALL_TABLES u
WHERE u.nested='NO'
AND (u.iot_type is null or u.iot_type='IOT');
EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'DEFAULT');

2)Fetch the DDL For All System Grants Granted To Arju.
-----------------------------------------------------


SELECT DBMS_METADATA.GET_GRANTED_DDL('SYSTEM_GRANT','ARJU') from dual;


3)Fetch the DDL For All Object Grants On ARJU.TEST
-------------------------------------------------


SELECT DBMS_METADATA.GET_DEPENDENT_DDL('OBJECT_GRANT',
'TEST','ARJU') FROM DUAL;
---------------------
GRANT SELECT ON "ARJU"."TEST" TO "TEST1"

4)Fetch the DDL for Table ARJU.TEST:
------------------------------------

SELECT DBMS_METADATA.GET_DDL('TABLE','TEST','ARJU') from dual;

Here first argument is object_type second one is table name and third one is owner name. If you own the table then while querying you can omit third argument as by default it takes current user.
In order to see the lists of object type which you can query from DBMS_METADAT.GET_DDL have a look at http://arjudba.blogspot.com/2008/06/table-view-tablespace-creation-script.html
Related Documents
How to get different object_type creation Script
Extract DDL from dump file

No comments:

Post a Comment