20150721-Oracle的元SQL

    
    
    
    
  1. SELECT TABLE_NAME 表名,COLUMN_ID 列序号,COLUMN_NAME 列名,
  2. CASE DATA_TYPE
  3. WHEN 'DATE' THEN DATA_TYPE
  4. WHEN 'NUMBER' THEN DATA_TYPE||'('||DATA_PRECISION||','||DATA_SCALE||')'
  5. WHEN 'CHAR' THEN DATA_TYPE||'('||DATA_LENGTH||')'
  6. WHEN 'VARCHAR2' THEN DATA_TYPE||'('||DATA_LENGTH||')'
  7. ELSE DATA_TYPE END 数据类型,DECODE(NULLABLE,'N','必填','可Null') 是否为空 
  8. FROM USER_TAB_COLS
  9. ORDER BY TABLE_NAME,COLUMN_ID
  10. --返回 CLOB 类型
  11. select dbms_metadata.get_ddl('TABLE','TWIPD_MASTER','TW_HSP_PMPA') 建表语句 from dual;

http://czmmiao.iteye.com/blog/1004873

    
    
    
    
  1. 1.得到一个表或索引的ddl语句
  2. SELECT DBMS_METADATA.GET_DDL('TABLE','DEPT','SCOTT') FROM DUAL;
  3. select dbms_metadata.get_ddl('INDEX','PK_DEPT','SCOTT') from dual;
  4. 2.得到一个用户下的所有表,索引,存储过程的ddl
  5. SELECT DBMS_METADATA.GET_DDL(U.OBJECT_TYPE, u.object_name)
  6. FROM USER_OBJECTS u
  7. where U.OBJECT_TYPE IN ('TABLE','INDEX','PROCEDURE');
  8. 3.得到所有表空间的ddl语句
  9. SELECT DBMS_METADATA.GET_DDL('TABLESPACE', TS.tablespace_name)
  10. FROM DBA_TABLESPACES TS;
  11. 4.得到所有创建用户的ddl
  12. SELECT DBMS_METADATA.GET_DDL('USER',U.username)
  13. FROM DBA_USERS U;
     
     
     
     
  1. a. 获取单个的建表、视图和建索引的语法
  2. set pagesize 0
  3. set long 90000
  4. set feedback off
  5. set echo off
  6. spool DEPT.sql
  7. select dbms_metadata.get_ddl('TABLE','TAB_NAME','SCOTT') from dual;
  8. select dbms_metadata.get_ddl('VIEW','VIEW_NAME','SCOTT') from dual;
  9. select dbms_metadata.get_ddl('INDEX','IDX_NAME','SCOTT') from dual;
  10. spool off;
  11. b.获取一个SCHEMA下的所有建表、视图和建索引的语法,以scott为例:
  12. set pagesize 0
  13. set long 90000
  14. set feedback off
  15. set echo off
  16. spool schema.sql
  17. connect scott/tiger;
  18. SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name) FROM USER_TABLES u;
  19. SELECT DBMS_METADATA.GET_DDL('VIEW',u.VIEW_name) FROM USER_VIEWS u;
  20. SELECT DBMS_METADATA.GET_DDL('INDEX',u.index_name) FROM USER_INDEXES u;
  21. spool off;
  22. c. 获取某个SCHEMA的建全部存储过程的语法
  23. set pagesize 0
  24. set long 90000
  25. set feedback off
  26. set echo off
  27. spool procedures.sql
  28. select DBMS_METADATA.GET_DDL('PROCEDURE',u.object_name) from user_objects u where object_type = 'PROCEDURE';
  29. spool off;
  30. d. 获取某个SCHEMA的建全部函数的语法
  31. set pagesize 0
  32. set long 90000
  33. set feedback off
  34. set echo off
  35. spool function.sql
  36. select DBMS_METADATA.GET_DDL('FUNCTION',u.object_name) from user_objects u where object_type = 'FUNCTION';
  37. spool off;



来自为知笔记(Wiz)


你可能感兴趣的:(20150721-Oracle的元SQL)