Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Adding New Elements to Existing XML Data Expand / Collapse
Author
Message
Posted Friday, September 07, 2012 11:28 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 10:31 AM
Points: 30, Visits: 189
say I've got an XML column containing something like this:
<doc>
<heading>
<orderid>1</orderid>
</heading>
<line>
<lineid>1</lineid>
</line>
<line>
<lineid>2</lineid>
</line>
</doc>

Now I am going to change the scheme to require a new element and I need to update all existing records with a new element and default value.

If the element will be a child of heading
<heading>
<orderid>1</orderid>
<headinginfo>default</headinginfo>
</heading>

which can only appear once I can use "update modify insert" method which has been discussed here and elsewhere.

However if the new element is a child of a repeatable element such as line
<line>
<lineid>1</lineid>
<lineinfo>default</lineinfo>
</line>
<line>
<lineid>2</lineid>
<lineinfo>default</lineinfo>
</line>

That method cannot work since insert will fail if it finds multiple insert locations.[A substantial shortcoming IMO.]

In practice my XML is much more complicated so I could not reasonable shred and flatten to do the change and the reconstruct via SQL.

So doubting I'm the first there to run into this, I'm wondering if anyone has a method they have found to efficiently make such changes.

Brute force would be to pull the xml out of record method, apply a transform, externally, and then update it back in. Can we do better?

[Edit: Maybe using regex viable.]

Thanks in advance.
Post #1356136
Posted Friday, September 07, 2012 1:02 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:36 AM
Points: 2,418, Visits: 1,565
I would use an XQuery flwor expression in your scenario. Here is an example to remodel the xml that you have provided and add the extra nodes to the xml schema:

DECLARE @XML XML
SET @XML = '
<doc>
<heading>
<orderid>1</orderid>
</heading>
<line>
<lineid>1</lineid>
</line>
<line>
<lineid>2</lineid>
</line>
</doc>'


SELECT @XML.query('for $x in /doc
return
<doc>
<heading>
{$x/heading/*}
<headinginfo>default</headinginfo>
</heading>
{
for $y in /doc/line
return
<line>
{$y/*}
<lineinfo>default</lineinfo>
</line>
}
</doc>
')

Post #1356196
Posted Monday, September 10, 2012 8:52 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 10:31 AM
Points: 30, Visits: 189
arthurolcot (9/7/2012)
I would use an XQuery for expression in your scenario.


I love the solution for the simple example. Sadly the XML I've actually got is, i think, far to large and complex to sue this method.

Thanks.
Post #1356797
Posted Monday, September 10, 2012 9:02 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:36 AM
Points: 2,418, Visits: 1,565
ok, without seeing more of the actual xml it is hard for me to comment on that.

However, have you seen this thread:

http://www.sqlservercentral.com/Forums/Topic1311855-21-1.aspx#bm1312389

I came up with this for someone who had similar requirements to yourself, but this adds a new attribute into every node rather than an element. It could be adapted to add an element instead. If nothing else, it may give you some further ideas on how you could achieve your requirements.
Post #1356805
Posted Monday, September 10, 2012 9:15 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 10:31 AM
Points: 30, Visits: 189
arthurolcot (9/10/2012)
ok, without seeing more of the actual xml it is hard for me to comment on that.

Fair enough.


I did review that thread before posting here. It is what inspired the line in my OP, "In practice my XML is much more complicated so I could not reasonably shred and flatten to do the change and the reconstruct via SQL."

I'm considering a regex solution, I ran across a mention this moring of mdq.XmlTransform at http://beyondrelational.com/modules/2/blogs/111/posts/10204/sql-2008-r2-mds-easter-egg.aspx. Our servers are still running 2008, so that probably doesn't get em there. But I'll be finding out why we haven't upgraded.

Thanks!

Post #1356814
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse