简体   繁体   English

如何使用XQuery从XML将值列表提取到行中?

[英]How to extract list of values into rows from XML using XQuery?

I have an XQuery as under 我有一个XQuery如下

DECLARE @x XML
SELECT @x = '<PartnerEmails>
<Email>a@xxxx.com</Email>
<Email>b@xxxx.com</Email>
</PartnerEmails>'
SELECT @x.query('data(PartnerEmails/Email)').value('.','varchar(100)') AS Val

Actual Output: 实际输出:

Val
a@xxxx.com b@xxxx.com

Expected Output 预期产量

a@xxxx.com
b@xxxx.com

ie In two different rows. 即在两个不同的行。

How to do so? 怎么做?

Use this: 用这个:

SELECT 
    node.value('.','varchar(100)') AS Val
FROM
    @x.nodes('/PartnerEmails/Email') AS PE(Node)    

Since you have multiple nodes inside <PartnerEmails> , you need to use the .nodes() function to create an "inline" table of XML fragments - each "row" in that table contains one <Email> node which you can then query on (and extract the contents of the XML node). 由于<PartnerEmails>有多个节点,因此需要使用.nodes()函数创建XML片段的“内联”表-该表中的每个“行”都包含一个<Email>节点,然后可以在该节点上查询(并提取XML节点的内容)。

DECLARE @x XML
SELECT @x = '<PartnerEmails>
<Email>a@xxxx.com</Email>
<Email>b@xxxx.com</Email>
</PartnerEmails>'

SELECT   ColumnValue.value('.','varchar(1000)')  as Val            
FROM @x.nodes('/PartnerEmails/Email') as Table1(ColumnValue) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM