I need to create a T-SQL query in SQL Server 2008 which outputs data as XML. But this is a generic process that queries data with different columns, which could have spaces in the column names. Therefore, I'd like the XML to list the column name as an attribute (spaces aren't allowed in element names).
Given a master table which links to a detail table for child records, I would want the output to look like this:
<master id="123">
<detail colname="customer">John Smith</detail>
<detail colname="amount">888.45</detail>
<detail colname="date">01/01/01</detail>
</master>
<master id="456">
<detail colname="customer">Suzie Jones</detail>
<detail colname="amount">1000.25</detail>
<detail colname="date">05/05/01</detail>
</master>
The columns in the detail record can vary, so I can't hardcode them.
I believe this may be possible using PIVOT command, but that gets really ugly when you don't know structure of data. I feel like there's got to be a way to get a column name to appear as an attribute value!
thanks for any advice.