gpt4 book ai didi

sql - 解析具有多个同名元素的 OpenXML

转载 作者:行者123 更新时间:2023-12-03 02:04:37 28 4
gpt4 key购买 nike

我有这样的数据结构:

<rootnode>
<group>
<id>1</id>
<anothernode>first string</anothernode>
<anothernode>second string</anothernode>
</group>
<group>
<id>2</id>
<anothernode>third string</anothernode>
<anothernode>fourth string</anothernode>
</group>
</rootnode>

以及以下代码:

EXEC sp_xml_preparedocument @index OUTPUT, @XMLdoc

SELECT *
FROM OPENXML (@index, 'rootnode/group')
WITH
(
id int 'id',
anothernode varchar(30) 'anothernode'
)

这给了我结果

id | anothernode
————————————————
1 | first string
2 | third string

如何让它显示此结果,而不是显示所有四个字符串?

id | anothernode
————————————————
1 | first string
1 | second string
2 | third string
2 | fourth string

最佳答案

SELECT *
FROM OPENXML (@index, 'rootnode/group/anothernode')
WITH
(
id int '../id',
anothernode varchar(30) '.'
)

或者您可以使用 XML 数据类型,如下所示:

SELECT G.N.value('(id/text())[1]', 'int') AS id,
A.N.value('text()[1]', 'varchar(30)') AS anothernode
FROM @XMLDoc.nodes('rootnode/group') AS G(N)
CROSS APPLY G.N.nodes('anothernode') AS A(N)

关于sql - 解析具有多个同名元素的 OpenXML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8013036/

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