Use dbms_metadata.get_ddl to view DDL statements

Source: Internet
Author: User

You can use the package dbms_metadata.get_ddl to view a table or table space DDL.

Detailed parameters of the get_ddl function in the dbms_metadata Package
DT 'vx! The g & y9cA0GET_DDL function returns the DDL statement of the original data of the created object. The detailed parameters are as follows:
F # fT g & j0 -- object_type --- object type of the DDL statement to return the original data
U d5b '{I} F0 -- name --- Object name
7jj x (R} zq0 -- schema --- Schema of the object. The default value is SchemaITPUB's personal space where the current user is located @ v6FO '+ d! W * N
-- Version --- version of the original data of the object ITPUB personal space q y] YH [z
-- Model --- the default data type is ORACLE.
[A 'P' qa0 -- transform.-XSL-T transform. to be applied.
! Q8fX aT nX | 0 -- RETURNS: the original data of the object is returned as CLOB by default.

Get_ddl function definition in the dbms_metadata PackageITPUB personal space IT * Qh & tgbk ^ 'd7 _
FUNCTION get_ddl (object_type IN VARCHAR2,
Mw ^ & D % m? TR '0name IN VARCHAR2,
F * Vp aO p9u0schema IN VARCHAR2 default null, ITPUB personal space G7aG h & yg2T; M
Version IN VARCHAR2 DEFAULT 'compuble ',
) _ F % j * X "Nz8D: | W_0model IN VARCHAR2 DEFAULT 'oracle ',
B7q0k (o) F. bY & H0transform. IN VARCHAR2 DEFAULT 'ddler') return clob; ITPUB personal space Ri * rX 'H' x4 _

NOTE: If sqlplus is used, you need to format the following items, especially long. Otherwise, the complete format cannot be displayed.SQL

Set linesize 180ITPUB personal space # AMIi HY
Sets pages 999
2viG4YT0n ~ & T0set long 90000

View the SQL statement used to create a user table

View SQLITPUB personal space 3Is gS of the current user table) RP! B8}
Select dbms_metadata.get_ddl ('table', 'ployees') from dual;
8dUz & eBe0 view other SQL statements using tables or Indexes

SELECT DBMS_METADATA.GET_DDL ('table', 'dept', 'Scott ') from dual;

View the SQL statement used to create a user IndexITPUB personal space n -~ Cpr P "Pe2Z9A
View the index of the expected table
G # @ R (FI j1y_0SQL> select INDEX_NAME, INDEX_TYPE, TABLE_NAME from user_indexes WHERE table_name = 'emp'; ITPUB personal space + n t n; D2d ~! R] Xq0P
View the current user index SQL
8Q5 ^ 7C: F; M $ wt0select dbms_metadata.get_ddl ('index', 'pk _ DEPT ') from dual;

View SQLITPUB's personal space P4Q # \ 5e indexed by other users! W; RL. H & S
Select dbms_metadata.get_ddl ('index', 'pk _ dept ', 'Scott') from dual; itpub personal space 9g8b, d9x9 \, B 'H
View the SQL statement used to create a primary keyItpub personal space: | & dbom (NV ~ NT
View the constraints of the required table. The personal space of itpub is Hok l0e & F #~
SQL> select owner, table_name, constraint_name, constraint_type from user_constraints where table_name = 'emp'; itpub personal space S * [+ z; G. y8v7v
View the SQL statement used to create a primary key
* Hw gc, p5mr0select dbms_metadata.get_ddl ('straint', 'emp_pk ') from dual;
NJ _ v ~ P: P kh0 view sqlitpub personal space z5e 'zm3t (B RP
SQL> select dbms_metadata.get_ddl ('ref _ constraint ', 'emp_fk_dept') from dual;
# BT * XCP "d ^ V0VIEW the statement for creating a VIEWItpub personal space! U. H [d) THP
View the sqlitpub personal space & G "k7b \ 0wl in the current user view
SQL> select dbms_metadata.get_ddl ('view', 'My _ tables ')

View sqlitpub personal space a * coj L in other user views
SQL> select dbms_metadata.get_ddl ('view', 'My _ tables ', 'Scott') from dual; itpub personal space ~ Ed 'W
You can also view the SQL statement used to create a view.
/AC % [b3y & UN ^ % x * ^ 0sql> select text from user_views where view_name = upper ('& view_name ');
J8ax5oqp "W" W0Some tips for using DBMS_METADATA.GET_DDLItpub personal space! V-JZ SK] 9u.
1. Obtain the DDL statements of all tables, indexes, stored procedures, and functions of a user.
4 K ~ S4yd/b0select dbms_metadata.get_ddl (U. object_type, U. object_name) itpub personal space 1z % C # uzaz $ _ $] 1 p
From user_objects uitpub personal space IDL muc ^ n l ~ 8 ?! E, ^
Where U. object_type in ('table', 'index', 'Procedure ', 'function ');
I3t: JG vs_y02. Get the DDL statement of all tablespaces. itpub personal space 6 ^ z4g! A k, nc9zx
Select dbms_metadata.get_ddl ('tablespace', ts. tablespace_name) itpub personal space 2a & F? NOx '^
From dba_tablespaces ts; itpub personal space? 7xhvy-c8n, H/u
3. Obtain the DDL statements of all created users.
4ph4dmsp s0select dbms_metadata.get_ddl ('user', U. username)
) A * B + Yu mleo! R0from dba_users U; itpub personal space jovrbdv}
4. Remove redundant parameters such as storage.
7 \ VWOd6V0EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM (DBMS_METADATA.SESSION_TRANSFORM, 'store', false );
| % \ T1 ["w0? F6l + Z0

Common Errors

SQL> select dbms_metadata.get_ddl ('table', 'pc', 'Scott ') from dual;
L8} p4L ''? JL8lM0ERROR:
3GU % x9q. U + e? VD0ORA-19206: Invalid value for query or ref cursor parameterITPUB personal space * v; r _] 3 | s
ORA-06512: at "SYS. DBMS_XMLGEN", line 83ITPUB personal space dGDi5J8t! JgR
ORA-06512: at "SYS. DBMS_METADATA", line 345ITPUB personal space K + | WVD
ORA-06512: at "SYS. DBMS_METADATA", line 410
, T j. | # e1m/\ Gl0ORA-06512: at "SYS. DBMS_METADATA", line 449
$ L! A) k'ifp @ ZN: a0ORA-06512: at "SYS. DBMS_METADATA", line 615
7L | "~ & [_ * H3Y3 ~ $ Z; T. 't0ora-06512: at "SYS. DBMS_METADATA", line 1221
^ Q rGi Sb $ gT. R0ORA-06512: at line 1

No rows selected

Solution: run $ ORACLE_HOME/rdbms/admin/catmeta. SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.