I had a requirement to generate DDL scripts of a particular schema in an Oracle database. I figure that there’s no way I am going to spent time cutting and pasting all these function, procedure and package codes from Toad or Sql Developer. I’m sure there is a faster way of doing this, and moreover I am too lazy to do just that.
And because of being lazy, I learned about this cool function in Oracle.
DBMS_METADATA.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;
And using this function I was able to script out the DDL codes.
set pagesize 0
set linesize 300
set trimspool on
spool ddl_generator.sql;
select 'set linesize 32767 long 9999999 longc 9999999 pagesize 0 trimspool on feedback off' from dual;
select
'spool '||object_name||'.'||replace(object_type,' ','_')||'.sql;'||chr(10)||'select dbms_metadata.get_ddl('||chr(39)||replace(object_type,' ','_')||chr(39)||','||chr(39)||object_name||chr(39)||','||chr(39)||'SMSII'||chr(39)||') from dual;'||chr(10)||'spool off;'
from dba_objects where owner = 'REDDIT'
and object_type in ('PROCEDURE','FUNCTION','PACKAGE','PACKAGE BODY','TRIGGER');
spool off;
The script will spool another script that when run will generate the DDL codes from a particular schema.
I must say, this is a kind of situation when being lazy is actually a good thing. Because of laziness, you prevent yourself from doing boring, dull and repetitive work and seek for other solution that, when applied, can accomplish the same goal but with the least amount of effort. The process of coming up with this solution usually requires you to think harder, but the time you spend on it is definitely much more interesting and learning.

