简体   繁体   English

SQL Server XQuery XML索引

[英]SQL Server XQuery XML indexing

Using SQL Server 2008- 使用SQL Server 2008-

I have XML data stored in a column of my table which is the result of exporting some drawing info: 我将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>

Where the number of to draw individual pieces is unknown. 绘制单张的数量未知。 Currently if I wanted to do something like move the entire drawing 100 units along the X-axis, I have SQL code like: 目前,如果我想做一些事情,例如将整个图形沿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")')

And I essentially do that an arbitrary number of times because I don't know how many nodes actually exist in each drawing. 而且我实际上可以执行任意次,因为我不知道每个图形中实际存在多少个节点。 I am fairly new to SQL, and even more so to XQuery, but is there a better way to go about this problem? 我对SQL相当陌生,对XQuery甚至还很陌生,但是有没有更好的方法解决这个问题?

To be more extensible, the next problem I have is when Devices are drawn on top of this model, they were originally drawn in 2d before being exported to xml files, and so they take on the height value (happens to be the Y-axis in my case) of the first section of the drawing, when the devices X and Z coordinates potentially place it at the end of the entire drawing. 为了更扩展,我遇到的下一个问题是,在此模型上绘制设备时,它们最初是在2d中绘制的,然后才导出到xml文件中,因此它们采用了高度值(以Y轴为准)在我的情况下),当设备X和Z进行协调时,可能会将其放置在整个图的末尾。 This causes some devices to be floating either above or below the models. 这会导致某些设备浮在模型上方或下方。 The only thing I could think to write for this problem is something like: 我唯一想到的就是写这样的东西:

-- 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

But this would involve having to copy and paste that code ~15 times (I'm not sure what the largest number of components a model could have, I have seen 6 on the current project) and changing the index [1] which seems extremely inefficient. 但这将涉及到不得不复制和粘贴该代码约15次(我不确定一个模型可以拥有的最大组件数量,我在当前项目中已经看到了6个组件)并更改了索引[1]效率低下。

The device XML layout is exactly the same as the model, just with a different value for . 设备XML布局与模型完全相同,只是的值不同。

It's not possible to replace multiple values at once in xml in SQL Server, there're a several options . 在SQL Server的xml中,不可能一次替换多个值,有几个选项 In your case I'd suggest to use simple loop: 在您的情况下,我建议使用简单循环:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM