I'm having an issue with a SQL query that is designed to return the resulting data as XML. Here's a code dump to show you all whats going on:
SQL Query (Note: Table names and column names redacted)
with resultdata as
(
SELECT
(
select * from Table1 (nolock)
where column1 = 99999 and column2 = -1 for xml auto, type
) as tabledata
UNION ALL
SELECT
(
select * from Table2 (nolock)
where column1 = 99999 and column2 = -1 for xml auto, type
)
UNION ALL
SELECT
(
select * from Table3 (nolock)
where column1 = 99999 and column2 = -1 for xml auto, type
)
UNION ALL
SELECT
(
select * from Table4 (nolock)
where column1 = 99999 and column2 = -1 for xml auto, type
)
UNION ALL
SELECT
(
select * from Table5 (nolock)
where column1 = 99999 and column2 = -1 for xml auto, type
)
UNION ALL
SELECT
(
select * from Table6 (nolock)
where column1 = 99999 and column2 = -1 for xml auto, type
)
UNION ALL
SELECT
(
select * from Table7 (nolock)
where column1 = 99999 and column2 = -1 for xml auto, type
)
UNION ALL
SELECT
(
select * from Table8 (nolock)
where column1 = 99999 and column2 = -1 for xml auto, type
)
)
select * from resultdata result for xml auto, elements
This returns an XML result that looks like the following (most of the XML redacted, the comments are where the actual data is):
<result>
<tabledata>
<!--Table1 results-->
</tabledata>
</result>
<result>
<tabledata>
<!--Table2 results-->
</tabledata>
</result>
<result>
<tabledata>
<!--Table3 results-->
</tabledata>
</result>
<result>
<tabledata>
<!--Table4 results-->
</tabledata>
</result>
<result>
<tabledata>
<!--Table5 results-->
</tabledata>
</result>
<result>
<tabledata>
<!--Table6 results-->
</tabledata>
</result>
<result>
<tabledata>
<!--Table7 results-->
</tabledata>
</result>
<result>
<tabledata>
<!--Table8 results-->
</tabledata>
</result>
Obviously this is malformed XML, but I can't seem to rework it so that it gives me results in a proper format, something like this:
<result>
<tabledata>
<!--Table1 results-->
</tabledata>
<tabledata>
<!--Table2 results-->
</tabledata>
<tabledata>
<!--Table3 results-->
</tabledata>
<tabledata>
<!--Table4 results-->
</tabledata>
<tabledata>
<!--Table5 results-->
</tabledata>
<tabledata>
<!--Table6 results-->
</tabledata>
<tabledata>
<!--Table7 results-->
</tabledata>
<tabledata>
<!--Table8 results-->
</tabledata>
</result>
As a side note, this is a rewrite of an existing query we have, which is why the syntax is mostly the way it is. A coworker of mine started to rework it and essentially gave me what I have posted here, however I have been struggling with trying to get it to work using his changes. If I need to scrap it though and start from scratch I have no problem with doing that.
Also, as another note, the XML that I want it to look like has to be that way because it is ultimately consumed by an XSLT that I dont have control over. The old version of this query returned each tabledata element as a different column name then did a string replace in the C# code that calls it. There were performance concerns about doing multiple string replaces on this XML (can be 100k+ lines) so the answer was to change the query to return the data the way we need it.