The most important question is: Why do you need the declaration?
Important to know: This declaration is not a necessary part of the XML, but a hint for the consumer how to read the content.
I most cases this declaration tells the consumer about a text file's encoding. There are millions of XML files out there yelling encoding="utf8"
or something like this, but - in fact - the file is utf16
, plain ASCII or any other encoding. If the declaration is not telling the truth, it is better not to have such a declaration at all...
Within SQL-Server there are three options to store XML (not talking about binary
or outdated text
here): Best is native XML
, which uses a nvarchar
based hierarchy table under the hood, and (a bad choice, but) often seen is a string based column based on one of the two string types. You must know, that SQL-Server can deal with (var)char(x)
(which is collation extended ASCII), and with n(var)char(x)
, which is UCS-2
(almost the same as utf16
).
What would be the benefit to store XML with a declaration in such a scenario? SQL-Server can take the encoding directly from the type used for storage. On string level you can store literally anything, even invalid / not well-formed XML. But, at the moment you enter the native XML type, SQL-Server will omit any declaration and will refuse rule breaking XML.
To answer your question
The only chance to add the declaration, if you really need it, is to convert your XML to a string type and add the declaration on string level.
Keep in mind that
- you cannot take this back into native XML without losing the declaration.
- If you state any encoding you should make sure, that the written target file is not a liar.
Some years ago I had the same question (needed to add a stylesheet hint).
You might read this related question (and a good answer by har07). My own answer there reflects the <?xml blahblah ?>
specifically.
final hints
Starting with v2019 there is native utf-8
support with some special collations and starting with v2014SP2 there was UTF-8 support for BCP.
You can use this code to find a collation's encoding:
select [name]
,[description]
,COLLATIONPROPERTY([name],'codepage') [codepage]
from sys.fn_helpcollations();
And - following this - you might use something like this
<?xml version="1.0" encoding="windows-1252"?>
To reflect the chosen encoding correctly.
General hint: Using the string-type NVARCHAR(MAX)
together with utf-16
works in (almost) any case.