首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么替换xml类型字段的多个属性值

2012-09-17 
如何替换xml类型字段的多个属性值SQL codeDECLARE @myDoc xmlSET @myDoc RootLocation LocationID10

如何替换xml类型字段的多个属性值

SQL code
 
DECLARE @myDoc xml
SET @myDoc = ' <Root>
<Location LocationID="10"
            LaborHours="1.1"
            MachineHours=".2" >Manufacturing steps are described here.
<step>Manufacturing step 1 at this work center </step>
<step>Manufacturing step 2 at this work center </step>
</Location>
</Root>'
SELECT @myDoc
-- update attribute value
SET @myDoc.modify('
  replace value of (/Root/Location/@LaborHours)[1]
  with    "100.0"
')
SELECT @myDoc



查MSDN知道,replace value of ...可以用来替换xml类型的值,
示例也是摘抄MSDN的,如果要在一条sql里完成对LocationID,LaborHours,MachineHours属性的修改,应该怎么做?

我说过写多条 replace value of (...) with "" 不行。

[解决办法]
DECLARE @myDoc xml
SET @myDoc = '<Root>
<Location LocationID="10" 
LaborHours="1.1"
MachineHours=".2" >Manufacturing steps are described here.
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>'
SELECT @myDoc
-- update attribute value
SET @myDoc.modify('
replace value of (/Root/Location/@LaborHours)[1]
with "100.0"
')
SET @myDoc.modify('
replace value of (/Root/Location/@MachineHours)[1]
with "100.0"
')

SELECT @myDoc

热点排行