Monday, March 26, 2012

How to insert a node, with value queried from another node

We're trying to move some of our data between nodes in an XML column.
I basically want to migrate:
FROM
<OldNodeParent>
<OldNode>1234</OldNode>
</OldNodeParent>
TO
<OldNodeParent>
<OldNode>1234</OldNode>
</OldNodeParent>
<NewNode>1234</NewNode>
I was trying something like (which doesn't work):
UPDATE MyTable
SET XmlColumn.modify('
insert <NewNode>(/OldNodeParent/OldNode)[1]</NewNode> as last
into (/)[1]
')
WHERE XmlColumn.exist('/OldNodeParent/OldNode') = 1
AND XmlColumn.exist('/NewNode') = 0
Any ideas on how this could be achieved?
Thanks,
LubdhaTry this
UPDATE MyTable
SET XmlColumn.modify('
insert
if (count(/NewNode) = 0)
then element NewNode {(/OldNodeParent/OldNode/text())[1]}
else ()
as last into (/)[1]
')|||More correctly
UPDATE MyTable
SET XmlColumn.modify('
insert
if ( (/OldNodeParent/OldNode/text()) and not (/NewNode) )
then element NewNode {(/OldNodeParent/OldNode/text())[1]}
else ()
as last into (/)[1]
')|||Wow! That worked like a charm, thanks a lot!

No comments:

Post a Comment