您的位置:首页 > 数据库 > Oracle

关于导出oracle多个表的建表语句DLL,生成.sql语句。

2015-01-19 10:10 477 查看
--('TABLE','LINE','ODS_XX')这里面的表和用户都需要大写。
如果表名用户名不大写会报这个错误:对象 "emp" 属于类型 TABLE, 在方案 "scott" 中未找到
亲测的代码:
set long 90000;
set linesize 4000;
set pagesize 4000;
spool table.sql
select dbms_metadata.get_ddl('TABLE','LINE','ODS_XX') from dual;
spool off

模仿plsql导出用户对象的详细的代码:

set pagesize 0
set long 90000
set feedback off
set echo off
--数据库登录
conn testgdzc/testgdzc@testjjgdzc
--导出文件名称
spool E:\table.sql
--输出信息采用缩排或换行格式化
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'PRETTY', TRUE);
--确保每个语句都带分号
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'SQLTERMINATOR', TRUE);
--关闭表索引、外键等关联(后面单独生成)
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'CONSTRAINTS', FALSE);
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'REF_CONSTRAINTS', FALSE);
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'CONSTRAINTS_AS_ALTER', FALSE);
--关闭存储、表空间属性
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'STORAGE', FALSE);
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'TABLESPACE', FALSE);
--关闭创建表的PCTFREE、NOCOMPRESS等属性
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'SEGMENT_ATTRIBUTES', FALSE);
select dbms_metadata.get_ddl('TABLE','PROJECT_STAGE_CONFIG') from dual;
select dbms_metadata.get_ddl('TABLE','PROJECT_LIBRARY_TEST') from dual;
select dbms_metadata.get_ddl('TABLE','PROJECT_UNIT_CONFIG') from dual;
--获取表注释
SELECT DBMS_LOB.substr(DBMS_METADATA.get_dependent_ddl ('COMMENT', 'PROJECT_STAGE_CONFIG'))
FROM (SELECT distinct 'PROJECT_STAGE_CONFIG'
FROM user_col_comments
WHERE comments IS NOT NULL
)
;
spool off;

alter table SP_PD_ANTI_ACCIDENT add(gsdm varchar2(6),jlsj date);

1、一次想查询表附加日志:
dbms_metadata.get_ddl('TABLE','表名','用户名') from dual;

2、报错如下:
ora-31603:对象“表名”属于类型 Table,在方案“用户名”中未找到

3、原因:
用户缺少相关数据字典视图的查询权限。

4、解决办法:
grant select_catalog_role to 用户名;

注:授权select any dictionary权限则报错依旧。

5、总结:

select any dictionary 与 select_catalog_role
相同之处:有了这两个中的一个,基本就可以查询数据字典
不同之处
1) select any dictionary是一种系统权限(system privilege),而select_catalog_role 是一种角色(a role)。
2) 角色的话需要重新登录或者显式的set role 来生效,而赋予系统权限是立即生效的。(P.S. 同样revoke权限也 是立即生效)
3) select_catalog_role可以查看一些数据字典的视图·(可以看role的定义),如dba_之类的,而select any
dictionary 可以查看sys的表,select_catalog_role看不到。

不单单是获取到了创建的基本字段的语句,还有主键,约束,外键,存储参数,表空间等,如果这些你不需要,都是可以进行过滤的,例如我过滤掉主键、外键、存储信息

使用如下语句:

exec DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'REF_CONSTRAINTS',false);
exec DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'CONSTRAINTS',false);
exec DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false);

Object Type

Name

Datatype

Meaning

All objects

PRETTY

BOOLEAN

If TRUE, format the output with indentation and line feeds. Defaults toTRUE.

All objects

SQLTERMINATOR

BOOLEAN

If TRUE, append a SQL terminator (; or /) to each DDL statement. Defaults to FALSE.

TABLE

SEGMENT_ATTRIBUTES

BOOLEAN

If TRUE, include segment attributes clauses in the DDL. If FALSE, omit them. Defaults to TRUE.

TABLE

STORAGE

BOOLEAN

If TRUE, include storage clauses in the DDL. If FALSE, omit them. Defaults to TRUE. (Ignored if SEGMENT_ATTRIBUTES is FALSE.)

TABLE

TABLESPACE

BOOLEAN

If TRUE, include tablespace clauses in the DDL. If FALSE, omit them. (Ignored if SEGMENT_ATTRIBUTES is FALSE.) Defaults to TRUE.

TABLE

CONSTRAINTS

BOOLEAN

If TRUE, include all non-referential table constraints in the DDL. If FALSE, omit them. Defaults to TRUE.

TABLE

REF_CONSTRAINTS

BOOLEAN

If TRUE, include all referential constraints (foreign keys) in the DDL. IfFALSE, omit them. Defaults to TRUE.

