使用其他列

时间:2017-04-13 16:35:21

标签: sql sql-server xml

我想使用同一个表中另一列的值更新XML类型列中的值。下面的代码不会更新值并且不会返回任何错误。

DDL:

CREATE TABLE dbo.customer
(customer_id INT IDENTITY (1,1) NOT NULL PRIMARY KEY
,customer_q1_value INT NOT NULL
,customer_details XML NOT NULL)

INSERT dbo.customer
SELECT 8, '<Customer><Status>Active</Status><CustomerValue>6</CustomerValue><Category>173</Category></Customer>'
UNION ALL
SELECT 14, '<Customer><Status>Active</Status><CustomerValue>5</CustomerValue><Category>173</Category></Customer>'
UNION ALL
SELECT 3, '<Customer><Status>Active</Status><CustomerValue>5</CustomerValue><Category>173</Category></Customer>'
UNION ALL
SELECT 7, '<Customer><Status>Active</Status><CustomerValue>7</CustomerValue><Category>173</Category></Customer>'

XML:

<Customer>
    <Status>Active</Status>
    <CustomerValue>6</CustomerValue>
    <Category>173</Category>
</Customer>

UPDATE customer
SET customer_details.modify('replace value of(/Customer/CustomerValue/text())[1] with (sql:column("customer_q1_value"))')

0 个答案:

没有答案