DBMS_METADATA包获得对象DDL语句

DBMS_METADATA包获得对象DDL语句


如何利用系统包DBMS_METADATA包中的GET_DDL函数来获取对象的定义语句。下面是该函数的入参和出参:

SQL> DESC DBMS_METADATA.GET_DDL

PARAMETER   TYPE     MODE DEFAULT?

----------- -------- ---- --------

(RESULT)    CLOB                   

OBJECT_TYPE VARCHAR2 IN            

NAME        VARCHAR2 IN            

SCHEMA      VARCHAR2 IN   Y        

VERSION     VARCHAR2 IN   Y        

MODEL       VARCHAR2 IN   Y        

TRANSFORM   VARCHAR2 IN   Y        

其详细参数如下:

l OBJECT_TYPE  需要返回原数据的DDL语句的对象类型

l NAME  对象名称

l SCHEMA  对象所在的SCHEMA,默认为当前用户所在所SCHEMA

l VERSION  对象原数据的版本

l MODEL  原数据的类型默认为ORACLE

l TRANSFORM  默认值为DDL

l RETURNS  对象的原数据默认以CLOB类型返回

一般情况下,只需要给出OBJECT_TYPE、NAME和SCHEMA3个参数即可。

n 查看创建表SQL语句:

SELECT DBMS_METADATA.GET_DDL('TABLE','DEPT','SCOTT') FROM DUAL;

SELECT DBMS_METADATA.GET_DDL('TABLE',U.TABLE_NAME) FROM USER_TABLES U;

n 查看创建索引的SQL语句:

SELECT DBMS_METADATA.GET_DDL('INDEX','PK_DEPT','SCOTT') FROM DUAL;

SELECT DBMS_METADATA.GET_DDL('INDEX',U.INDEX_NAME) FROM USER_INDEXES U;

n 查看创建主键的SQL语句:

SELECT DBMS_METADATA.GET_DDL('CONSTRAINT','EMP_PK') FROM DUAL;

n 查看创建外键的SQL语句:

SELECT DBMS_METADATA.GET_DDL('REF_CONSTRAINT','EMP_FK_DEPT') FROM DUAL;

n 查看创建视图(VIEW)的SQL语句:

SELECT DBMS_METADATA.GET_DDL('VIEW', 'MY_TABLES','SCOTT') FROM DUAL;

SELECT DBMS_METADATA.GET_DDL('VIEW', U.OBJECT_NAME)

  FROM USER_OBJECTS U

 WHERE OBJECT_TYPE = 'VIEW';

SELECT TEXT FROM USER_VIEWS WHERE VIEW_NAME=UPPER('&VIEW_NAME');

n 查看创建存储过程(PROCEDURE)的SQL语句:

SELECT DBMS_METADATA.GET_DDL('PROCEDURE', U.OBJECT_NAME)

  FROM USER_OBJECTS U

 WHERE OBJECT_TYPE = 'PROCEDURE';

n 查看创建触发器(TRIGGER)的SQL语句:

SELECT DBMS_METADATA.GET_DDL('TRIGGER', U.OBJECT_NAME)

  FROM USER_OBJECTS U

 WHERE OBJECT_TYPE = 'TRIGGER';

n 查看创建函数(FUNCTION)的SQL语句:

SELECT DBMS_METADATA.GET_DDL('FUNCTION', U.OBJECT_NAME)

  FROM USER_OBJECTS U

 WHERE OBJECT_TYPE = 'FUNCTION';

n 查看创建包(PACKAGE)的SQL语句:

SELECT DBMS_METADATA.GET_DDL('PACKAGE', U.OBJECT_NAME)

  FROM USER_OBJECTS U

 WHERE OBJECT_TYPE = 'PACKAGE';

n 查看创建序列(SEQUENCE)的SQL语句:

SELECT DBMS_METADATA.GET_DDL('SEQUENCE', U.OBJECT_NAME)

  FROM USER_OBJECTS U

 WHERE OBJECT_TYPE = 'SEQUENCE';

n 查看创建同义词(SYNONYM)的SQL语句:

SELECT DBMS_METADATA.GET_DDL('SYNONYM', U.OBJECT_NAME)

  FROM USER_OBJECTS U

 WHERE OBJECT_TYPE = 'SYNONYM';

n 查看创建表空间(TABLESPACE)的SQL语句:

SELECT DBMS_METADATA.GET_DDL('TABLESPACE', U.TABLESPACE_NAME)

  FROM USER_TABLESPACES U;

n 查看创建角色(ROLE)的SQL语句:

SELECT DBMS_METADATA.GET_DDL('ROLE', U.ROLE) FROM DBA_ROLES U;

n 查看创建用户(USER)的SQL语句:

SELECT DBMS_METADATA.GET_DDL('USER','SYS') FROM DUAL;

n 得到某个SCHEDULER JOB的创建语句:

SELECT DBMS_METADATA.GET_DDL('PROCOBJ', D.JOB_NAME, D.OWNER)

  FROM DBA_SCHEDULER_JOBS D

 WHERE D.JOB_TYPE = 'STORED_PROCEDURE'

   AND D.STATE = 'SCHEDULED'

   AND D.SCHEDULE_NAME IS NULL;

n 得到一个用户下的所有表、索引、存储过程、函数的DDL语句:

SELECT DBMS_METADATA.GET_DDL(U.OBJECT_TYPE, U.OBJECT_NAME)

FROM   USER_OBJECTS U

WHERE  U.OBJECT_TYPE IN ('TABLE', 'INDEX', 'PROCEDURE', 'FUNCTION');

如果想去掉表的存储参数(例如,INITIAL、NEXT、FREELISTS等参数),那么可以使用DBMS_METADATA包中的函数SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',FALSE)来完成,代码如下所示:

SYS@lhrdb> SELECT DBMS_METADATA.GET_DDL('TABLE','DEPT','SCOTT') FROM DUAL;

DBMS_METADATA.GET_DDL('TABLE','DEPT','SCOTT')

--------------------------------------------------------------------------------

  CREATE TABLE "SCOTT"."DEPT"

   (    "DEPTNO" NUMBER(2,0),

        "DNAME" VARCHAR2(14),

        "LOC" VARCHAR2(13),

         CONSTRAINT "PK_DEPT" PRIMARY KEY ("DEPTNO")

  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS

  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645

  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1

  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)

  TABLESPACE "USERS"  ENABLE

   ) SEGMENT CREATION IMMEDIATE

  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 FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)

  TABLESPACE "USERS"

SYS@lhrdb> EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',FALSE);

PL/SQL procedure successfully completed.

SYS@lhrdb> SELECT DBMS_METADATA.GET_DDL('TABLE','DEPT','SCOTT') FROM DUAL;

DBMS_METADATA.GET_DDL('TABLE','DEPT','SCOTT')

--------------------------------------------------------------------------------

  CREATE TABLE "SCOTT"."DEPT"

   (    "DEPTNO" NUMBER(2,0),

        "DNAME" VARCHAR2(14),

        "LOC" VARCHAR2(13),

         CONSTRAINT "PK_DEPT" PRIMARY KEY ("DEPTNO")

  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS

  TABLESPACE "USERS"  ENABLE

   ) SEGMENT CREATION IMMEDIATE

  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255

 NOCOMPRESS LOGGING

  TABLESPACE "USERS"

使用DBMS_METADATA.GET_DDL需要注意以下问题:

(1)DBMS_METADATA.GET_DDL()包内的参数都要大写,否则会报ORA-31600: invalid input value table for parameter OBJECT_TYPE in function GET_DDL的错误。

(2)是否查的当前用户的DDL语句,若不是则需要加上对象的属主信息即SCHEMA参数。

(3)若在SQL*Plus中显示不全,则需要set long 9999。

现在来回答本题目中的问题,代码如下所示:

SET PAGESIZE 0

SET TRIMSPOOL ON

SET LINESIZE 10000

SET LONG 90000

SET FEEDBACK OFF

SET FEED OFF;

SET ECHO OFF

SPOOL SCHEMA_SCOTT.SQL

SELECT DBMS_METADATA.GET_DDL(U.OBJECT_TYPE, U.OBJECT_NAME,U.OWNER)

FROM   DBA_OBJECTS U

WHERE  U.OBJECT_TYPE IN ('TABLE', 'INDEX', 'PROCEDURE', 'FUNCTION','PACKAGE','TRIGGER')

AND U.OWNER='SCOTT';

SPOOL OFF;

 

可以使用如下的SQL脚本生成某个用户下的所有对象的DDL语句:

sqlplus<<EOF

set long 100000

set head off

set echo off

set pagesize 0

set verify off

set feedback off

spool schema.out

 

