SQL查询以检索嵌套xml中的所有实例

时间:2011-04-14 11:39:58

标签: xml tsql nested

我有一个MS SQL 2005数据库,其中报告表包含 XMLReport 列,其XML结构与此类似:

<my:CART_Marine>
    <my:Area_Summary_Details>
        <my:Areas>
            <my:Area_Group>
                <my:Area_Number>1</my:Area_Number>
                <my:Paint_Application>
                    <my:Paint_Applications>
                        <my:Paint_Application_Group>
                            <my:Coat_Number>1</my:Coat_Number>
                            <my:Base_Batches>
                                <my:Base_Batch_Group>
                                    <my:Base_Batch_No>1234567</my:Base_Batch_No>
                                    <my:Base_Batch_No>5455443</my:Base_Batch_No>
                                    <my:Base_Batch_No>8677667</my:Base_Batch_No>
                                    <my:Base_Batch_No>3455445</my:Base_Batch_No>
                                </my:Base_Batch_Group>
                            </my:Base_Batches>  
                        </my:Paint_Application_Group>
                        <my:Paint_Application_Group>
                            <my:Coat_Number>2</my:Coat_Number>
                            <my:Base_Batches>
                                <my:Base_Batch_Group>
                                    <my:Base_Batch_No>9744566</my:Base_Batch_No>
                                    <my:Base_Batch_No>8755632</my:Base_Batch_No>
                                </my:Base_Batch_Group>
                            </my:Base_Batches>  
                        </my:Paint_Application_Group>
                        <my:Paint_Application_Group>
                            <my:Coat_Number>3</my:Coat_Number>
                            <my:Base_Batches>
                                <my:Base_Batch_Group>
                                    <my:Base_Batch_No>5456783</my:Base_Batch_No>
                                </my:Base_Batch_Group>
                            </my:Base_Batches>  
                        </my:Paint_Application_Group>
                    </my:Paint_Applications>
                </my:Paint_Application>
            </my:Area_Group>
            <my:Area_Group>
                <my:Area_Number>2</my:Area_Number>
                <my:Paint_Application>
                    <my:Paint_Applications>
                        <my:Paint_Application_Group>
                            <my:Coat_Number>1</my:Coat_Number>
                            <my:Base_Batches>
                                <my:Base_Batch_Group>
                                    <my:Base_Batch_No>2312311</my:Base_Batch_No>
                                    <my:Base_Batch_No>2352244</my:Base_Batch_No>
                                    <my:Base_Batch_No>8746773</my:Base_Batch_No>
                                    <my:Base_Batch_No>7363634</my:Base_Batch_No>
                                </my:Base_Batch_Group>
                            </my:Base_Batches>  
                        </my:Paint_Application_Group>
                    </my:Paint_Applications>
                </my:Paint_Application>
            </my:Area_Group>
            <my:Area_Group>
                <my:Area_Number>3</my:Area_Number>
                <my:Paint_Application>
                    <my:Paint_Applications>
                        <my:Paint_Application_Group>
                            <my:Coat_Number>1</my:Coat_Number>
                            <my:Base_Batches>
                                <my:Base_Batch_Group>
                                    <my:Base_Batch_No>1523552</my:Base_Batch_No>
                                    <my:Base_Batch_No>6164633</my:Base_Batch_No>
                                </my:Base_Batch_Group>
                            </my:Base_Batches>  
                        </my:Paint_Application_Group>
                    </my:Paint_Applications>
                </my:Paint_Application>
            </my:Area_Group>
        </my:Areas>                     
    </my:Area_Summary_Details>                          
</my:CART_Marine>                               

论坛 Area_Group Paint_Application_Group Base_Batch_Group 是可重复的

我想要实现的是一个包含列的表:

Area_Number | Coat_Number |的 Base_Batch_No

其中 Coat_Number 仅来自指定的 Area_Number Base_Batch_No 仅来自指定的 Coat_Number

基于上面的例子,它应该创建如下:

**Area_Number** |**Coat_Number**    |**Base_Batch_Number**

1       |1      |1234567

1       |1      |5455443

1       |1      |8677667

1       |1      |3455445

1       |2      |9744566

1       |2      |8755632

1       |3      |5456783

2       |1      |2312311

2       |1      |2352244

2       |1      |8746773

2       |1      |7363634

3       |1      |1523552

3       |1      |6164633

我尝试了很多方法,但我完成了这样的事情:

select distinct
        r.XMLReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Area_Number)[1]','nvarchar(12)')  AS Area_Number,
        s.XMLReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Paint_Application/my:Paint_Applications/my:Paint_Application_Group/my:Coat_Number)[1]','nvarchar(12)') AS Coat_Number,
        t.XmlReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Paint_Application/my:Paint_Applications/my:Paint_Application_Group/my:Base_Batches/my:Base_Batch_Group/my:Base_Batch_No)[1]','nvarchar(12)') AS Base_Batch_Number
