1

If I have a table:

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

and I want to transfer data to a 1 column XML table:

create table XMLTable (
    data1 sys.xmltype
);

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

How would I do that?

2 Answers 2

3
INSERT INTO XMLTABLE
SELECT
    XMLELEMENT(
        "column2",
        XMLELEMENT("column1", COLUMN1), XMLELEMENT("column2", COLUMN2), XMLELEMENT("column3", COLUMN3)
    )
FROM
    thisTable;
Sign up to request clarification or add additional context in comments.

3 Comments

This is not working. I tried it and in xmltable only XMLTYPE is coming as output.
Because the result is XMLTYPE not VARCHAR2/CLOB; it depends on application how it's presented. SQL*Plus shows XML document. Try SELECT TO_CLOB(DATA1) FROM XMLTABLE
Thank @Husqvik. It is working perfectly. +1 from my side
1

You can insert it using below procedure.

  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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.