【问题标题】:XML query in SQL Server 2005SQL Server 2005 中的 XML 查询
【发布时间】:2013-11-25 13:35:32
【问题描述】:

我正在运行的查询无法正常运行,这是我的代码:

select  a.id, a.userid, c.firstname + ' ' + c.lastname AS Name, a.objectid, a.settings,
CAST(settings as XML).exist('property[@name=''colFirstChoiceVendorPaymentTerms'']') as first,
CAST(settings as XML).exist('property[@name="colSecondChoiceVendorPaymentTerms"]//property[@name=''Visible'' and text()=''true'']') as second,
CAST(settings as XML).exist('property[@name="colThirdChoiceVendorPaymentTerms"]//property[@name=''Visible'' and text()=''true'']') as third
FROM wcukopera05.vstx.dbo.screenlayout a
join    wcuksql01.hrsystem.dbo.person c 
on      a.UserId=c.Id
where   a.objectid = 'gridViewCustomerCurrentRatesCosts'

我有一段很长的 XML,我想检查 XML 中是否存在某些属性,例如 (colThirdChoiceVendorPaymentTerms) 并查看可见性是否为真。

此时我的代码返回所有正确的列,但列“第一”、“第二”和“第三”的值都返回 0。但其中一些应该返回 1...

我不明白为什么它们都返回 0?

我已在此处上传 XML:txt.do/dev1,您将看到 colFirstChoiceVendorPaymentTerms 和 colSecondChoiceVendorPaymentTerms 的可见性设置为 true。但是 colThirdChoiceVendorPaymentTerms 没有可见性。这就是我要检查的。可见性和列是否存在。

再次感谢您的帮助。

【问题讨论】:

    标签: c# mysql sql sql-server xml


    【解决方案1】:
    'property[@name="colSecondChoiceVendorPaymentTerms"]//property[@name=''Visible'' and text()=''true'']'
    

    我不明白为什么它们都返回 0?

    1. 您正在寻找一个属性根节点。
    2. 您正在比较属性 @name,您应该在其中与节点值进行比较
    3. 当您开始查找名称为 Visible 的属性时,您的属性级别太深了。

    你应该看起来像这样。

    '//property[.="colSecondChoiceVendorPaymentTerms"]/../property[@name=''Visible'' and text()=''true'']'
    

    或者你无法使用深度搜索并使用value来获取节点@name="Visible"的值。

    settings.value('(/XtraSerializer/property/property[property/@name="Name" and property = "colSecondChoiceVendorPaymentTerms"]/property[@name = "Visible"])[1]', 'bit')
    

    【讨论】:

      猜你喜欢
      • 2011-01-25
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 2011-04-05
      • 2012-10-19
      相关资源
      最近更新 更多