Skip to content

Instantly share code, notes, and snippets.

Created March 28, 2013 16:18
Show Gist options
  • Save anonymous/5264556 to your computer and use it in GitHub Desktop.
Save anonymous/5264556 to your computer and use it in GitHub Desktop.
How to script table creation in Oracle

if the table isn't local i.e. database link you'll need to create a local version to sniff

CREATE TABLE foo AS SELECT * FROM SCHEMA.TABLE@DBLINK.WORLD WHERE rownum < 11;

but surely this is enough? actually no, this will be a best guess by oracle. the next bit of sql will allow you to tailor the schema create script without all the scripted guff that oracle likes to add.

SELECT decode(A.COLUMN_ID,1,'CREATE TABLE ' || A.TABLE_NAME || '(\n','') || RPAD(COLUMN_NAME ,50) || 
DECODE( DATA_TYPE,'VARCHAR2',DATA_TYPE ||'(' ||DATA_LENGTH || ')',
    'NUMBER',DATA_TYPE ||'(' ||DATA_PRECISION || decode(DATA_SCALE,0,')', ',' || DATA_SCALE || ')'),
    DATA_TYPE)
 || DECODE(NULLABLE,'N','          NOT NULL','') ||  decode( SIGN(A.COLUMN_ID - b.COLUMN_ID), 0, ' );',' ,') AS FOO
FROM ALL_TAB_COLUMNS A, (SELECT TABLE_NAME, MAX(COLUMN_ID) COLUMN_ID FROM ALL_TAB_COLUMNS 
WHERE OWNER = 'OWNER_WHERE_LOCAL_COPY_OF_FOO_RESIDES' GROUP BY TABLE_NAME)B
WHERE A.TABLE_NAME = B.TABLE_NAME
AND A.TABLE_NAME in ('foo')
ORDER BY  A.TABLE_NAME,A.COLUMN_ID;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment