Moving XML over a DBLink

后端 未结 5 1929
不知归路
不知归路 2021-01-22 17:24

I am trying to move some data over a dblink and one of the columns is an XMLType column. The code looks like this:

begin
    delete from some_schema.some_remote_         


        
5条回答
  •  無奈伤痛
    2021-01-22 18:18

    The "xml->text->xml" chain might be complicated, but could help in some cases (for example when inserting is not on option but updating only). You can try with "n" peaces of varchar columns (in the destination table or in a differnet one, perheaps in different schema on the remote DB), where "n" is: ceil(max(dbms_lob.getlength(MyXmlColumn)) / 4000)

    Then you can transfer these fragments to remote temporary fields:

    insert into RemoteSchema.MyTable(Id, XmlPart1, XmlPart2,...)
    (select 1 /*some Id*/,
            dbma_lob.substr(MyXmlColumn.getclobval(), 4000, 1),
            dbma_lob.substr(MyXmlColumn.getclobval(), 4000, 4001),
            ...
     from LocalSchema.MyTable
    

    XmlType can be re-composed from fragments like this:

    create or replace function concat_to_xml(p_id number)
    return xmltype
    is
      xml_lob clob;
      xml xmltype;
    begin
      dbms_lob.createtemporary(xml_lob, true);
      for r in (select XmlPart1, XmlPart2, ... from RemoteSchema.MyTable where Id = p_id)
      loop
        if r.XmlPart1 is not null then
          dbms_lob.writeappend(xml_lob, length(r.XmlPart1), r.XmlPart1);
        end if;
        if r.XmlPart2 is not null then
          dbms_lob.writeappend(xml_lob, length(r.XmlPart2), r.XmlPart2);
        end if;
        ...
      end loop;
      xml := xmltype(xml_lob);
      dbms_lob.freetemporary(xml_lob);
      return xml;
    end;
    

    Finally use the result to update any other table in the remothe schema like:

    update RemoteSchema.MyTable2 t2 set t2.MyXmlColumn = concat_to_xml(1 /*some Id*/);
    

提交回复
热议问题