ORACLE XML值作为透视提取

时间:2014-04-08 11:05:10

标签: xml oracle pivot extract

我想知道有什么方法可以让我把xml文件作为数据库吗?

我的代码

select t.extract('/S/@ID').getstringval() as s 
FROM
TABLE( 
XMLSEQUENCE( 
(select xmltype('<?xml version="1.0"?>
<SERIAL>
<S ID="1E0100057107" />
<S ID="1E0100057108" />
<S ID="1E0100057109" />
<S ID="1E0100057110" />
</SERIAL>').extract( '//S' ) from dual)
))T;
desired output 
S
-------------------------
1E0100057107,1E0100057108,1E0100057109,1E0100057110
谢谢..

1 个答案:

答案 0 :(得分:0)

使用LISTAGG功能(支持最多4000个字符串)

select LISTAGG(t.extract('/S/@ID').getstringval(),',')  WITHIN GROUP (ORDER BY 1) as s
FROM
TABLE( 
XMLSEQUENCE( 
(select xmltype('<?xml version="1.0"?>
<SERIAL>
<S ID="1E0100057107" />
<S ID="1E0100057108" />
<S ID="1E0100057109" />
<S ID="1E0100057110" />
</SERIAL>').extract( '//S' ) from dual)
))T;

XMLAGG

select xmlagg(xmlelement(e, t.extract('/S/@ID').getstringval()|| ',')).extract('//text()').extract('//text()')
FROM
TABLE( 
XMLSEQUENCE( 
(select xmltype('<?xml version="1.0"?>
<SERIAL>
<S ID="1E0100057107" />
<S ID="1E0100057108" />
<S ID="1E0100057109" />
<S ID="1E0100057110" />
</SERIAL>').extract( '//S' ) from dual)
))T;