gpt4 book ai didi

xml - SQLServer 2008R2中如何跨多行查询xml

转载 作者:数据小太阳 更新时间:2023-10-29 02:22:23 25 4
gpt4 key购买 nike

在一个表中,我有多行,每行都包含基于通用模式的 xml。 XML 的示例可能是:

<Items>
<Item>Item 1</Item>
<Item>Item 2</Item>
</Items>

如果我在一个表中有多行,所有行都包含类似的 xml,是否可以编写一个查询,在单个结果集中返回所有行的 Item 节点中的所有值?我们正在使用 SQL Server 2008 R2

最佳答案

如果您的 xml 在 XML 列中定义..

DECLARE @Items AS TABLE 
(
ItemXml XML
)

-- test data with a couple rows of xml
INSERT INTO @Items(ItemXml)
VALUES ('<Items><Item>Item 1</Item><Item>Item 2</Item></Items>')
,('<Items><Item>Item 3</Item><Item>Item 4</Item></Items>')

-- the query
SELECT t.i.value('.','VARCHAR(8000)') AS Item
FROM @Items CROSS APPLY ItemXml.nodes('/Items/Item') t(i)

给你

Item
------
Item 1
Item 2
Item 3
Item 4

这里的关键是nodes()哪个

is useful when you want to shred an xml data type instance into relational data. It allows you to identify nodes that will be mapped into a new row.

关于xml - SQLServer 2008R2中如何跨多行查询xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12379236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com