在 SQL 服务器中获取特定的 XML 子节点

Get specific XML Child node in SQL Server

我正在尝试将 XMl 加载到 MS SQL 服务器中具有逻辑键的不同表。 我在 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)