views:

50

answers:

3

I have a flattened table that contains columns that represent groups that need to be displayed in XML. Example data:

Market, Label, Style, Type
XXX,    YYY,   JJJ,   111
XXX,    YYY,   JJJ,   222
XXX,    YYY,   JJJ,   333
XXX,    YYY,   JJJ,   444    
XXX,    YYY,   LLL,   111    
XXX,    YYY,   LLL,   222    
XXX,    YYY,   LLL,   333    
XXX,    YYY,   LLL,   444

Using T-SQL what would be the best way to output the following:

<Market value=XXX>
    <label value=YYY>
       <Style value=JJJ>
          <Type value=111>
          </Type>
          ...
       </Style>
       <Style value=LLL>
          ...
       </Style>
    </label>
</Market>

Can I do this by using the XML Explicit clause in SQL Server?

A: 

there are 3 shaping method to generate table to xml in SQL Server 2000:

  1. for xml auto -> generate the query to xml automatically based on using table
  2. for xml raw -> generate the query table to row tag in xml
  3. for xml explicit -> the most complex but most comfort way :) you can defined your own xml structure

for the cheat sheet you can see this link

hope this can help you.

fritz
There's four, `PATH` is 2005+
OMG Ponies
Thanks for the link. I have had a look at the 3 options. All of them seem to offer examples that use multiple tables my problem uses only one table to create the XML hierarchy. Not sure if it can be done in SQL?
Panaymel
i'm pretty sure it will work. cause the tuts is for sql server :)
fritz
A: 

This might help.. take a look

SELECT distinct MyTable.Market "Market/@Value",
        MyTable.Label "Market/Label/@Value",
        MyTable.Style "Market/Label/Style/@Value",

             (SELECT Type AS  "Value"
               FROM   MyTable myTab
               WHERE myTab.Market=MyTable.Market
                        and myTab.Label=MyTable.Label
                        and myTab.Style = MyTable.Style
               FOR XML PATH ('')
               ) AS "Market/Label/Style/Type"
        FROM MyTable  
FOR XML PATH('')

Resultant XML was :

<Market Value="XXX">
  <Label Value="YYY">
    <Style Value="JJJ">
      <Type>&lt;Value&gt;111&lt;/Value&gt;&lt;Value&gt;222&lt;/Value&gt;&lt;Value&gt;333&lt;/Value&gt;&lt;Value&gt;444&lt;/Value&gt;</Type>
    </Style>
  </Label>
</Market>
<Market Value="XXX">
  <Label Value="YYY">
    <Style Value="LLL">
      <Type>&lt;Value&gt;111&lt;/Value&gt;&lt;Value&gt;123&lt;/Value&gt;</Type>
    </Style>
  </Label>
</Market>

Hope it helps

Aamod Thakur
Thanks for that. Its so close but when I add more data it does not correctly group by. I think I will the answer that was left by Robert it might be easier to do it in code.
Panaymel
A: 

Formatting complex XML documents in T-SQL is a fool's errand. It can be done - maybe - but then you come back to it a month later and what you've got is incomprehensible.

It's much, much easier to either write a method in C# or whatever that processes a DataReader to produce the XML, or write an XSLT transform that converts the XML emitted from the query into whatever specialized format you're trying to create.

Robert Rossney
OMG Ponies