I'm trying to generate XML that will output the structure and data of any table using SQL 2008.
So, for example, you would pass in a table name to this stored procedure and it would generate XML in this format:
<TABLE name="mytable">
<ROW>
<COL name="firstname">John</COL>
<COL name="lastname">Smith</COL>
</ROW>
<ROW>
<COL name="firstname">Bob</COL>
<COL name="lastname">Jones</COL>
</ROW>
</TABLE>
If there was a table called mytable (firstname varchar(100), lastname varchar(100))
You would be able to call stored procedure like this:
exec spGenerateTableData @tablename='mytable'
The trick I can't get is how to make the column names attributes of the COL element. I'm sure you need to use FOR XML clause in select statement. Keep in mind, this should work given any table passed in - so you don't know the names of the columns in advance.
Thanks for any help!