gpt4 book ai didi

sql - 在 SQL Server 中获取特定的 XML 子节点

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

我正在尝试将 XMl 加载到具有 MS SQL Server 中的逻辑键的不同表中。我在 SQL 中获取所有子节点时卡住了。

<Books>
<Book>
<Name>Book1</Name>
<Author>abc</Author>
<Stores>
<Name>Amazon</Name>
</Stores>
</Book>
<Book>
<Name>Book2</Name>
<Author>cde</Author>
<Stores>
<Name>Flipkart</Name>
</Stores>
</Book>
</Books>

我想得到如下结果。

BookId  Name   Author  StoreXML
1 Book1 abc <Stores><Name>Amazon</Name></Stores>
2 Book2 cde <Stores><Name>Flipkart</Name></Stores>

最佳答案

这样试试

DECLARE @xml XML=
N'<Books>
<Book>
<Name>Book1</Name>
<Author>abc</Author>
<Stores>
<Name>Amazon</Name>
</Stores>
</Book>
<Book>
<Name>Book2</Name>
<Author>cde</Author>
<Stores>
<Name>Flipkart</Name>
</Stores>
</Book>
</Books>';

SELECT ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) AS BookId --there is no id in your XML...
,b.value('(Name/text())[1]','nvarchar(max)') AS BookName
,b.value('(Author/text())[1]','nvarchar(max)') AS Author
,b.query('Stores') AS StoreXML
FROM @xml.nodes('/Books/Book') AS A(b)

关于sql - 在 SQL Server 中获取特定的 XML 子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41141457/

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