select dbms_metadata.get_ddl(object_type, object_name, owner)

from

(

    --Convert DBA_OBJECTS.OBJECT_TYPE to DBMS_METADATA object type:

    select

        owner,

        --Java object names may need to be converted with DBMS_JAVA.LONGNAME.

        --That code is not included since many database don't have Java installed.

        object_name,

        decode(object_type,

            'DATABASE LINK',      'DB_LINK',

            'JOB',                'PROCOBJ',

            'RULE SET',           'PROCOBJ',

            'RULE',               'PROCOBJ',

            'EVALUATION CONTEXT', 'PROCOBJ',

            'PACKAGE',            'PACKAGE_SPEC',

            'PACKAGE BODY',       'PACKAGE_BODY',

            'TYPE',               'TYPE_SPEC',

            'TYPE BODY',          'TYPE_BODY',

            'MATERIALIZED VIEW',  'MATERIALIZED_VIEW',

            'QUEUE',              'AQ_QUEUE',

            'JAVA CLASS',         'JAVA_CLASS',

            'JAVA TYPE',          'JAVA_TYPE',

            'JAVA SOURCE',        'JAVA_SOURCE',

            'JAVA RESOURCE',      'JAVA_RESOURCE',

            object_type

        ) object_type

    from dba_objects

    where owner in ('LHR')

        --These objects are included with other object types.

        and object_type not in ('INDEX PARTITION','INDEX SUBPARTITION',

           'LOB','LOB PARTITION','LOB SUBPARTITION','TABLE PARTITION','TABLE SUBPARTITION','PROGRAM')

        --Ignore system-generated types that support collection processing.

        and not (object_type = 'TYPE' and object_name like 'SYS_PLSQL_%')

        --Exclude nested tables, their DDL is part of their parent table.

        and (owner, object_name) not in (select owner, table_name from dba_nested_tables)

        --Exlclude overflow segments, their DDL is part of their parent table.

        and (owner, object_name) not in (select owner, table_name from dba_tables where iot_type = 'IOT_OVERFLOW')

)

order by owner, object_type, object_name;

 

 

spool off

quit

EOF

 

cat schema.out|sed 's/OWNER1/MYOWNER/g'>schema.out.change.sql

 
 
 

通过系统包DBMS_METADATA.GET_DDL也可以获取用户的权限信息,如下所示:

SELECT DBMS_METADATA.GET_DDL('USER', 'LHR') DDL_SQL FROM DUAL

UNION ALL

SELECT DBMS_METADATA.GET_GRANTED_DDL('OBJECT_GRANT', 'LHR') FROM DUAL

UNION ALL

SELECT DBMS_METADATA.GET_GRANTED_DDL('ROLE_GRANT', 'LHR') FROM DUAL

UNION ALL

SELECT DBMS_METADATA.GET_GRANTED_DDL('SYSTEM_GRANT', 'LHR') FROM DUAL;

将结果拷贝出来简单的用文本编辑工具编辑后即可运行。

 
 
 
 
 




 
   
 

Oracle9i新特性-使用DBMS_METADATA包获得对象DDL语句


从Oracle9i开始Oracle提供了一个新的系统包DBMS_METADATA,可以用于提取对象创建的DDL语句。

这个Package功能极其强大,我们来看看它的使用方法.

1.获得表的创建语句.

2.进一步的,可以通过dbms_metadata获得更全面的DDL语句

Oracle提供一个全面的范例:
$ORACLE_HOME/rdbms/demo/mddemo.sql
包括索引、授权、触发器等所有DDL语句都可以被提取。

有兴趣的可以参考该文件,按照Demo的例子进行测试。

 
 

1.得到一个表的ddl语句:

SET SERVEROUTPUT ON
SET LINESIZE 1000
SET FEEDBACK OFF
set long 999999      ------显示不完整
SET PAGESIZE 1000    ----分页  

   

EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false);   

---去除storage等多余参数  

   

SELECT DBMS_METADATA.GET_DDL('TABLE','TCC_NE_FRAME') FROM DUAL;  

SELECT DBMS_METADATA.GET_DDL('TABLE','TCC_NE_SNAP') FROM DUAL;  

   

2.得到一个用户下的所有表,索引,存储过程的ddl  

   

SET SERVEROUTPUT ON
SET LINESIZE 1000
SET FEEDBACK OFF
set long 999999  ------显示不完整
SET PAGESIZE 1000  ----分页
---去除storage等多余参数
EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false);  

   

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语句  

   

SET SERVEROUTPUT ON
SET LINESIZE 1000
SET FEEDBACK OFF
set long 999999     ------显示不完整
SET PAGESIZE 1000   ----分页  


---去除storage等多余参数   

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


4.得到所有创建用户的ddl  

   


SET SERVEROUTPUT ON
SET LINESIZE 1000
SET FEEDBACK OFF
set long 999999        ------显示不完整
SET PAGESIZE 1000      ----分页  


---去除storage等多余参数   

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

另外,若执行不了该包,则需要正确安装好相应的包。  



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=2041672

 

******************************************************

常见错误1:

ORA-39212: 安装错误: 未正确加载 XSL 样式表
ORA-06512: 在 "SYS.DBMS_SYS_ERROR", line 79
ORA-06512: 在 "SYS.DBMS_METADATA_INT", line 7398
ORA-06512: 在 "SYS.DBMS_METADATA_INT", line 7447
ORA-06512: 在 "SYS.DBMS_METADATA_INT", line 9453
ORA-06512: 在 "SYS.DBMS_METADATA", line 1919
ORA-06512: 在 "SYS.DBMS_METADATA", line 2792
ORA-06512: 在 "SYS.DBMS_METADATA", line 4333
ORA-06512: 在 line 1

 

ORA-06502: PL/SQL: 数字或值错误
ORA-31605: the following was returned from LpxXSLResetAllVars in routine kuxslResetParams:
LPX-1: NULL pointer
ORA-06512: 在 "SYS.UTL_XML", line 246
ORA-06512: 在 "SYS.DBMS_METADATA_INT", line 7511
ORA-06512: 在 "SYS.DBMS_METADATA_INT", line 9453
ORA-06512: 在 "SYS.DBMS_METADATA", line 1919
ORA-06512: 在 "SYS.DBMS_METADATA", line 2792
ORA-06512: 在 "SYS.DBMS_METADATA", line 4333
ORA-06512: 在 line 1

 

【解决办法】:

如果遇到类似的错误,需要 http://metalink.oracle.com 站点下载编号为2736436的补丁。

 

常见错误2:

SQL> select dbms_metadata.get_ddl('TABLE','PC','SCOTT') from dual;
ERROR:
ORA-19206: Invalid value for query or REF CURSOR parameter
ORA-06512: at "SYS.DBMS_XMLGEN", line 83
ORA-06512: at "SYS.DBMS_METADATA", line 345
ORA-06512: at "SYS.DBMS_METADATA", line 410
ORA-06512: at "SYS.DBMS_METADATA", line 449
ORA-06512: at "SYS.DBMS_METADATA", line 615
ORA-06512: at "SYS.DBMS_METADATA", line 1221
ORA-06512: at line 1



no rows selected

SQL>

解决办法:运行$ORACLE_HOME/rdbms/admin/catmeta.sql

*****************************************************

常见错误3:  

今天在一个数据库上执行DBMS_METADATA包的时候,出现了ORA-19206的错误。

 


具体错误信息如下:

SQL> SELECT DBMS_METADATA.GET_DDL('MATERIALIZED_VIEW', 'CAT_AUTH_GMP', 'NDMAIN') FROM DUAL;
ERROR:
ORA-19206: Invalid value for query or REF CURSOR parameter
ORA-06512: at "SYS.DBMS_XMLGEN", line 83
ORA-06512: at "SYS.DBMS_METADATA", line 345
ORA-06512: at "SYS.DBMS_METADATA", line 410
ORA-06512: at "SYS.DBMS_METADATA", line 449
ORA-06512: at "SYS.DBMS_METADATA", line 615
ORA-06512: at "SYS.DBMS_METADATA", line 1221
ORA-06512: at line 1

no rows selected

仔细检查了一下,发现所有和物化视图相关的表在执行DBMS_METADATA.GET_DDL时都会出现上面的错误,而其他的对象并没有受到影响。

由于这个数据库的数据是通过EXP/IMP迁移得到的,虽然在IMP过程中没有什么明显的错误信息,但是仍然怀疑是EXP/IMP造成的数据字典出现了错误。

查询了一下METALINK信息,发现了很多类似的错误,而Oracle给出的解决方法是重装XDB。

于是尝试重装XML方案:

首先以SYSDBA身份登陆,关闭并重启数据库,为卸载XML对象做准备:

