通过Oracle 10g中的数据库链接复制元数据

时间:2009-10-28 09:44:31

标签: oracle metadata dblink

提前感谢您的帮助专家。

我希望能够使用在数据库B上创建的过程将数据库A中的数据库对象复制到数据库B中。

我在两者之间创建了一个数据库链接,并调整了dbms_metadata的get_ddl函数,如下所示:

create or replace function GetDDL
   (
      p_name in MetaDataPkg.t_string
      p_type in MetaDataPkg.t_string

   )
      return MetaDataPkg.t_longstring
   is
      -- clob
      v_clob clob;

      -- array of long strings
      c_SYSPrefix        constant char(4)  := 'SYS_';
      c_doublequote      constant char(1)  := '"';   
      v_longstrings metadatapkg.t_arraylongstring;
      v_schema      metadatapkg.t_string;
      v_fullength   pls_integer := 0;
      v_offset      pls_integer := 0;
      v_length      pls_integer := 0;

   begin

        SELECT DISTINCT OWNER
        INTO v_schema
        FROM all_objects@ENTORA
        where object_name = upper(p_name);

      -- get DDL
      v_clob := dbms_metadata.get_ddl(p_type, upper(p_name), upper(v_schema));

      -- get CLOB length
      v_fullength := dbms_lob.GetLength(v_clob);

      for nIndex in 1..ceil(v_fullength / 32767)
      loop
         v_offset := v_length + 1;
         v_length := least(v_fullength - (nIndex - 1) * 32767, 32767);

         dbms_lob.read(v_clob, v_length, v_offset, v_longstrings(nIndex));


          -- Remove table’s owner from DDL string:

         v_longstrings(nIndex) := replace(
            v_longstrings(nIndex),
            c_doublequote || user || c_doublequote || '.',
            ''
         );


          -- Remove the following from DDL string:
          -- 1) "new line" characters (chr(10))
          -- 2) leading and trailing spaces

         v_longstrings(nIndex) :=
         ltrim(rtrim(replace(v_longstrings(nIndex), chr(10), '')));         
      end loop;

      -- close CLOB
      if (dbms_lob.isOpen(v_clob) > 0)
      then
         dbms_lob.close(v_clob);
      end if;

      return v_longstrings(1);

   end GetDDL;

以便删除通常随元数据提供的架构前缀。 每当我使用以下查询在数据库链接上运行此函数时,我得到一个空值。

选择getddl('TABLE','TABLE1') 来自user_tables @ ENTORA table_name ='TABLE1';

选择getddl('TABLE','TABLE1') 来自双@ ENTORA;

t_string是varchar2(30)  t_longstring是varchar2(32767) 并键入t_ArrayLongString是t_longstring的表

如果有人能提供帮助,我将非常感激。非常感谢。

4 个答案:

答案 0 :(得分:2)

CREATE OR REPLACE function DEMO_FN
(object_type varchar2, table_name varchar2) return varchar2 
is 

v_longstrings varchar2(32223);
c_doublequote      constant char(1)  := '"';  
begin 
v_longstrings := dbms_metadata.get_ddl(object_type,table_name);

-- Remove double quotes from DDL string:
v_longstrings := replace(v_longstrings, c_doublequote || user || c_doublequote || '.','');

-- Remove the following from DDL string:
          -- 1) "new line" characters (chr(10))
          -- 2) leading and trailing spaces
v_longstrings := ltrim(rtrim(replace(v_longstrings, chr(10), '')));
return v_longstrings; 
end;
/

请注意,签名模式必须具有SELECT_CATALOG_ROLE才能执行此功能。

一个例子是

选择demo_fn @ db_link('TABLE','TABLE_NAME')FROM DUAL;

答案 1 :(得分:1)

这很好用

declare
  v_ddl  clob;
  V_DDL_PART varchar2(4000);
begin
    for I in 0 .. 50 loop
      select DBMS_LOB.SUBSTR@db_link(DBMS_METADATA.GET_DDL@db_link('TABLE','MYTABLE'),(4000 * I + 1)) into V_DDL_PART from dual@db_link;
      V_DDL := V_DDL || V_DDL_PART;
    end loop;
end;

答案 2 :(得分:1)

这个工作正常,小错误修复:

declare
    v_ddl  clob;
    V_DDL_PART varchar2(4000);
begin
    for I in 0 .. 10 loop
        select DBMS_LOB.SUBSTR@LINK(
            DBMS_METADATA.GET_DDL@LINK('TABLE','NAME','SCHEMA'),
            4000,(4000 * I + 1)) into V_DDL_PART from dual@LINK;
        V_DDL := V_DDL || V_DDL_PART;
    end loop;
    dbms_output.put_line(v_ddl);
end;

答案 3 :(得分:-1)

我希望我的语法正确,但我认为它应该是v_clob := dbms_metadata@ENTORA.get_ddl(...)而不是v_clob := dbms_metadata.get_ddl(...)