简体   繁体   中英

Nested sub tag resultset xml T-SQL

I'm not an expert for XML query... but I need only little step to my goal.

With the query below.

I got a problem in EANs tag with the sub tag 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'); 

I get this result:

<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>

but I need the result to look like this:

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

Can someone help me with this?

THANKS Alen, Italy

Try this as the subquery.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM