views:

31

answers:

2

Hi All,

I have two separate tables TVs and Receivers that I am using the FOR XML PATH commands to build XML off of. My issue is that I want to combine the output of my TV XML Build with my Receiver XML Build to create one XML output.

So I would have something like this(Which allows me to keep the TVs and Receivers Tags Separate within the FilterData Root):

<FilterData>
<TVs>
    <TV>
        <Type>LCD</Type>
        <Brand>Samsung</Brand>
    </TV>
    <TV>
        <Type>LCD</Type>
        <Brand>Panasonic</Brand>
    </TV>
</TVs>
<Receivers>
    <Receiver>
        <Type>Surround 7.1</Type>
        <Brand>Onkyo</Brand>
    </Receiver>
    <Receiver>
        <Type>Surround 7.1</Type>
        <Brand>Denon</Brand>
    </Receiver>
</Receivers>
</FilterData>

The problem is that when I build my queries to output this XML

Select
Type
,Brand
From dbo.TVs
FOR XML PATH('TV'),ROOT('TVS') TYPE

AND

Select
Type
,Brand
From dbo.Receivers
FOR XML PATH('Receiver'),ROOT('Receivers') TYPE

I am not sure how to combine these to look like the example:

<FilterData>
<TVs>
    <TV>
        <Type>LCD</Type>
        <Brand>Samsung</Brand>
    </TV>
    <TV>
        <Type>LCD</Type>
        <Brand>Panasonic</Brand>
    </TV>
</TVs>
<Receivers>
    <Receiver>
        <Type>Surround 7.1</Type>
        <Brand>Onkyo</Brand>
    </Receiver>
    <Receiver>
        <Type>Surround 7.1</Type>
        <Brand>Denon</Brand>
    </Receiver>
</Receivers>

Any thoughts would be greatly appreciated.

Thanks,

S

+1  A: 
DECLARE @tvs TABLE
(
      [Type]    varchar(20) not null
    , [Brand]   varchar(50) not null
)

DECLARE @receivers TABLE
(
      [Type]    varchar(20) not null
    , [Brand]   varchar(50) not null
)

INSERT INTO @tvs([Type], [Brand]) Values('LCD', 'Samsung'), ('LCD', 'Panasonic');
INSERT INTO @receivers([Type], [Brand]) Values('Surround 7.1', 'Onkyo'), ('Surround 7.1', 'Dennon');



SELECT
    (
        SELECT
              [Type]
            , [Brand]
        FROM
            @tvs
        FOR XML PATH('TV'),ROOT('TVS'), TYPE
    )
    ,
    (
        SELECT
              [Type]
            , [Brand]
        FROM
            @receivers
        FOR XML PATH('Receiver'),ROOT('Receivers'), TYPE
    )
FOR XML PATH('FilterData');    
etliens
+1  A: 

Use:

 SELECT (SELECT t.type, t.brand
           FROM dbo.TVs t
        FOR XML PATH('tv'), ROOT('tvs'), ELEMENTS, TYPE),
       (SELECT r.type, r.brand
          FROM dbo.Receivers r
       FOR XML PATH('receiver'), ROOT('receivers'), ELEMENTS, TYPE)
FOR XML PATH('filterdata')

Tested using:

WITH tvs AS (
    SELECT 'LCD' AS type, 'Samsung' AS brand
    UNION ALL
    SELECT 'LCD' AS type, 'Panasonic' AS brand),
     receivers AS (
    SELECT 'Surround 7.1' AS type, 'Onkyo' AS brand
    UNION ALL
    SELECT 'Surround 7.1', 'Denon')
 SELECT (SELECT t.type, t.brand
           FROM tvs t
        FOR XML PATH('tv'), ROOT('tvs'), ELEMENTS, TYPE),
       (SELECT r.type, r.brand
          FROM receivers r
       FOR XML PATH('receiver'), ROOT('receivers'), ELEMENTS, TYPE)
FOR XML PATH('filterdata')
OMG Ponies