将数据从表传输到XML对象表

时间:2015-11-18 02:40:05

标签: sql xml oracle plsql object-oriented-database

如果我有桌子:

create table thisTable (
    column1 varchar(20),
    column2 varchar(20),
    column3 varchar(20)
);

我希望将数据传输到1列XML表:

create table XMLTable (
    data1 sys.xmltype
);

<column2>
    <column1>..</column1>
    <column2>..</column2>
    <column3>..</column3>
</column2>

我该怎么做?

2 个答案:

答案 0 :(得分:3)

INSERT INTO XMLTABLE
SELECT
    XMLELEMENT(
        "column2",
        XMLELEMENT("column1", COLUMN1), XMLELEMENT("column2", COLUMN2), XMLELEMENT("column3", COLUMN3)
    )
FROM
    thisTable;

答案 1 :(得分:1)

您可以使用以下程序插入它。

  declare 
    sourceTable varchar2(80) := 'THISTABLE';
    destTable varchar2(80) := 'XMLTABLE';
    destColumn varchar2(80) := 'data1';
    TYPE cur_typ IS REF CURSOR;
    c cur_typ;
    colu varchar2(2000);
    vsql varchar2(2000) := ' select ';
begin



      for r in (select column_name from user_tab_columns where table_name = sourceTable order by column_id)
      loop
        vsql := vsql ||  ''' <' || r.column_name || '>'' || ' || r.column_name || ' || ''</' || r.column_name || '> '' || ' ;       

      end loop;

      vsql := substr(vsql, 0 ,length(vsql)-4);
      vsql :=  vsql  ||' as x From ' || sourceTable;


     open c for vsql ;
      loop
       FETCH c INTo colu;
       EXIT WHEN c%NOTFOUND;
            dbms_output.put_line(colu);
          execute immediate '  insert into ' ||  destTable || ' values (xmltype(''<column2>' || colu || '</column2>'')) ';

      end loop;
      close c;

end;