from Report  
        cross apply Report.XMLReport.nodes('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(/my:CART_Marine/my:Area_Summary_Details/my:Areas/my:Area_Group)') AS s(XMLReport)
        cross apply  Report.XMLReport.nodes('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(/my:CART_Marine/my:Area_Summary_Details/my:Areas/my:Area_Group)') AS r(XMLReport)
        cross apply  Report.XMLReport.nodes('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(/my:CART_Marine/my:Area_Summary_Details/my:Areas/my:Area_Group)') AS t(XMLReport)

where
        r.XMLReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Area_Number)[1]','nvarchar(12)')= s.XMLReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Area_Number)[1]','nvarchar(12)')
    AND
        s.XMLReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Paint_Application/my:Paint_Applications/my:Paint_Application_Group/my:Coat_Number)[1]','nvarchar(12)')=   t.XMLReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Paint_Application/my:Paint_Applications/my:Paint_Application_Group/my:Coat_Number)[1]','nvarchar(12)')

这是生成一个如下表格(我从脑海中创建了这个表,所以它不能显示所有行,但你会发现这个想法)

**Area_Number** |**Coat_Number**    |**Base_Batch_Number**

1           |1              |1234567

1           |1              |5455443

1           |1              |8677667

1           |1              |3455445

2           |1              |2312311

3           |1              |1523552

因此,只包含第一个 Coat_Number ,并且仅包含第一个 Coat_Number 中的第一个 Base_Batch_Number

我无法弄清楚此查询如何迭代 Paint_Application_Group Base_Batch_Group 组的所有实例。

请帮忙,我现在已经连续3天了......

Krzysztof Deneka

1 个答案:

答案 0 :(得分:0)

为了简单起见,我删除了名称空间,但这里的想法是:

DECLARE @report XML = N'
<CART_Marine>
    <Area_Summary_Details>
        <Areas>
            <Area_Group>
                <Area_Number>1</Area_Number>
                <Paint_Application>
                    <Paint_Applications>
                        <Paint_Application_Group>
                            <Coat_Number>1</Coat_Number>
                            <Base_Batches>
                                <Base_Batch_Group>
                                    <Base_Batch_No>1234567</Base_Batch_No>
                                    <Base_Batch_No>5455443</Base_Batch_No>
                                    <Base_Batch_No>8677667</Base_Batch_No>
                                    <Base_Batch_No>3455445</Base_Batch_No>
                                </Base_Batch_Group>
                            </Base_Batches>  
                        </Paint_Application_Group>
                        <Paint_Application_Group>
                            <Coat_Number>2</Coat_Number>
                            <Base_Batches>
                                <Base_Batch_Group>
                                    <Base_Batch_No>9744566</Base_Batch_No>
                                    <Base_Batch_No>8755632</Base_Batch_No>
                                </Base_Batch_Group>
                            </Base_Batches>  
                        </Paint_Application_Group>
                        <Paint_Application_Group>
                            <Coat_Number>3</Coat_Number>
                            <Base_Batches>
                                <Base_Batch_Group>
                                    <Base_Batch_No>5456783</Base_Batch_No>
                                </Base_Batch_Group>
                            </Base_Batches>  
                        </Paint_Application_Group>
                    </Paint_Applications>
                </Paint_Application>
            </Area_Group>
            <Area_Group>
                <Area_Number>2</Area_Number>
                <Paint_Application>
                    <Paint_Applications>
                        <Paint_Application_Group>
                            <Coat_Number>1</Coat_Number>
                            <Base_Batches>
                                <Base_Batch_Group>
                                    <Base_Batch_No>2312311</Base_Batch_No>
                                    <Base_Batch_No>2352244</Base_Batch_No>
                                    <Base_Batch_No>8746773</Base_Batch_No>
                                    <Base_Batch_No>7363634</Base_Batch_No>
                                </Base_Batch_Group>
                            </Base_Batches>  
                        </Paint_Application_Group>
                    </Paint_Applications>
                </Paint_Application>
            </Area_Group>
            <Area_Group>
                <Area_Number>3</Area_Number>
                <Paint_Application>
                    <Paint_Applications>
                        <Paint_Application_Group>
                            <Coat_Number>1</Coat_Number>
                            <Base_Batches>
                                <Base_Batch_Group>
                                    <Base_Batch_No>1523552</Base_Batch_No>
                                    <Base_Batch_No>6164633</Base_Batch_No>
                                </Base_Batch_Group>
                            </Base_Batches>  
                        </Paint_Application_Group>
                    </Paint_Applications>
                </Paint_Application>
            </Area_Group>
        </Areas>                     
    </Area_Summary_Details>                          
</CART_Marine>'

SELECT  batch.value('../../../../../../Area_Number[1]', 'INTEGER'),
        batch.value('../../../Coat_Number[1]', 'INTEGER'),
        batch.value('.', 'INTEGER')
FROM    @report.nodes('//Base_Batch_No') r(batch)