【发布时间】:2017-02-12 01:49:42
【问题描述】:
我有一个表,其中包含两个 NUMERIC 类型的字段和一个 XML 类型的字段。这是一个粗略的示例:
CREATE TABLE books (
ID INT NOT NULL,
price NUMERIC(4,2),
discount NUMERIC(2,2),
book XML
);
XML 值看起来像,比如说,
<?xml version="1.0" encoding="UTF-8"?>
<book>
<title>Harry Potter</title>
<author>J K Rowling</author>
<Store>
<Name>Burke and Burkins</Name>
<Address>Some St, Somewhere, Some City</Address>
</Store>
</book>
现在我的问题是,使用xml.modify(),如何在Store 下添加两个xpath,价格和折扣分别来自books.price 和books.discount?
<?xml version="1.0" encoding="UTF-8"?>
<book>
<title>Harry Potter</title>
<author>J K Rowling</author>
<Store>
<Name>Burke and Burkins</Name>
<Address>Some St, Somewhere, Some City</Address>
<Price>value from books.price from the same row</Price>
<Discount>value from books.discount from the same row</Discount>
</Store>
</book>
这是一个粗略的例子,所以请不要担心 XML 数据的来源。假设 book 列已经存在 XML 数据。
我知道如何用静态值更新表格,
UPDATE books
SET book.modify('insert <Price>10.99</Price><Discount>20.00</Discount> after (/book/Store/Address)[1]')
这里不考虑性能。
【问题讨论】:
标签: sql sql-server xml tsql dml