0

我的表中有一个 XML 列,如下所示:

<word A="al"   B="h"   C="Ps" />
<word A="has"  B="es"  C="Pf" /> 
<word A="mom"  B="es"  C="Ph" />

我想像这样更新这个字段:

<word A="al"   B="B1"   C="C1" /> 
<word A="has"  B="B2"  C="C1" /> 
<word A="mom"  B="B2"  C="C2" />

我想通过 SQL Server 中的一个函数来做。

谢谢!

4

2 回答 2

4

正如 Mikael 所说,您需要描述更新背后的逻辑。但是对于上面的预期输出,以下应该有效:

DECLARE @Words xml
SELECT @Words = '
<word A="al"   B="h"   C="Ps" />
<word A="has"  B="es"  C="Pf" />
<word A="mom"  B="es"  C="Ph" />'

SET @Words.modify('replace value of(/word[@A = "al"]/@B)[1] with "B1"')
SET @Words.modify('replace value of(/word[@A = "al"]/@C)[1] with "C1"')
SET @Words.modify('replace value of(/word[@A = "has"]/@B)[1] with "B2"')
SET @Words.modify('replace value of(/word[@A = "has"]/@C)[1] with "C1"')
SET @Words.modify('replace value of(/word[@A = "mom"]/@B)[1] with "B2"')
SET @Words.modify('replace value of(/word[@A = "mom"]/@C)[1] with "C2"')

SELECT @Words
于 2012-04-24T08:13:08.290 回答
1

请在您的 XQuery 中尝试“替换值”。
参考:http: //msdn.microsoft.com/en-us/library/ms190675.aspx

于 2012-04-24T08:08:40.127 回答