oracle匯出DDL

孤竹星發表於2015-11-23

dbms_metadata.get_ddl

[@more@]

dbms_metadata.get_ddl

1.得到一個表或索引的ddl語句
SELECT DBMS_METADATA.GET_DDL('TABLE','DEPT','SCOTT') FROM DUAL;

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

2.得到一個使用者下的所有表,索引,儲存過程的ddl

SELECT DBMS_METADATA.GET_DDL(U.OBJECT_TYPE, u.object_name)
FROM USER_OBJECTS u
where U.OBJECT_TYPE IN ('TABLE','INDEX','PROCEDURE');

3.得到所有表空間的ddl語句

SELECT DBMS_METADATA.GET_DDL('TABLESPACE', TS.tablespace_name)
FROM DBA_TABLESPACES TS;

4.得到所有建立使用者的ddl

SELECT DBMS_METADATA.GET_DDL('USER',U.username)
FROM DBA_USERS U;

應用:

a. 獲取單個的建表、檢視和建索引的語法

set pagesize 0
set long 90000
set feedback off
set echo off
spool DEPT.sql
select dbms_metadata.get_ddl('TABLE','TAB_NAME','SCOTT') from dual;
select dbms_metadata.get_ddl('VIEW','VIEW_NAME','SCOTT') from dual;
select dbms_metadata.get_ddl('INDEX','IDX_NAME','SCOTT') from dual;
spool off;

b.獲取一個SCHEMA下的所有建表、檢視和建索引的語法,以scott為例:
set pagesize 0
set long 90000
set feedback off
set echo off
spool schema.sql
connect scott/tiger;
SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name) FROM USER_TABLES u;
SELECT DBMS_METADATA.GET_DDL('VIEW',u.VIEW_name) FROM USER_VIEWS u;
SELECT DBMS_METADATA.GET_DDL('INDEX',u.index_name) FROM USER_INDEXES u;
spool off;

c. 獲取某個SCHEMA的建全部儲存過程的語法
set pagesize 0
set long 90000
set feedback off
set echo off
spool procedures.sql
select DBMS_METADATA.GET_DDL('PROCEDURE',u.object_name) from user_objects u where object_type = 'PROCEDURE';
spool off;

d. 獲取某個SCHEMA的建全部函式的語法
set pagesize 0
set long 90000
set feedback off
set echo off
spool function.sql
select DBMS_METADATA.GET_DDL('FUNCTION',u.object_name) from user_objects u where object_type = 'FUNCTION';
spool off;

獲取某個使用者的所有索引的DDL

select dbms_metadata.get_ddl('INDEX',index_name,owner) from dba_indexes a where a.owner='SCOTT';

select dbms_metadata.get_ddl('INDEX',index_name,owner) from dba_indexes a where a.owner not like '%SYS%'

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28282660/viewspace-1844367/,如需轉載,請註明出處,否則將追究法律責任。

相關文章