Transforming myTableWithManyColsAndRows -> myTableWithOneXmlTypeColumn as follows:
declare
result xmltype;
begin
result := dbms_xmlgen.getxmltype('select * from myTableWithManyColsAndRows')
insert into table myTableWithOneXmlTypeColumn values result;
end;
Can I do the reverse, transform: myTableWithOneXmlTypeColumn -> myTableWithManyColsAndRows ?
That is, can I somehow construct a SQL statement that would query myTableWithOneXmlTypeColumn and return the same results as 'select * from myTableWithManyColsAndRows'?
--Creating a sample table with four columns and inserting random data.
create table myTableWithManyColsAndRows(
id_ number,
col1 varchar2(10),
col2 number,
col3 date
);
insert into myTableWithManyColsAndRows
select level, dbms_random.string('l', round(dbms_random.value(5,10))), round(dbms_random.value(1,100)),
to_date(round(dbms_random.value(2455928,2457024)),'J')
from dual
connect by level <= 7;
--Query to generate XML from the table.
select dbms_xmlgen.getxmltype('select * from myTableWithManyColsAndRows') from dual;
DBMS_XMLGEN.GETXMLTYPE('SELECT*FROMMYTABLEWITHMANYCOLSANDROWS')
-----------------------------------------------------------------
<ROWSET>
<ROW>
<ID_>1</ID_>
<COL1>ajfwmcaet</COL1>
<COL2>79</COL2>
<COL3>18-DEC-2013 00:00:00</COL3>
</ROW>
<ROW>
<ID_>2</ID_>
<COL1>wagvymnl</COL1>
<COL2>13</COL2>
<COL3>08-FEB-2012 00:00:00</COL3>
</ROW>
<ROW>
<ID_>3</ID_>
<COL1>jvlrv</COL1>
<COL2>45</COL2>
<COL3>28-DEC-2014 00:00:00</COL3>
</ROW>
<ROW>
<ID_>4</ID_>
<COL1>xirqutl</COL1>
<COL2>29</COL2>
<COL3>12-NOV-2012 00:00:00</COL3>
</ROW>
<ROW>
<ID_>5</ID_>
<COL1>rtdtmselx</COL1>
<COL2>75</COL2>
<COL3>26-JAN-2012 00:00:00</COL3>
</ROW>
<ROW>
<ID_>6</ID_>
<COL1>acvvw</COL1>
<COL2>70</COL2>
<COL3>19-SEP-2014 00:00:00</COL3>
</ROW>
<ROW>
<ID_>7</ID_>
<COL1>kbcmhh</COL1>
<COL2>62</COL2>
<COL3>26-MAY-2013 00:00:00</COL3>
</ROW>
</ROWSET>
To get back the rows and columns from XML, you need to use XMLTable . tag has multiple tags. So, first step is to extract those and map to a XMLType( xmlrow). From this, the individual columns are extracted.
with myTableWithOneXmlTypeColumn(xmlcol) as (
select dbms_xmlgen.getxmltype('select * from myTableWithManyColsAndRows') from dual
)
select cols.id_ id_, cols.col1 col1, cols.col2 col2, to_date(cols.col3,'dd-mon-yyyy hh24:mi:ss') col3
from myTableWithOneXmlTypeColumn x,
XMLTABLE('ROWSET'
PASSING x.xmlcol
COLUMNS xmlrow xmltype PATH 'ROW'
) xmlrows,
XMLTABLE('ROW'
PASSING xmlrows.xmlrow
COLUMNS id_ number PATH 'ID_',
col1 varchar2(10) PATH 'COL1',
col2 number PATH 'COL2',
col3 varchar2(20) PATH 'COL3'
) cols;
ID_ COL1 COL2 COL3
---------- ---------- ---------- --------------------------
1 ajfwmcaet 79 18-DEC-2013 00:00:00
2 wagvymnl 13 08-FEB-2012 00:00:00
3 jvlrv 45 28-DEC-2014 00:00:00
4 xirqutl 29 12-NOV-2012 00:00:00
5 rtdtmselx 75 26-JAN-2012 00:00:00
6 acvvw 70 19-SEP-2014 00:00:00
7 kbcmhh 62 26-MAY-2013 00:00:00
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.