嵌套子标记结果集xml T-SQL

时间:2017-05-31 15:13:09

标签: sql-server xml tsql

我不是XML查询的专家......但我只需要迈出一小步。

使用以下查询。

我在EANs标签中遇到了子标签EAN的问题。

SELECT 
    ExternalId,  
    [Name],
    [Description],
    BrandExternalId,
    CategoryExternalId,
    ProductPageUrl,
    ImageUrl,    
    (SELECT ManufacturerPartNumber
     FOR XML PATH('ManufacturerPartNumbers'), TYPE),
    (SELECT b.EAN_Single as EAN  
     FROM #SP b 
     WHERE (ff.ExternalId = b.codart)  
     FOR XML PATH('EANs'), TYPE)
FROM 
    Cestino.acap.Bazaar_Servizio_00 ff with (nolock) 
WHERE 
    [ExternalId] IN (100001023)
FOR XML PATH ('Product'), ROOT('Products'); 

我得到了这个结果:

<Products>
    <Product>
        <ExternalId>100001023</ExternalId>
        <Name>Carta Lucida Adesiva Photo Stickers PS-101</Name>
        <Description>Carta lucida Photo Stickers.</Description>
        <BrandExternalId>CANON</BrandExternalId>
        <CategoryExternalId>10132</CategoryExternalId>
        <ManufacturerPartNumbers>
            <ManufacturerPartNumber>0001C001</ManufacturerPartNumber>
        </ManufacturerPartNumbers>
        <EANs>
            <EAN>0138030471030</EAN>
        </EANs>
        <EANs>
            <EAN>5051749491517</EAN>
        </EANs>
    </Product>
</Products>

但我需要将结果看起来像这样:

<EANs>
    <EAN>0138030471030</EAN>
    <EAN>5051749491517</EAN>
</EANs>

有人可以帮我这个吗?

感谢Alen,意大利

1 个答案:

答案 0 :(得分:1)

尝试将其作为子查询。

 (SELECT b.EAN_Single as EAN  
     FROM #SP b 
     WHERE (ff.ExternalId = b.codart)  
     FOR XML PATH(''), ROOT('EANS'), TYPE)