TABLE

CONSTRAINTS_AS_ALTER

BOOLEAN

If TRUE, include table constraints as separate ALTER TABLE (and, if necessary, CREATE INDEX) statements. If FALSE, specify table constraints as part of the CREATE TABLE statement. Defaults to FALSE. Requires thatCONSTRAINTS be TRUE.

TABLE

OID

BOOLEAN

If TRUE, include the OID clause for object tables in the DDL. If FALSE, omit it. Defaults to FALSE.

TABLE

SIZE_BYTE_KEYWORD

BOOLEAN

If TRUE, include the BYTE keyword as part of the size specification ofCHAR and VARCHAR2 columns that use byte semantics. If FALSE, omit the keyword. Defaults to FALSE.

TABLE, INDEX

PARTITIONING

BOOLEAN

If TRUE, include partitioning clauses in the DDL. If FALSE, omit them. Defaults to TRUE.

INDEX, CONSTRAINT,ROLLBACK_SEGMENT,CLUSTER, TABLESPACE

SEGMENT_ATTRIBUTES

BOOLEAN

If TRUE, include segment attributes clauses (physical attributes, storage attributes, tablespace, logging) in the DDL. If FALSE, omit them. Defaults to TRUE.

INDEX, CONSTRAINT,ROLLBACK_SEGMENT, CLUSTER

STORAGE

BOOLEAN

If TRUE, include storage clauses in the DDL. If FALSE, omit them. (Ignored if SEGMENT_ATTRIBUTES is FALSE.) Defaults to TRUE.

INDEX, CONSTRAINT,ROLLBACK_SEGMENT, CLUSTER

TABLESPACE

BOOLEAN

If TRUE, include tablespace clauses in the DDL. If FALSE, omit them. (Ignored if SEGMENT_ATTRIBUTES is FALSE.) Defaults to TRUE.

TYPE

SPECIFICATION

BOOLEAN

If TRUE, include the type specification in the DDL. If FALSE, omit it. Defaults to TRUE.

TYPE

BODY

BOOLEAN

If TRUE, include the type body in the DDL. If FALSE, omit it. Defaults toTRUE.

TYPE

OID

BOOLEAN

If TRUE, include the OID clause in the DDL. If FALSE, omit it. Defaults toFALSE.

PACKAGE

SPECIFICATION

BOOLEAN

If TRUE, include the package specification in the DDL. If FALSE, omit it. Defaults to TRUE.

PACKAGE

BODY

BOOLEAN

If TRUE, include the package body in the DDL. If FALSE, omit it. Defaults to TRUE.

VIEW

FORCE

BOOLEAN

If TRUE, use the FORCE keyword in the CREATE VIEW statement. If FALSE, do not use the FORCE keyword in the CREATE VIEW statement. Defaults toTRUE.

OUTLINE

INSERT

BOOLEAN

If TRUE, include the INSERT statements into the OL$ dictionary tables that will create the outline and its hints. If FALSE, omit a CREATE OUTLINEstatement. Defaults to FALSE.

Note: This object type is being deprecated.

All objects

DEFAULT

BOOLEAN

Calling SET_TRANSFORM_PARAM with this parameter set to TRUE has the effect of resetting all parameters for the transform to their default values. Setting this FALSE has no effect. There is no default.

All objects

INHERIT

BOOLEAN

If TRUE, inherits session-level parameters. Defaults to FALSE. If an application calls ADD_TRANSFORM to add the DDL transform, then by default the only transform parameters that apply are those explicitly set for that transform handle. This has no effect if the transform handle is the session transform handle.

ROLE

REVOKE_FROM

Text

The name of a user from whom the role must be revoked. If this is a non-null string and if the CREATE ROLE statement grants you the role, aREVOKE statement is included in the DDL after the CREATE ROLEstatement.

Note: When you issue a CREATE ROLE statement, Oracle may grant you the role. You can use this transform parameter to undo the grant.

Defaults to null string.

TABLESPACE

REUSE

BOOLEAN

If TRUE, include the REUSE parameter for datafiles in a tablespace to indicate that existing files can be reused. If FALSE, omit the REUSEparameter.

Defaults to FALSE.

CLUSTER, INDEX,ROLLBACK_SEGMENT, TABLE,TABLESPACE

PCTSPACE

NUMBER

A number representing the percentage by which space allocation for the object type is to be modified. The value is the number of one-hundreths of the current allocation. For example, 100 means 100%.

If the object type is TABLESPACE, the following size values are affected:

- in file specifications, the value of SIZE

- MINIMUM EXTENT

- EXTENT MANAGEMENT LOCAL UNIFORM SIZE

For other object types, INITIAL and NEXT are affected.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: