转自:http://www.jb51.net/article/39715.htm
dbms_metadata.get_ddl()用于获取对象的ddl,其具体用法如下。
注意:在sqlplus里,为了更好的展示DDL,需要设置如下参数:
set line 200
set pagesize 0
set long 99999
set feedback off
set echo off
1)获得表、索引、视图、存储过程、函数的DDL
select dbms_metadata.get_ddl(‘INDEX‘,‘INDEX_NAME‘,‘INDEX_OWNER‘) from dual;
select dbms_metadata.get_ddl(‘VIEW‘,‘VIEW_NAME‘,‘VIEW_OWNER‘) from dual;
select dbms_metadata.get_ddl(‘PROCEDURE‘,‘PROCEDURE_NAME‘,‘PROCEDURE_OWNER‘) from dual;
select dbms_metadata.get_ddl(‘FUNCTION‘,‘FUNCTION_NAME‘,‘FUNCTION_OWNER‘) from dual;
下面这个脚本用于获得某个schema下所有的表、索引、视图、存储过程、函数的DDL
set long 90000
set feedback off
set echo off
spool schema_ddl.sql
SELECT DBMS_METADATA.GET_DDL(‘TABLE‘,u.table_name,u.owner) FROM DBA_TABLES u;
SELECT DBMS_METADATA.GET_DDL(‘VIEW‘,u.view_name,u.owner) FROM DBA_VIEWS u;
SELECT DBMS_METADATA.GET_DDL(‘INDEX‘,u.index_name,u.owner) FROM DBA_INDEXES u;
select dbms_metadata.get_ddl(‘PROCEDURE‘,u.object_name, u.owner,) from dba_objects u where u.object_type = ‘PROCEDURE‘;
select dbms_metadata.get_ddl(‘FUNCTION‘,u.object_name, u.owner,) from dba_objects u where u.object_type = ‘FUNCTION‘;
spool off;
2)获得表空间的DDL
获得单个表空间的DDL:
获得所有表空间的DDL:
FROM DBA_TABLESPACES TS;
3)获得用户的DDL
获得单个用户的DDL:
代码如下:
4)获得所有用户的DDL
FROM DBA_USERS U;
19:37:33 SYS@db1(db1)> set line 200
19:37:34 SYS@db1(db1)> set pagesize 0
19:37:34 SYS@db1(db1)> set long 99999
19:37:35 SYS@db1(db1)> set feedback off
19:37:35 SYS@db1(db1)> set echo off
19:37:37 SYS@db1(db1)>
19:37:38 SYS@db1(db1)> select dbms_metadata.get_ddl(‘PROCEDURE‘,‘db1‘,‘check‘) from dual;
CREATE OR REPLACE PROCEDURE "check"."db1" as
var number:=1;
begin
while var >= 1
loop
insert into zabbix.SQL_EXEC_TIME_MON_1 select sysdate,a.sql_id,null,a.LAS
T_CALL_ET,a.username,a.status,a.schemaname,a.osuser,a.client_info,a.machine,a.te
rminal,a.program,a.SID,a.SERIAL#,a.sql_address,a.sql_exec_start,a.module ,a.even
t from v$session a where a.status=‘ACTIVE‘ and a.machine not in (‘db1‘,‘lo
calhost‘) and a.LAST_CALL_ET>1;
commit;
dbms_lock.sleep(1);
select min(id) into var from zabbix.stop_sql;
end loop;
end;
19:37:44 SYS@db1(db1)>