HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Script out Oracle DDL in an automated fashion

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
scriptddlautomatedfashionoracleout

Problem

Oracle SQL Developer is able to export DDL through Tools -> Database Export... This works very well, but requires manual intervention.

I know of DBMS_METADATA.get_ddl(), but have found that the export isn't perfect. I ran into issues where the exported DBMS_METADATA DDL wasn't usable without first fixing up problems like breaks in the middle of a keyword, and worse. However, if anyone knows a way of exporting DDL through DMBS_METADATA that can run without manual fixes, that'd be a great solution too.

Basically, I am looking for an automatic/scriptable way to export DDL identical to what is exported through the manual way.

How can I do that?

Solution

The reason you are having problems with dbms_metadata.get_ddl is that it outputs CLOBs which can be up to 4GB in size. By default, SQL*Plus and Oracle SQL Developer truncate long text so they don't trash the client with large gobs of text.

It's very easy to override this behavior in SQL*Plus with a few SET commands and get clean DDL.

The script you need is:

-- Run this script in SQL*Plus.

-- don't print headers or other crap
set heading off;
set echo off;
set pagesize 0;

-- don't truncate the line output
-- trim the extra space from linesize when spooling
set long 99999;
set linesize 32767;
set trimspool on;

-- don't truncate this specific column's output
col object_ddl format A32000;

spool sys_ddl.sql;

SELECT dbms_metadata.get_ddl(object_type, object_name, owner) || ';' AS object_ddl
FROM DBA_OBJECTS
WHERE
OWNER = 'SYS'
AND OBJECT_TYPE IN (
'TABLE'
, 'INDEX'
, 'SEQUENCE'
, 'VIEW'
)
ORDER BY
OWNER
, OBJECT_TYPE
, OBJECT_NAME
;

spool off;

Context

StackExchange Database Administrators Q#8392, answer score: 7

Revisions (0)

No revisions yet.