SQL Server XML 查询:查询多个同名子元素

SQL Server XML querying: Query multiple child elements with same name(SQL Server XML 查询:查询多个同名子元素)
本文介绍了SQL Server XML 查询:查询多个同名子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一个问题中,我想知道如何使用 SQL 根据标识符JOIN 不同的 XML 元素.我得到了几个不错的解决方案,你可以看到 这里.

In a previous question I wanted to know how I can use SQL to JOIN different XML elements based on an identifier. I was provided with several nice solutions as you can see here.

现在我正在根据我的实际数据调整此解决方案.不幸的是,我偶然发现了我在链接问题中提供的最小可行示例中不存在的新障碍.在我的实际数据中,我还有几个同名的子元素.请参阅以下示例数据中的元素 .

Now I am in the process of adapting this solution to my actual data. Unfortunately I stumbled upon a new obstacle that was not present in the minimum viable example I provided in the linked question. In my actual data I also have several child elements of the same name. See the element <subElement> in the following example data.

<xml>
    <dataSetData>
        <text>ABC</text>
    </dataSetData>
    <generalData>
        <id>123</id>
        <text>text data</text>
        <subElement>
            <subData>sub example data AAA</subData>
        </subElement>
        <subElement>
            <subData>sub example data BBB</subData>
        </subElement>
    </generalData>
    <generalData>
        <id>456</id>
        <text>text data 2</text>
        <subElement>
            <subData>sub example data CCC</subData>
        </subElement>
    </generalData>
    <specialData>
        <id>123</id>
        <text>special data text</text>
    </specialData>
    <specialData>
        <id>456</id>
        <text>special data text 2</text>
    </specialData>
</xml>

预期结果应如下所示.

DataSetData | GeneralDataID | GeneralDataText | subData              | SpecialDataTest
ABC         | 123           | text data       | sub example data AAA | special data text
ABC         | 123           | text data       | sub example data BBB | special data text
ABC         | 456           | text data  2    | sub example data CCC | special data text 2

目前的解决方案(不考虑数据)如下(取自这里):

The current solution (without considering the <subElement> data) is as follows (taken from here):

SELECT TheXml.value('(/xml/dataSetData/text/text())[1]', 'VARCHAR(20)') AS DataSetData
     ,B.*
    , sp.value('(id/text())[1]', 'INT') AS SpecialDataID 
    , sp.value('(text/text())[1]', 'VARCHAR(30)') AS SpecialDataTest
INTO dbo.TestResult4
FROM dbo.TestXml
CROSS APPLY TheXml.nodes('/xml/generalData') AS A(g)
CROSS APPLY(SELECT g.value('(id/text())[1]', 'INT') AS GeneralDataID 
                 , g.value('(text/text())[1]', 'VARCHAR(30)') AS GeneralDataText) B
OUTER APPLY TheXml.nodes('/xml/specialData[id=sql:column("B.GeneralDataID")]') AS special(sp);

推荐答案

好的,这很简单...

SELECT TheXml.value('(/xml/dataSetData/text/text())[1]', 'VARCHAR(20)') AS DataSetData
    , B.*
    , se.value('(subData/text())[1]','varchar(100)') AS SubData 
    , sp.value('(id/text())[1]', 'INT') AS SpecialDataID 
    , sp.value('(text/text())[1]', 'VARCHAR(30)') AS SpecialDataTest
FROM dbo.TestXml
CROSS APPLY TheXml.nodes('/xml/generalData') AS A(g)
CROSS APPLY(SELECT g.value('(id/text())[1]', 'INT') AS GeneralDataID 
                 , g.value('(text/text())[1]', 'VARCHAR(30)') AS GeneralDataText) B
OUTER APPLY A.g.nodes('subElement') C(se)
OUTER APPLY TheXml.nodes('/xml/specialData[id=sql:column("B.GeneralDataID")]') AS D(sp);

这篇关于SQL Server XML 查询:查询多个同名子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)