2

When I try to store a XML in SQL than have an empty Element, SQL just change it and store it with only one tag for the element. For Example the XML to store is:

<ROOT>
    <FIRSTNAME>ROGER</FIRSTNAME>
    <MIDDLENAME></MIDDLENAME>
</ROOT>

Then Sql stored it like

    <ROOT>
        <FIRSTNAME>ROGER</FIRSTNAME>
        <MIDDLENAME />
    </ROOT>

The sql update is just very simple:

UPDATE 
    SESIONESREPORTES
   SET 
    SER_PARAMETROS = '
        <ROOT>
               <FIRSTNAME>ROGER</FIRSTNAME>
               <MIDDLENAME></MIDDLENAME>
        </ROOT>'
 WHERE SER_ID=7

I need like this because I have some query that fails when a element is empty, you can see it here..

Merging many rows in a single

2
  • 1
    How is the data getting into the database? It seems like you will need to modify that code to achieve this.
    – Gramma2005
    May 28, 2013 at 22:02
  • You should have a look at how you extract the data and fix that instead. May 29, 2013 at 10:54

2 Answers 2

4

I don't think you can, looking at the following link:

XML Data Type and Columns

According to this (XML Storage Options Section):

The data is stored in an internal representation that preserves the XML content of the data. This internal representation includes information about the containment hierarchy, document order, and element and attribute values. Specifically, the InfoSet content of the XML data is preserved. For more information about InfoSet, visit http://www.w3.org/TR/xml-infoset. The InfoSet content may not be an identical copy of the text XML, because the following information is not retained: insignificant white spaces, order of attributes, namespace prefixes, and XML declaration.

So the internal storage will strip out all parts it deems unnecessary, the document goes on to state that if you need an exact copy of the XML document and not just the content, you should use either [n]varchar(max) or varbinary(max)

2
<MIDDLENAME></MIDDLENAME>

and

<MIDDLENAME/>

are equivalent; any XML parser will treat them identically - as an empty element. If your query fails on an empty element, it will fail on either of them. You'll need to either rewrite your query to handle empty elements, put some content in the <MIDDLENAME> element, or omit the element entirely (if your query can handle it's absence.)

1
  • In fact, the solution was SELECT @SQL = REPLACE(@SQL,'&#x20;','') in the query.. thx u Will May 29, 2013 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.