Move xml column values ​​to external to query xml path

I want to wrap xml from a column in an out query for xml without the column name as an element.

declare @tab table (col xml)
insert @tab
select '<element/>'

select  'val' AS "@att" , col
from  @tab
for xml path ('ROOT')

This gives

<ROOT att="val">
  <col>
    <element />
  </col>
</ROOT>

but I want

<ROOT att="val">
    <element />
</ROOT>

All help is most appreciated.

+3
source share
2 answers

A bit cleaner method.

SELECT
    'val' AS [@att], 
    [col] AS [*]
FROM
    @tab
FOR XML PATH ('ROOT');
+1
source
select
  'val' AS "@att",
  (select col)
from  @tab
for xml path ('ROOT')
+2
source

All Articles