Reputation: 6290
I have three XML columns in my SQL Table (single row table just used for storing XML) and the XML structure is something like this:
In Column 1 -
<Column1XML>
....
</Column1XML>
In Column 2 -
<Column2XML>
....
</Column2XML>
In Column 3 -
<Column3XML>
....
</Column3XML>
The final resultant XML that I am looking for is:
<SomeTagName>
<Column1XML>
....
</Column1XML>
<Column2XML>
....
</Column2XML>
<Column3XML>
....
</Column3XML>
</SomeTagName>
How can I obtain this required structure without doing string concatenation? I am sure there must be a way out with Typed XML.
Thanks in advance for looking up my question.
Upvotes: 1
Views: 1429
Reputation: 138960
Here are two ways for you.
Sample data
declare @T table
(
Col1 xml,
Col2 xml,
Col3 xml
)
insert into @T values
(
'<Column1XML></Column1XML>',
'<Column2XML></Column2XML>',
'<Column3XML></Column3XML>'
)
Use the root name as column alias
select Col1 as 'SomeTagName',
Col2 as 'SomeTagName',
Col3 as 'SomeTagName'
from @T
for xml path('')
Use *
as column alias and specify a path()
.
select Col1 as '*',
Col2 as '*',
Col3 as '*'
from @T
for xml path('SomeTagName')
Result:
<SomeTagName>
<Column1XML />
<Column2XML />
<Column3XML />
</SomeTagName>
Upvotes: 2