SQL> CONN / AS SYSDBA
Connected.
SQL> SHUTDOWN IMMEDIATE
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> STARTUP
ORACLE instance started.

Total System Global Area 5876197568 bytes
Fixed Size 739520 bytes
Variable Size 503316480 bytes
Database Buffers 5368709120 bytes
Redo Buffers 3432448 bytes
Database mounted.
Database opened.
SQL> SET ECHO ON
SQL> SPO XDB_REMOVAL.LOG
SQL> @?/rdbms/admin/catnoqm.sql
SQL> Rem
SQL> Rem $Header: catnoqm.sql 03-jan-2002.17:32:31 spannala Exp $
SQL> Rem
SQL> Rem catnoqm.sql
SQL> Rem
SQL> Rem Copyright (c) 2001, 2002, Oracle Corporation. All rights reserved.
SQL> Rem
SQL> Rem NAME
SQL> Rem catnoqm.sql - CATalog script for removing (NO) XDB
SQL> Rem
SQL> Rem DESCRIPTION
SQL> Rem this script drops the metadata created for SQL XML management
SQL> Rem This scirpt must be invoked as sys. It is to be invoked as
SQL> Rem
SQL> Rem @@catnoqm
SQL> Rem NOTES
SQL> Rem
SQL> Rem MODIFIED (MM/DD/YY)
SQL> Rem spannala 01/03/02 - tables are not handled by xdb
SQL> Rem spannala 01/02/02 - registry
SQL> Rem spannala 12/20/01 - passing in the resource tablespace name
SQL> Rem tsingh 11/17/01 - remove connection string
SQL> Rem tsingh 06/30/01 - XDB: XML Database merge
SQL> Rem amanikut 02/13/01 - Creation
SQL> Rem
SQL> Rem
SQL>
SQL> execute dbms_registry.removing('XDB');
BEGIN dbms_registry.removing('XDB'); END;

*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "SYS.DBMS_REGISTRY", line 420
ORA-06512: at line 1


SQL> drop user xdb cascade;

User dropped.

SQL> SPO OFF

从上面脚本调用的结果可以确定,XML对象在执行导入时没有注册成功,可能这就是导致错误产生的原因。

保证JAVA池和共享池都大于150M,且XDB表空间可扩展,或者大于150M。则可以重启系统。

SQL> SHUTDOWN IMMEDIATE
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> STARTUP
ORACLE instance started.

Total System Global Area 5876197568 bytes
Fixed Size 739520 bytes
Variable Size 503316480 bytes
Database Buffers 5368709120 bytes
Redo Buffers 3432448 bytes
Database mounted.
Database opened.

下面重新安装XML方案:

SQL> SET ECHO ON
SQL> SPO XDB_INSTALL.LOG
SQL> @?/rdbms/admin/catqm.sql XDBPASSWD XDB TEMP

调用catqm.sql时,后面的三个参数依次时XDB用户的密码、默认表空间和临时镖客。

SQL> @?/rdbms/admin/catxdbj.sql

然后继续执行上面的SQL:

然后检查XDB用户是否存在错误对象,以及XDB是否注册到数据库中:

SQL> SELECT COUNT(*) FROM DBA_OBJECTS WHERE OWNER = 'XDB' AND STATUS = 'INVALID';

COUNT(*)
----------
0

SQL> SELECT COMP_NAME, STATUS, VERSION FROM DBA_REGISTRY WHERE COMP_NAME = 'Oracle XML Database';

COMP_NAME STATUS VERSION
---------------------------------------- ----------- --------------------
Oracle XML Database VALID 9.2.0.4.0

下面重启数据库和监听,XDB重装完毕。

重建XDB之后,对DBMS_METADATA的访问恢复正常:

SQL> SELECT DBMS_METADATA.GET_DDL('MATERIALIZED_VIEW', 'CAT_AUTH_GMP', 'NDMAIN') FROM DUAL;

DBMS_METADATA.GET_DDL('MATERIALIZED_VIEW','CAT_AUTH_GMP','NDMAIN')
------------------------------------------------------------------

CREATE MATERIALIZED VIEW "NDMAIN"."CAT_AUTH_GMP"
ORGANIZATION HEAP PCTFREE

 
 
 

dbms_metadata是oracle提供的从数据字典中获取数据库对象元数据(也就是对象的原始定义)的包。在这以前DBA还可以通过创建自定义SQL来获取对象定义,这个包的出现可以大大减少我们的工作。这个包提供了很多有用的工具,10g对这个包进行了更新加强,增加了许多新功能。

常用的过程有dbms_metadata.get_ddl或dbms_metadata.get_xml(对象定义语句以XML的格式返回), 还可以利用dbms_granted_ddl或dbms_granted_xml获取对象的权限信息

获取tablespace定义

SQL> set linesize 200
SQL> set pagesize 990
SQL> set long 1000
SQL> select dbms_metadata.get_ddl('TABLESPACE',tablespace_name) from dba_tablespaces;

DBMS_METADATA.GET_DDL('TABLESP
--------------------------------------------------------------------------------

CREATE TABLESPACE "SYSTEM" DATAFILE
'/u02/oradata/idctest/test/datafile/o1_mf_system_3142lbps_.dbf' SIZE 262144000
AUTOEXTEND ON NEXT 10485760 MAXSIZE 32767M
LOGGING ONLINE PERMANENT BLOCKSIZE 8192
EXTENT MANAGEMENT LOCAL AUTOALLOCATE SEGMENT SPACE MANAGEMENT MANUAL


CREATE UNDO TABLESPACE "UNDOTBS1" DATAFILE
'/u02/oradata/idctest/test/datafile/o1_mf_undotbs1_3142lbq6_.dbf' SIZE 209715200
AUTOEXTEND ON NEXT 10485760 MAXSIZE 32767M
BLOCKSIZE 8192
EXTENT MANAGEMENT LOCAL AUTOALLOCATE

..........

获取用户定义

SQL> select dbms_metadata.get_ddl('USER',username) from dba_users;

DBMS_METADATA.GET_DDL('USER',U
--------------------------------------------------------------------------------

CREATE USER "HR" IDENTIFIED BY VALUES '01C6FA40E1437B28'
DEFAULT TABLESPACE "HRD"
TEMPORARY TABLESPACE "TEMP"


CREATE USER "OSC" IDENTIFIED BY VALUES '1EAF99B140949974'
DEFAULT TABLESPACE "OSCD"
TEMPORARY TABLESPACE "TEMP"


CREATE USER "SYSMAN" IDENTIFIED BY VALUES '447B729161192C24'
DEFAULT TABLESPACE "SYSAUX"
TEMPORARY TABLESPACE "TEMP"
................

................

获取表的定义

SQL> select dbms_metadata.get_ddl('TABLE','TEST') from dual;

DBMS_METADATA.GET_DDL('TABLE',
--------------------------------------------------------------------------------

CREATE TABLE "IQUEWEB"."TEST"
( "A" CHAR(4),
"B" VARCHAR2(10),
"ADDRESS" VARCHAR2(200)
) 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> select dbms_metadata.get_granted_ddl('SYSTEM_GRANT') from dual;

DBMS_METADATA.GET_GRANTED_DDL(
--------------------------------------------------------------------------------

GRANT UNLIMITED TABLESPACE TO "IQUEWEB"


SQL> select dbms_metadata.get_granted_ddl('OBJECT_GRANT') from dual;

DBMS_METADATA.GET_GRANTED_DDL(
--------------------------------------------------------------------------------

GRANT DELETE ON "INTERINFO"."IQUE_USER_BASE" TO "IQUEWEB"

GRANT INSERT ON "INTERINFO"."IQUE_USER_BASE" TO "IQUEWEB"

GRANT SELECT ON "INTERINFO"."IQUE_USER_BASE" TO "IQUEWEB"

GRANT UPDATE ON "INTERINFO"."IQUE_USER_BASE" TO "IQUEWEB"

GRANT SELECT ON "INTERINFO"."IQUE_USER" TO "IQUEWEB"

GRANT REFERENCES ON "INTERINFO"."IQUE_FIN_PROJECT" TO "IQUEWEB"

GRANT SELECT ON "INTERINFO"."IQUE_FIN_PROJECT" TO "IQUEWEB"




利用DBMS_METADATA包获取权限信息     
     
 

Oracle的9i增加了DBMS_METADATA包,从此在需要得到对象的创建脚本时,不再需要通过查询多张系统视图去自己拼凑结果了。只需要调用这个包中的GET_DDL过程就可以轻松的获取对象的创建脚步。

不过很多人对DBMS_METADATA包的了解仅限与此。当需要获取用户的权限时,往往还是采用通过到数据字典中读取的方式,其实DBMS_METADATA包本身就支持获取权限信息。


DBMS_METADATA的GET_GRANTED_DDL过程可以用于获取用户的授权信息。Oracle支持的授权相关信息类型包括:OBJECT_GRANT、SYSTEM_GRANT、ROLE_GRANT、DEFAULT_ROLE、TABLESPACE_QUOTA和PROXY。

由于使用十分简单,这里给一个简单的例子,就不详细描述了:

SQL> CREATE USER TEST IDENTIFIED BY TEST DEFAULT TABLESPACE YANGTK;

用户已创建

SQL> GRANT CONNECT TO TEST;

授权成功。

SQL> GRANT SELECT ANY TABLE TO TEST;

授权成功。

SQL> GRANT INSERT, UPDATE ON T TO TEST;

授权成功。

SQL> ALTER USER TEST QUOTA 10M ON YANGTK;

用户已更改。

SQL> CREATE ROLE R1;

角色已创建

SQL> GRANT R1 TO TEST;

授权成功。

SQL> ALTER USER TEST DEFAULT ROLE R1;

用户已更改。

SQL> ALTER USER TEST GRANT CONNECT THROUGH YANGTK;

用户已更改。

SQL> SET LONG 10000
SQL> SELECT DBMS_METADATA.GET_GRANTED_DDL('OBJECT_GRANT', 'TEST') FROM DUAL;

DBMS_METADATA.GET_GRANTED_DDL('OBJECT_GRANT','TEST')
----------------------------------------------------------------------

GRANT UPDATE ON "YANGTK"."T" TO "TEST"

GRANT INSERT ON "YANGTK"."T" TO "TEST"

SQL> SELECT DBMS_METADATA.GET_GRANTED_DDL('SYSTEM_GRANT', 'TEST') FROM DUAL;

DBMS_METADATA.GET_GRANTED_DDL('SYSTEM_GRANT','TEST')
----------------------------------------------------------------------

GRANT SELECT ANY TABLE TO "TEST"

SQL> SELECT DBMS_METADATA.GET_GRANTED_DDL('ROLE_GRANT', 'TEST') FROM DUAL;

DBMS_METADATA.GET_GRANTED_DDL('ROLE_GRANT','TEST')
----------------------------------------------------------------------

GRANT "CONNECT" TO "TEST"

GRANT "R1" TO "TEST"

SQL> SELECT DBMS_METADATA.GET_GRANTED_DDL('DEFAULT_ROLE', 'TEST') FROM DUAL;

DBMS_METADATA.GET_GRANTED_DDL('DEFAULT_ROLE','TEST')
----------------------------------------------------------------------

ALTER USER "TEST" DEFAULT ROLE "R1"

SQL> SELECT DBMS_METADATA.GET_GRANTED_DDL('TABLESPACE_QUOTA', 'TEST') FROM DUAL;

DBMS_METADATA.GET_GRANTED_DDL('TABLESPACE_QUOTA','TEST')
----------------------------------------------------------------------

ALTER USER "TEST" QUOTA 640 ON "YANGTK"


SQL> SELECT DBMS_METADATA.GET_GRANTED_DDL('PROXY', 'TEST') FROM DUAL;

DBMS_METADATA.GET_GRANTED_DDL('PROXY','TEST')
----------------------------------------------------------------------

ALTER USER "TEST" GRANT CONNECT THROUGH "YANGTK"


需要注意的是,如果指定查询的授权不存在,并不是简单的返回未选定行,而是还会显示错误信息:

SQL> REVOKE SELECT ANY TABLE FROM TEST;

撤销成功。

SQL> SELECT DBMS_METADATA.GET_GRANTED_DDL('SYSTEM_GRANT', 'TEST') FROM DUAL;
ERROR:
ORA-31608: 找不到类型为 SYSTEM_GRANT 的指定对象
ORA-06512: 在"SYS.DBMS_SYS_ERROR", line 86
ORA-06512: 在"SYS.DBMS_METADATA", line 631
ORA-06512: 在"SYS.DBMS_METADATA", line 1339
ORA-06512: 在line 1

未选定行

最后还要说明一点,DBMS_METADATA的GET_GRANTED_DDL不会显示SYSDBA和SYSOPER权限。


上一篇:exp导出出现:ORA-00904: : invalid identifier


下一篇:数据库基础教程