How to get Oracle create table statement in SQL*Plus

折月煮酒 提交于 2019-11-26 11:17:02

问题


I have a table that exists in an Oracle database, but doesn\'t show on my list of tables in the tool SQL Developer. However, if I go to SQL*Plus, and do a

select table_name from user_tables;

I get the table listed. If I type

desc snp_clearinghouse;

it shows me the fields. I\'d like to get the create statement, because I need to add a field. I can modify the table to add the field, but I still need the create statement to put into our source control. What pl/sql statement is used to get the create statement for a table?


回答1:


From Get table and index DDL the easy way:

set heading off;
set echo off;
Set pages 999;
set long 90000;

spool ddl_list.sql

select dbms_metadata.get_ddl('TABLE','DEPT','SCOTT') from dual;

select dbms_metadata.get_ddl('INDEX','DEPT_IDX','SCOTT') from dual;

spool off;



回答2:


Same as above but generic script found here gen_create_table_script.sql

-- #############################################################################################
--
-- %Purpose: Generate 'CREATE TABLE' Script for an existing Table in the database
--
-- Use:      SYSTEM, SYS or user having SELECT ANY TABLE  system privilege
--
-- #############################################################################################
--
set serveroutput on size 200000
set echo off
set feedback off
set verify off
set showmode off
--
ACCEPT l_user CHAR PROMPT  'Username: '
ACCEPT l_table CHAR PROMPT 'Tablename: '
--
DECLARE
 CURSOR TabCur IS
 SELECT table_name,owner,tablespace_name,
        initial_extent,next_extent,
        pct_used,pct_free,pct_increase,degree
   FROM sys.dba_tables
  WHERE owner=upper('&&l_user')
    AND table_name=UPPER('&&l_table');
--
 CURSOR ColCur(TableName varchar2) IS
 SELECT column_name col1,
        DECODE (data_type,
                'LONG',       'LONG   ',
                'LONG RAW',   'LONG RAW  ',
                'RAW',        'RAW  ',
                'DATE',       'DATE   ',
                'CHAR',       'CHAR' || '(' || data_length || ') ',
                'VARCHAR2',   'VARCHAR2' || '(' || data_length || ') ',
                'NUMBER',     'NUMBER' ||
                DECODE (NVL(data_precision,0),0, ' ',' (' || data_precision ||
                DECODE (NVL(data_scale, 0),0, ') ',',' || DATA_SCALE || ') '))) ||
        DECODE (NULLABLE,'N', 'NOT NULL','  ') col2
   FROM sys.dba_tab_columns
  WHERE table_name=TableName
    AND owner=UPPER('&&l_user')
 ORDER BY column_id;
--
 ColCount    NUMBER(5);
 MaxCol      NUMBER(5);
 FillSpace   NUMBER(5);
 ColLen      NUMBER(5);
--
BEGIN
 MaxCol:=0;
 --
 FOR TabRec in TabCur LOOP
    SELECT MAX(column_id) INTO MaxCol FROM sys.dba_tab_columns
     WHERE table_name=TabRec.table_name
       AND owner=TabRec.owner;
    --
    dbms_output.put_line('CREATE TABLE '||TabRec.table_name);
    dbms_output.put_line('( ');
    --
    ColCount:=0;
    FOR ColRec in ColCur(TabRec.table_name) LOOP
      ColLen:=length(ColRec.col1);
      FillSpace:=40 - ColLen;
      dbms_output.put(ColRec.col1);
      --
      FOR i in 1..FillSpace LOOP
         dbms_output.put(' ');
      END LOOP;
      --
      dbms_output.put(ColRec.col2);
      ColCount:=ColCount+1;
      --
      IF (ColCount < MaxCol) THEN
         dbms_output.put_line(',');
      ELSE
         dbms_output.put_line(')');
      END IF;
    END LOOP;
    --
    dbms_output.put_line('TABLESPACE '||TabRec.tablespace_name);
    dbms_output.put_line('PCTFREE '||TabRec.pct_free);
    dbms_output.put_line('PCTUSED '||TabRec.pct_used);
    dbms_output.put_line('STORAGE ( ');
    dbms_output.put_line('  INITIAL     '||TabRec.initial_extent);
    dbms_output.put_line('  NEXT        '||TabRec.next_extent);
    dbms_output.put_line('  PCTINCREASE '||TabRec.pct_increase);
    dbms_output.put_line(' )');
    dbms_output.put_line('PARALLEL '||TabRec.degree);
    dbms_output.put_line('/');
 END LOOP;
END;
/


来源:https://stackoverflow.com/questions/937398/how-to-get-oracle-create-table-statement-in-sqlplus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!