1

I generated an xml containing all the rows of my table using dbms_xmlgen.getxmltype:

select dbms_xmlgen.getxmltype('select * from MyTable') from dual;

So I have an xml like this:

    <ROWSET>
    <ROW>
     <ID>1</ID>
     <SURNAME>Smith</SURNAME>
      <NAME>John</NAME>
      <ADDRESS>XXX</ADDRESS>
    </ROW>
    <ROW>
     <ID>2</ID>
     <SURNAME>Allen</SURNAME>
     <NAME>Jim</NAME>
     <ADDRESS>YYY</ADDRESS>
     </ROW>
   </ROWSET>

Is it possible to re-insert the values from XML above into the same table dinamically without specifing any column name?

2 Answers 2

2

dbms_xmlsave can do that for you. In it's most basic form

declare
   myxml clob;
   rowsv number;
   ctx dbms_xmlsave.ctxType;
begin null;
   my_xml := --  XML data
   ctx := dbms_xmlsave.newcontext('myTable');
   rows := dbms_xmlsave.insertxml(ctx,myxml);
   dbms_xmlsave.closecontext(ctx);
end;
/

Oracle documentation

Sign up to request clarification or add additional context in comments.

1 Comment

It mainly worls but with date columns I receive this error: ORA-29532 oracle.xml.sql.OracleXMLSQLException Unparseable date: "02-LUG-19"
1

Is it possible to re-insert the values from XML above into the same table dinamically without specifing any column name?

You can do as shown in below demo:

--Table having xml
CREATE TABLE xml_tab (  xml_data  XMLTYPE);

--Inserting xml                       
INSERT INTO xml_tab VALUES ( '<ROWSET>
                            <ROW>
                             <ID>1</ID>
                             <SURNAME>Smith</SURNAME>
                              <NAME>John</NAME>
                              <ADDRESS>XXX</ADDRESS>
                            </ROW>
                            <ROW>
                             <ID>2</ID>
                             <SURNAME>Allen</SURNAME>
                             <NAME>Jim</NAME>
                             <ADDRESS>YYY</ADDRESS>
                             </ROW>
                           </ROWSET>'
);
--Selecting xml
SELECT * FROM xml_tab;                        

--Table to insert records
CREATE TABLE xmtab (
    id        NUMBER,
    surname   VARCHAR2(50),
    name      VARCHAR2(50),
    address   VARCHAR2(50)
);

--Inserting into XMTAB
INSERT INTO XMTAB
SELECT xt.*
FROM xml_tab x,
    XMLTABLE('/ROWSET/ROW'
     PASSING x.xml_data
     COLUMNS
      ID          NUMBER       PATH 'ID',
      SURNAME     VARCHAR2(50) PATH 'SURNAME',
      NAME        VARCHAR2(50) PATH 'NAME',
      ADDRESS     VARCHAR2(50) PATH 'ADDRESS'
          ) xt
;

4 Comments

it's not the scope of the Question. Insert into XMTAB must be done dinamically without specifing columns.
@Ork To extract the values from the XML you need to mention the column names. It's NOT possible to do without that. You can still do it but the data will be saved under one columns not individual columns. Means if you remove COLUMNS ID NUMBER PATH 'ID', SURNAME VARCHAR2(50) PATH 'SURNAME', NAME VARCHAR2(50) PATH 'NAME', ADDRESS VARCHAR2(50) PATH 'ADDRESS' you still get the rows of type xmltype, which you can insert into table. Btw why you don't want to explicitly mention the columns ?
As you can see above, user @Rene suggested the solution and it is possible. The only problem with his solution is data column format. I need to do it because I've got backup procedures on multiple tables and I don't want to create a single procedure for every table I need to backup.
I need to do it because I've got backup procedures on multiple tables . The worst part playing with XML is its not very handy and its really difficult to manipulate. Although Rene solution seems working for you bt i can still see some error being faced by you. If that has been resolved you can close the question by accepting Rene's answer

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.