1

I am selecting 2 sets of columns from the same table, as follows.

select (select a, b, c from T1 FOR XML RAW('CAT1')),
       (select d, e, f from T1 FOR XML RAW('CAT2'))
for XML PATH('Parent')

The result of the query is

<Parent>&lt;CAT1 a="Data1" b="Data2', c="Data3" &gt;&lt;CAT2 d="Data4" e="Data5" f="Data6"&gt;</Parent>

How to avoid? I would get enclosed in a proper parent xml?

Expected Result

<Parent>
   <CAT1 a="Data1" b="Data2', c="Data3">
   <CAT2 d="Data4" e="Data5" f="Data6">
</Parent>

1 Answer 1

2

Use TYPE to specify that the sub queries should return XML.

select (select a, b, c from T1 FOR XML RAW('CAT1'), TYPE),
       (select d, e, f from T1 FOR XML RAW('CAT2'), TYPE)
for XML PATH('Parent')

TYPE Directive in FOR XML Queries

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.