oracle dbms_metadata 獲取ddl語句
直接查詢
SQL> select dbms_metadata.get_ddl('TABLE','EMP','SCOTT') from dual; DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT') -------------------------------------------------------------------------------- CREATE TABLE "SCOTT"."EMP" ( "EMPNO" NUMBER(4,0), "ENAME" VARCHAR2(10), SQL> |
此時會發現使輸出的結果不全,這是因為sqlplus 預設輸出的lob長度為80,做如下設定後查詢
SQL> set pagesize 1000 SQL> set long 8000 SQL> select dbms_metadata.get_ddl('TABLE','EMP','SCOTT') from dual; DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT') --------------------------------------------------------------------------------
CREATE TABLE "SCOTT"."EMP" ( "EMPNO" NUMBER(4,0), "ENAME" VARCHAR2(10), "JOB" VARCHAR2(9), "MGR" NUMBER(4,0), "HIREDATE" DATE, "SAL" NUMBER(7,2), "COMM" NUMBER(7,2), "DEPTNO" NUMBER(2,0), CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "USERS" ENABLE, CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO") REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "USERS"
|
此時可以發現表所有相關屬性全部列出;
但是某些時候,並不需要顯示的這麼全,所以可以有選擇性的進行格式化輸出:
不顯示儲存屬性等:
SQL> execute dbms_metadata.set_transform_param(dbms_metadata.session_transform,'SEGMENT_ATTRIBUTES', false); PL/SQL procedure successfully completed. SQL> select dbms_metadata.get_ddl('TABLE','EMP','SCOTT') from dual; DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT') --------------------------------------------------------------------------------
CREATE TABLE "SCOTT"."EMP" ( "EMPNO" NUMBER(4,0), "ENAME" VARCHAR2(10), "JOB" VARCHAR2(9), "MGR" NUMBER(4,0), "HIREDATE" DATE, "SAL" NUMBER(7,2), "COMM" NUMBER(7,2), "DEPTNO" NUMBER(2,0), CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO") ENABLE, CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO") REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE ) |
格式化輸出,不顯示約束等:
SQL> execute dbms_metadata.set_transform_param(dbms_metadata.session_transform,'CONSTRAINTS', FALSE) PL/SQL procedure successfully completed. SQL> execute dbms_metadata.set_transform_param(dbms_metadata.session_transform,'REF_CONSTRAINTS', FALSE) PL/SQL procedure successfully completed. SQL> execute dbms_metadata.set_transform_param(dbms_metadata.session_transform,'SQLTERMINATOR', TRUE) PL/SQL procedure successfully completed. SQL> select dbms_metadata.get_ddl('TABLE','EMP','SCOTT') from dual; DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT') -------------------------------------------------------------------------------- CREATE TABLE "SCOTT"."EMP" ( "EMPNO" NUMBER(4,0), "ENAME" VARCHAR2(10), "JOB" VARCHAR2(9), "MGR" NUMBER(4,0), "HIREDATE" DATE, "SAL" NUMBER(7,2), "COMM" NUMBER(7,2), "DEPTNO" NUMBER(2,0) ) ; SQL> |
最簡單的方式:
SQL> execute dbms_metadata.set_transform_param(dbms_metadata.session_transform,'PRETTY', FALSE) PL/SQL procedure successfully completed. SQL> select dbms_metadata.get_ddl('TABLE','EMP','SCOTT') from dual; DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT') -------------------------------------------------------------------------------- CREATE TABLE "SCOTT"."EMP" ("EMPNO" NUMBER(4,0), "ENAME" VARCHAR2(10), "JOB" V ARCHAR2(9), "MGR" NUMBER(4,0), "HIREDATE" DATE, "SAL" NUMBER(7,2), "COMM" NUMBER (7,2), "DEPTNO" NUMBER(2,0)) ; |
同時需要了解表所在使用者擁有的許可權,以便能夠正常的使用表:
SQL> SELECT DBMS_METADATA.GET_GRANTED_DDL('ROLE_GRANT','SCOTT') FROM DUAL;
DBMS_METADATA.GET_GRANTED_DDL('ROLE_GRANT','SCOTT') -------------------------------------------------------------------------------- GRANT "CONNECT" TO "SCOTT"; GRANT "RESOURCE" TO "SCOTT"; SQL> |
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25656398/viewspace-704837/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle中獲取TABLE的DDL語句的方法Oracle
- oracle檢視物件DDL語句Oracle物件
- Oracle獲取資料庫中的物件建立語句Oracle資料庫物件
- 獲取oracle sql語句詳細些執行計劃OracleSQL
- 獲取表空間DDL
- Oracle vs PostgreSQL,研發注意事項(2)-DDL語句與事務OracleSQL
- Laravel 獲取執行的sql語句LaravelSQL
- PostgreSQL 函式獲取表DDLSQL函式
- 利用DBMS_METADATA包獲取許可權資訊(轉)
- 6.3. 基本SQL語句——6.3.1. DDLSQL
- MySQL入門---(一)SQL的DDL語句MySql
- Mysql 獲取表設計查詢語句MySql
- ORACLE常用語句:Oracle
- PostgreSQL獲取建表語句儲存過程SQL儲存過程
- MySQL 執行DDL語句 hang住了怎麼辦?MySql
- Oracle基本SQL語句OracleSQL
- Oracle 建立序列語句Oracle
- MogDB/openGauss學習筆記-獲取物件DDL筆記物件
- ORACLE 資料庫 查詢語句與DML語句Oracle資料庫
- SQL查詢語句 (Oracle)SQLOracle
- 利用WITH MOVE語句獲取資料庫邏輯檔名BG資料庫
- DBeave如何檢視資料庫表的DDL建立語句資料庫
- mysql中建庫、建表、增刪改查DDL語句MySql
- ORACLE多表關聯UPDATE語句Oracle
- oracle中的條件語句Oracle
- Oracle資料庫語句大全Oracle資料庫
- 列出oracle dbtime得sql語句OracleSQL
- 【LOB】Oracle lob管理常用語句Oracle
- oracle語句練習--初級Oracle
- oracle資料庫常用語句Oracle資料庫
- php一句話獲取獲取檔案目錄的方法PHP
- ORACLE結構化查詢語句Oracle
- 後臺執行SQL語句(oracle)SQLOracle
- Oracle SQL精妙SQL語句講解OracleSQL
- 【ERROR】JOB執行DDL語句報錯ORA-06550 & PLS-00103Error
- Oracle執行語句跟蹤 使用sql trace實現語句追蹤OracleSQL
- oracle-資料庫- insert 插入語句Oracle資料庫
- java連線oracle執行sql語句JavaOracleSQL
- 【SCRIPT】Oracle表管理段管理常用語句Oracle