![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I need to use XQuery to move a node between two XML data type fields... I know I can delete a node like this: SET MyXMLField1.modify ('delete (//Node[@MyKey="123"])[1]') Where ID = @ID And I know I can insert a node like this: SET MyXMLField1.modify ('insert Node Text="SomeText" /Node into (//Node[@MyKey="123"])[1]') But I need to move the node (maybe delete in one field, save it in memory, and insert into another field). And all the sub-nodes need to go along with it. How can this be done? The XML is always pulled from MyXMLField1, and then is moved into MyXMLField1 or MyXMLField2. |
#3
| |||
| |||
|
|
Derek Hart wrote: I need to use XQuery to move a node between two XML data type fields... I know I can delete a node like this: SET MyXMLField1.modify ('delete (//Node[@MyKey="123"])[1]') Where ID = @ID And I know I can insert a node like this: SET MyXMLField1.modify ('insert Node Text="SomeText" /Node into (//Node[@MyKey="123"])[1]') But I need to move the node (maybe delete in one field, save it in memory, and insert into another field). And all the sub-nodes need to go along with it. How can this be done? The XML is always pulled from MyXMLField1, and then is moved into MyXMLField1 or MyXMLField2. There are functions sql:variable and sql:column that you can use to bring in data from variables or columns. However with SQL Server 2005, it is not supported to use variables of type xml in modify expressions e.g. DECLARE @x1 XML; DECLARE @x2 XML; SET @x1 = N'<root foo>bar</foo /root>'; SET @x2 = N'<root/>'; DECLARE @x3 XML; SET @x3 = @x1.query('root/foo'); SET @x2.modify('insert sql:variable("@x3") into /root'); SELECT @x2; gives an error. I think you mentioned SQL Server 2008, I am not sure they have removed that restriction but you could try the above. -- Martin Honnen --- MVP Data Platform Development http://msmvps.com/blogs/martin_honnen/ |
![]() |
| Thread Tools | |
| Display Modes | |
| |