【问题标题】:SQL Server XQuery XML indexingSQL Server XQuery XML 索引
【发布时间】:2015-04-23 13:33:50
【问题描述】:

使用 SQL Server 2008-

我将 XML 数据存储在我的表的一列中,这是导出一些绘图信息的结果:

<layout>
    <config>
        <graphic_type>Box</graphic_type>
        <data_access>
        </data_access>
        <data>
            <dimension x="1" y="2" z="3" />
            <curve_info ir="-1.5" or="1.5" degree="0"/>
            <position x="4" y="5" z="6" />
            <rotation x="7" y="8" z="9" />
            <color>FFD3D3D3</color>
            <is_position_relative>false</is_position_relative>
        </data>
    </config>
    <config>
        ...
    </config>
</layout>

绘制单个棋子的数量是未知的。 目前,如果我想做一些事情,比如将整个绘图沿 X 轴移动 100 个单位,我有如下 SQL 代码:

SET @xTrans = 100
UPDATE TableName
SET xmlColumn.modify('replace value of (//data/position/@x)[1] with sql:variable("@xTrans")')
SET xmlColumn.modify('replace value of (//data/position/@x)[2] with sql:variable("@xTrans")')
SET xmlColumn.modify('replace value of (//data/position/@x)[3] with sql:variable("@xTrans")')
...
SET xmlColumn.modify('replace value of (//data/position/@x)[20] with sql:variable("@xTrans")')

而且我基本上会执行任意次数的操作,因为我不知道每个图形中实际存在多少个节点。 我对 SQL 很陌生,对 XQuery 更是如此,但是有没有更好的方法来解决这个问题?

为了更具可扩展性,我遇到的下一个问题是,在此模型之上绘制设备时,它们最初是在 2d 中绘制的,然后才导出到 xml 文件,因此它们采用高度值(恰好是在我的例子中,Y 轴)在绘图的第一部分,当设备 X 和 Z 坐标可能将其放置在整个绘图的末尾时。这会导致某些设备在模型上方或下方浮动。对于这个问题,我唯一能想到的就是这样写:

-- Determine if moving along X or Z axis by Y rotation
-- If its the Z-axis, find the range that section covers with position+dimension
    -- @range = (///position/@z)[1] + (///dimension/@z)[1]
-- See if the device falls in that range
    -- If (///position/@z)[1] < device position @z < @range
-- Then we need the rotation of that box in the Z-axis
-- to calculate the height change for the device

但这将涉及复制和粘贴该代码约 15 次(我不确定模型可以拥有的最大组件数量,我在当前项目中看到了 6 个)并更改索引 [1]这似乎效率极低。

设备 XML 布局与模型完全相同,只是 . 的值不同。

【问题讨论】:

    标签: sql-server xml xpath xquery sqlxml


    【解决方案1】:

    在 SQL Server 的 xml 中一次替换多个值是不可能的,有一个 several options。在你的情况下,我建议使用简单的循环:

    select @i = max(t.xmlColumn.value('count(//data/position[@x != 100])', 'int'))
    from TableName as t
    
    while @i > 0
    begin
        update TableName set
            xmlColumn.modify('
                replace value of (//data/position[@x != 100])[1]/@x
                with sql:variable("@xTrans")'
            )
    
        set @i = @i - 1
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-31
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多