-1

我正在尝试在 SQL Server 2005 中查询 XML 列

我有一个非常长的字符串,它存储在一个列中,它是 XML。我想检查某个项目的可见性是否设置为 true。

关于如何做到这一点的任何想法?

我无法发布包含所有 XML 的字符串,因为此堆栈编辑器不喜欢该代码。

谢谢

我附上了一张显示我的选择语句和结果的图片,您可以从案例表达式中看到我正在尝试做什么。

在此处输入图像描述

问题是设置列显示了列的名称,但我想看看该列的可见性(colFirstChoiceVendorPaymentTerms)是否设置为真......

4

2 回答 2

0

试试这个代码:

SELECT 
  CAST(data as XML).exist('//XtraSerializer//property[@name="colFirstChoiceVendorPaymentTerms"]/property[@name=''Visible'' and text()=''true'']') as col1,
  CAST(data as XML).exist('//XtraSerializer//property[@name="colSecondChoiceVendorPaymentTerms"]/property[@name=''Visible'' and text()=''true'']') as col2
  FROM TBL
于 2013-11-22T11:08:31.113 回答
0

对您的问题的回答很大程度上取决于您使用的 xml 架构。例如,如果您使用这样的属性 xml:

'<Item ID="1" Visibility="1" />'

您可以使用此查询过滤项目:

select *
from temp
where data.exist('Item[@Visibility = "1"]') = 1

看到这个sql fiddle demo

在我们没有您的 xml 的确切架构之前,很难更准确地回答。


更新

所以看起来你有 DevExpress 设置 xml 存储在你的数据库中。因此,如果您的简化 xml 是这样的:

<XtraSerializer version="1.0" application="View">
  <property name="Columns" iskey="true" value="9">
    <property name="colFirstChoiceVendorPaymentTerms" isnull="true" iskey="true">
      <property name="Visible">true</property>
    </property>
    <property name="colSecondChoiceVendorPaymentTerms" isnull="true" iskey="true">
      <property name="Visible">false</property>
    </property>
  </property>
</XtraSerializer>

然后你可以像这样查询它:

select
data.value('(XtraSerializer/property/property[@name="colFirstChoiceVendorPaymentTerms"]/property[@name="Visible"])[1]', 'bit') as first,

data.value('(XtraSerializer/property/property[@name="colSecondChoiceVendorPaymentTerms"]/property[@name="Visible"])[1]', 'bit') as second
from temp

sql fiddle demo


更新 2

如果你的数据类型是 text/varchar,试试这个:

select
    c.data_xml.value('(XtraSerializer/property/property[@name="colFirstChoiceVendorPaymentTerms"]/property[@name="Visible"])[1]', 'bit') as first,
    c.data_xml.value('(XtraSerializer/property/property[@name="colSecondChoiceVendorPaymentTerms"]/property[@name="Visible"])[1]', 'bit') as second
from temp as t
    outer apply (select cast(t.data as xml) as data_xml) as c
于 2013-11-22T11:01:35.187 回答