sql 使用XQuery方法修改更新XML

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 使用XQuery方法修改更新XML相关的知识,希望对你有一定的参考价值。

/*
You can use the XQuery modify method to insert, replace, and delete parts of XML within SQL Server:

- modify (insert ... into xquery)
- modify (replace xquery with ...)
- modify (delete xquery)

The modify method in XML is used within an outer T-SQL query, so in T-SQL it will always use UPDATE, and then within the XQuery code using the modify method you can then insert, replace or delete).

*/

-- Use modify to insert XML. OrderItems is an XML column in the table. Where OrderID=3, this will use the modify method to INSERT a new item element with attributes id="100" and quantity="3" as the last element into the path /items ([1] indicating the first instance.
UPDATE dbo.SalesOrder2
SET OrderItems.modify	('insert <item id="100" quanitity="3"/>
			as last
			into (/items[1])')
WHERE OrderID=7;

/* Result, note the line where OrderID = 7 now has an added item with id="100" and quantity="3":
OrderID	OrderDate	OrderItems
5	2017-11-05	<items count="2"><item id="561" quantity="1" /><item id="127" quantity="2" /></items>
6	2017-11-05	<items count="1"><item id="129" quantity="1" /></items>
7	2017-11-05	<items count="2"><item id="561" quantity="1" /><item id="167" quantity="1" /><item id="100" quanitity="3" /></items>
*/

-- use modify to insert element with text value. This example takes /items element and inserts a child element called order, which takes the value from the relational column in the table called OrderId
UPDATE dbo.SalesOrder2
SET OrderItems.modify	('insert element order {sql:column("OrderID")}
			as first
			into (/items[1])');

/* Result, note how each items element now has a child element called order, which has a text value that has been copied over from the relational column OrderID:
OrderID	OrderDate	OrderItems
5	2017-11-05	<items count="2"><order>5</order><item id="561" quantity="1" /><item id="127" quantity="2" /></items>
6	2017-11-05	<items count="1"><order>6</order><item id="129" quantity="1" /></items>
7	2017-11-05	<items count="2"><order>7</order><item id="561" quantity="1" /><item id="167" quantity="1" /><item id="100" quanitity="3" /></items>
*/

-- use modify to insert an attribute. This example creates a SQL variable @now which grabs the current datetime. The modify method then uses XQuery insert to insert an attribute lastupdated, which takes its value from the SQL variable @now, which is inserted into the first instance of /items.
DECLARE @now DATETIME = GETDATE();

UPDATE dbo.SalesOrder2
SET OrderItems.modify	('insert attribute lastupdated {sql:variable("@now")}
			into (/items[1])');

/*Result, note how each items element now has an attribute lastupdated which takes the value of the SQL variable @now
OrderID	OrderDate	OrderItems
5	2017-11-05	<items count="2" lastupdated="2017-11-07T08:26:46.823"><order>5</order><item id="561" quantity="1" /><item id="127" quantity="2" /></items>
6	2017-11-05	<items count="1" lastupdated="2017-11-07T08:26:46.823"><order>6</order><item id="129" quantity="1" /></items>
7	2017-11-05	<items count="2" lastupdated="2017-11-07T08:26:46.823"><order>7</order><item id="561" quantity="1" /><item id="167" quantity="1" /><item id="100" quanitity="3" /></items>
*/

-- use modify to update XML.
UPDATE dbo.SalesOrder2
SET OrderItems.modify	('replace value of (/items[1]/@count)
			with "4"')
WHERE OrderID = 7;

/* Result, where the count attribute of items element has been updated to value of 4:
OrderID	OrderDate	OrderItems
7	2017-11-05	<items count="4" lastupdated="2017-11-07T08:26:46.823"><order>7</order><item id="561" quantity="1" /><item id="167" quantity="1" /><item id="100" quanitity="3" /></items>
*/

-- use a function.  
DECLARE @itemcount int;
SELECT @itemcount = OrderItems.value('count(/items[1]/item)', 'int')
FROM dbo.SalesOrder
WHERE OrderID = 3;


-- use modify to delete XML
UPDATE dbo.SalesOrder2
SET OrderItems.modify('delete /items/order');

以上是关于sql 使用XQuery方法修改更新XML的主要内容,如果未能解决你的问题,请参考以下文章

如何在 MS SQL 2005 中使用 SQL XQuery 修改多个节点

XQuery sql Join在join不匹配时插入空节点

使用 XPath/XQuery 过滤 XML 列上的 SQL 查询

SQL语句中column的意思和常用法 是怎么的 ??? 谢谢 麻烦解释下

sql 2005数据库 如何操作xml

XQuery 更新:插入或替换取决于节点是不是存在不可能?