利用DBMS_METADATA取得TABLE INDEX TRIGGER创建的脚本

declare
    tabname varchar2(10) :='TEST';
    tabowner varchar2(10) :='TEST';
    t_str varchar2(2000);
    C_str varchar2(2000);
    I_str varchar2(2000);
    TR_str varchar2(2000);
    t_ddl varchar2(2000);
    C_ddl varchar2(2000);
    I_ddl varchar2(2000);
    TR_ddl varchar2(2000);
    cursor ddl_ta is
       select t_ddl,c_ddl,i_ddl,tr_ddl from (
       SELECT DBMS_METADATA.get_ddl ('TABLE',tabname,tabowner) t_ddl
       FROM all_tables
       WHERE wner =tabowner AND table_name =tabname
       UNION ALL
       SELECT DBMS_METADATA.get_dependent_ddl ('COMMENT',tabname,tabowner) c_ddl
       FROM (SELECT table_name, owner
       FROM all_col_comments
       WHERE wner =tabowner AND table_name =tabname AND comments IS NOT NULL
       UNION all
       SELECT table_name, owner
       FROM SYS.all_tab_comments
       WHERE wner = tabowner AND table_name =tabname AND comments IS NOT NULL)
       UNION ALL
       SELECT DBMS_METADATA.get_dependent_ddl ('INDEX',tabname,tabowner) i_ddl
       FROM all_indexes
       WHERE table_owner =tabowner
       AND table_name = tabname
       AND index_name NOT IN (
       SELECT constraint_name
       FROM SYS.all_constraints
       WHERE table_name =tabname
       AND constraint_type = 'P')
       AND uniqueness != 'UNIQUE'
       UNION ALL
       SELECT DBMS_METADATA.get_ddl ('TRIGGER',tabname,tabowner) tr_ddl
       FROM all_triggers
       WHERE table_owner =tabowner AND table_name =tabname);
begin
open ddl_ta;
  loop
    fetch ddl_ta into t_str,c_str,i_str,tr_str;
    exit when ddl_ta%notfound;
dbms_output.put_line('================get table_ddl script======================');
dbms_output.put_line(t_str);
       if c_str is not null then
dbms_output.put_line('================get comment_ddl script====================');
dbms_output.put_line(c_str);
          else
          if i_str is not null then
dbms_output.put_line('================get index_ddl script======================');
dbms_output.put_line(i_str);
            if tr_str is not null then
dbms_output.put_line('================get constr_ddl script=====================');
dbms_output.put_line(tr_str);
            end if;
         end if;
      end if;
end loop;
close ddl_ta;
end;
/

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7882490/viewspace-668083/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7882490/viewspace-668083/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值