更新xml列的属性值

时间:2014-09-02 18:18:26

标签: sql-server sql-server-2008

我想更新xml列"Value"的属性值。目前它包含"Air Force",我想根据条件"US Air Force"

QuestionID="42" Code="1"更新它

这是我的xml。

create table #temp (id int ,xml_data xml)
insert into #temp values(1,
'<xml>
  <answer QuestionAnswerID="2D62FC6E-3C46-468C-AA09-10F93621A0A5" QuestionID="42" Code="2" Value="09/07/2004" />
  <answer QuestionAnswerID="4DC1C66B-D241-4CD7-9E95-128CF141730D" QuestionID="12" Code="1000.2" />
  <answer QuestionAnswerID="70452906-AAB0-44AE-B7A4-3F80889A5FD5" QuestionID="1" Code="1000.1" />
  <answer QuestionAnswerID="025BEA18-F89D-4FA9-92B0-4B061DDEA875" QuestionID="24" Code="1000.2" />
  <answer QuestionAnswerID="EEE1C9B6-3E3C-4773-854E-54E90AFB9454" QuestionID="42" Code="1" Value="Air Force" />
  <answer QuestionAnswerID="C6372F2B-39B7-4443-A881-5A5C9494C87D" QuestionID="33" Code="1000.2" />
  <answer QuestionAnswerID="1C840888-1849-426D-A01F-6B7B3AE00634" QuestionID="37" Code="1000.2" />
  <answer QuestionAnswerID="9994A5B0-C232-410A-885C-6FC487D2389B" QuestionID="22" Code="1000.2" />
  <answer QuestionAnswerID="73CA5F1D-E5FF-4C5C-8518-7542F99A3953" QuestionID="11" Code="1000.2" />
  <answer QuestionAnswerID="19BBA2EF-7F04-4A50-B283-820403B330E5" QuestionID="18" Code="1000.2" />
  <answer QuestionAnswerID="CF617207-2D17-4ACD-B2B6-83141F0CD6C3" QuestionID="10" Code="1000.2" />
  <answer QuestionAnswerID="8088EB0C-BC69-43B1-98A3-8C1E575792E4" QuestionID="7" Code="1000.2" />
  <answer QuestionAnswerID="33DB4393-1FA6-4A72-ABB1-8DD3AC26716E" QuestionID="13" Code="1000.2" />
  <answer QuestionAnswerID="AF4953E6-69AE-4A1F-8B73-A941C82CC516" QuestionID="21" Code="1000.2" />
  <answer QuestionAnswerID="C3EF05EE-632D-4714-ACAF-B0F10766F459" QuestionID="14" Code="1000.2" />
  <answer QuestionAnswerID="3021AF5E-991F-4423-9F55-BE450F8D8E9C" QuestionID="42" Code="3" Value="09/06/2010" />
  <answer QuestionAnswerID="417B01B3-ACD4-42CD-83E3-CB7CCF8622FA" QuestionID="16" Code="1000.2" />
  <answer QuestionAnswerID="CC534459-F87C-4553-8E05-CDA7F90EEA2A" QuestionID="4" Code="1000.2" />
  <answer QuestionAnswerID="6AF7E20A-9A83-469C-A2DC-D6F2B6CAB32B" QuestionID="15" Code="1000.2" />
  <answer QuestionAnswerID="EB6B09FD-04FF-4946-B37C-D81A63036E7A" QuestionID="5" Code="1000.2" />
  <answer QuestionAnswerID="8D99E329-5CAE-4B5F-9479-D99888DAEF1E" QuestionID="39" Code="1000.2" />
  <answer QuestionAnswerID="E3CE6FC9-A19E-4C32-96B1-FE2D7DE9D94D" QuestionID="8" Code="1000.2" />
</xml>')


insert into #temp values(2,
'<xml>
  <answer QuestionAnswerID="2D62FC6E-3C46-468C-AA09-10F93621A0A5" QuestionID="42" Code="2" Value="09/07/2004" />
  <answer QuestionAnswerID="4DC1C66B-D241-4CD7-9E95-128CF141730D" QuestionID="12" Code="1000.2" />
  <answer QuestionAnswerID="70452906-AAB0-44AE-B7A4-3F80889A5FD5" QuestionID="1" Code="1000.1" />
  <answer QuestionAnswerID="025BEA18-F89D-4FA9-92B0-4B061DDEA875" QuestionID="24" Code="1000.2" />
  <answer QuestionAnswerID="C6372F2B-39B7-4443-A881-5A5C9494C87D" QuestionID="33" Code="1000.2" />
  <answer QuestionAnswerID="1C840888-1849-426D-A01F-6B7B3AE00634" QuestionID="37" Code="1000.2" />
  <answer QuestionAnswerID="9994A5B0-C232-410A-885C-6FC487D2389B" QuestionID="22" Code="1000.2" />
  <answer QuestionAnswerID="73CA5F1D-E5FF-4C5C-8518-7542F99A3953" QuestionID="11" Code="1000.2" />
  <answer QuestionAnswerID="19BBA2EF-7F04-4A50-B283-820403B330E5" QuestionID="18" Code="1000.2" />
  <answer QuestionAnswerID="CF617207-2D17-4ACD-B2B6-83141F0CD6C3" QuestionID="10" Code="1000.2" />
  <answer QuestionAnswerID="8088EB0C-BC69-43B1-98A3-8C1E575792E4" QuestionID="7" Code="1000.2" />
  <answer QuestionAnswerID="33DB4393-1FA6-4A72-ABB1-8DD3AC26716E" QuestionID="13" Code="1000.2" />
  <answer QuestionAnswerID="AF4953E6-69AE-4A1F-8B73-A941C82CC516" QuestionID="21" Code="1000.2" />
  <answer QuestionAnswerID="C3EF05EE-632D-4714-ACAF-B0F10766F459" QuestionID="14" Code="1000.2" />
  <answer QuestionAnswerID="3021AF5E-991F-4423-9F55-BE450F8D8E9C" QuestionID="42" Code="3" Value="09/06/2010" />
  <answer QuestionAnswerID="EEE1C9B6-3E3C-4773-854E-54E90AFB9454" QuestionID="42" Code="1" Value="Air Force" />

  <answer QuestionAnswerID="417B01B3-ACD4-42CD-83E3-CB7CCF8622FA" QuestionID="16" Code="1000.2" />
  <answer QuestionAnswerID="CC534459-F87C-4553-8E05-CDA7F90EEA2A" QuestionID="4" Code="1000.2" />
  <answer QuestionAnswerID="6AF7E20A-9A83-469C-A2DC-D6F2B6CAB32B" QuestionID="15" Code="1000.2" />
  <answer QuestionAnswerID="EB6B09FD-04FF-4946-B37C-D81A63036E7A" QuestionID="5" Code="1000.2" />
  <answer QuestionAnswerID="8D99E329-5CAE-4B5F-9479-D99888DAEF1E" QuestionID="39" Code="1000.2" />
  <answer QuestionAnswerID="E3CE6FC9-A19E-4C32-96B1-FE2D7DE9D94D" QuestionID="8" Code="1000.2" />
</xml>')

请帮我写一个更新查询来执行此操作。我正在使用Sql Server 2008。

1 个答案:

答案 0 :(得分:2)

请尝试以下方法:

update #temp
set xml_data.modify('replace value of (/xml/answer[@QuestionID="42" and @Code="1"]/@Value)[1] with "US Air Force"')

<强>参考文献: