I have a query like the following:
;WITH XMLNAMESPACES ( DEFAULT 'http://www.somewhere.com')
SELECT ( 'SOMETHING' )
FOR XML PATH('RootNode'), TYPE
Running this works fine. However, I run into troubles when I try to set the XML output to a variable like this:
DECLARE @MYXML AS XML
SELECT @MYXML = (
;WITH XMLNAMESPACES ( DEFAULT 'http://www.somewhere.com')
SELECT ( 'SOMETHING' )
FOR XML PATH('RootNode'), TYPE
)
This just give me a syntax error :-( Any ideas on how to accomplish this would be greatly appreciated.
To add namespaces to the XML constructed by the FOR XML query, first specify the namespace prefix to URI mappings by using the WITH NAMESPACES clause. Then, use the namespace prefixes in specifying the names in the query as shown in the following modified query.
When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".
DECLARE @MYXML AS XML
;WITH XMLNAMESPACES ( DEFAULT 'http://www.somewhere.com')
SELECT @MYXML = (
SELECT ( 'SOMETHING' )
FOR XML PATH('RootNode'), TYPE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With