【问题标题】:How to update specific XML attributes in SQL Server?如何更新 SQL Server 中的特定 XML 属性?
【发布时间】:2013-06-22 20:22:46
【问题描述】:

我们将所有不同成本的 XML 文档存储在 SQL Server 2008 r2 中。现在,某些价格需要根据公式进行更新。

一种方法是:

  1. 分解 XML 并访问值。
  2. 进行计算
  3. 现在重建 XML 结构。

这是唯一的方法吗?还是有一种更简单的方法可以让我们即时访问这些值并一次性更新整个 XML?对于这种情况,最好的方法是什么?

这是 XML 的示例:

<ItemAttributeData>
 <Section ID="469919" TypeID="61000002">
  <Attributes>
    <Attribute ID="3870" UniqueID="1c71212b-8395-4b43-a44d-9a63374c8c99" VALUE="2" />
    <Attribute ID="3569" UniqueID="c0b754d3-e03e-4d4a-9bc1-dcb11c04535b" VALUE="2" />
    <Attribute ID="1592" UniqueID="2609b344-88b2-44d6-be20-07b634705c30" VALUE="2" />
   </Attributes>
  </Section>
 <Section ID="469920" TypeID="61000001">
   <Attributes>
     <Attribute ID="3702" UniqueID="7cabf9e8-4f60-4e1e-afe5-75b153e8c5eb" VALUE="1000001649" />
     <Attribute ID="3870" UniqueID="595f8e2c-bccb-4fcb-81d4-303fcb7d44c4" VALUE="2" />
     <Attribute ID="3868" UniqueID="2226f6c6-380a-4195-98f7-c6deec84e4e8" VALUE="2" />
     <Attribute ID="3754" UniqueID="bf936c3f-fa96-49b9-8a62-46fb7e0009d5" VALUE="1000001600" />
     <Attribute ID="2204" UniqueID="bdc78784-86ad-4567-bc06-40896d603eaf" VALUE="1" />
     <Attribute ID="3874" UniqueID="8728249f-5e60-4938-a60d-208426fe11c8" VALUE="1" />
    </Attributes>
   </Section>
  <Section ID="469921" TypeID="61000001" />
 </ItemAttributeData>

【问题讨论】:

  • 这块 XML 是否存储在 SQL Server 中数据类型为 XML 的列中?
  • @Stoleg:是的,它是存储在 SQL Server 中的 XML 类型的列

标签: sql sql-server xml sql-server-2008 tsql


【解决方案1】:

您可以使用非标准的XML update command replace value of。喜欢

DECLARE @myDoc xml
SET @myDoc = '<Root>
<Location LocationID="10" 
            LaborHours=".1"
            MachineHours=".2" >Manu 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

SET @myDoc.modify('
  replace value of (/Root/Location[1]/@LaborHours)[1]
  with (
       if (count(/Root/Location[1]/step) > 3) then
         "3.0"
       else
          "1.0"
      )
')
SELECT @myDoc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2012-09-14
    • 2014-03-23
    • 1970-01-01
    • 2016-09-04
    相关资源
    最近更新 更多