Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove column name when selecting from a XMLTYPE column using FOR XML PATH

I have a table with a XMLTYPE column named 'InvoiceXML'.

The data in this column is XML in the form:

<Invoice CustomerNum="1234" >
<CustomDeliveryDetails />
</Invoice>

When I do a

SELECT ... FOR XML PATH(''), ROOT('Invoices') 

I end up with:

<Invoices>
 <InvoiceXML>
  <Invoice CustomerNum="1234" >
  <CustomDeliveryDetails />
  </Invoice>
 </InvoiceXML>
</Invoices>

How do I stop the column name InvoiceXML appearing in the output?

like image 345
David Avatar asked Jan 25 '11 14:01

David


2 Answers

declare @T table (invoiceXML xml)

insert into @T values (
  '<Invoice CustomerNum="1234" >
     <CustomDeliveryDetails />
   </Invoice>
  ')

insert into @T values (
  '<Invoice CustomerNum="4321" >
     <CustomDeliveryDetails />
   </Invoice>
  ')

select (select T.invoiceXML)
from @T as T
for xml path(''), root('Invoices')

Edit 1 The subquery (select T.invoiceXML) has no column name so it is removed.

like image 156
Mikael Eriksson Avatar answered Sep 21 '22 18:09

Mikael Eriksson


Try:

SELECT cast(cast(InvoiceXML as nvarchar(max)) + '' as XML)
FROM whatever
FOR XML PATH(''), ROOT('Invoices')
like image 26
Cade Roux Avatar answered Sep 24 '22 18:09

Cade Roux