【问题标题】:How to pass a variable into XPath of XQuery in an SQL statement如何在 SQL 语句中将变量传递到 XQuery 的 XPath
【发布时间】:2013-09-11 13:55:45
【问题描述】:

场景:我在 MSSQL 数据库中有一个 xml 列,我必须使用 XQuery 解析该单元格的 XML 数据。

     Xml content : <AnchoredXml xmlns="urn:schema:Microsoft.Rtc.Management.ScopeFramework.2008" SchemaWriteVersion="2">
  <Key ScopeClass="Global">
    <SchemaId Namespace="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008" ElementName="Topology" />
    <AuthorityId Class="Host" InstanceId="00000000-0000-0000-0000-000000000000" />
  </Key>
  <Dictionary Count="1">
    <Item>
      <Key />
      <Value Signature="b1ac04f7-d8f0-4300-86cf-fb2b3383536c">
        <Topology xmlns="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008">
          <InternalDomains AllowAllDomains="false" DefaultDomain="ocsqa.com">
            <InternalDomain Name="ocsqa.com" Authoritative="false" AllowSubDomains="false" />
          </InternalDomains>
          <Sites>
            <CentralSite SiteId="1">
              <Name>LyncSite</Name>
              <Location />
            </CentralSite>
          </Sites>
          <Clusters>

这是该单元格中xml内容的一条数据。

我正在使用下面的查询来遍历上面xml的节点:

select @cluster = @Items.query('/DocItemSet/DocItem/Data/*[@SchemaWriteVersion="2"]/*[2]/*[1]/*[2]/*[1]/*[3]') 

上述查询的输出是:

    <p1:Cluster xmlns:p1="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008" RequiresReplication="true" RequiresSetup="true" Fqdn="XXXX.ocsqa.com">
  <p1:ClusterId SiteId="1" Number="1" />
  <p1:Machine OrdinalInCluster="1" Fqdn=" XXXX.ocsqa.com">
    <p1:NetInterface InterfaceSide="Primary" InterfaceNumber="1" IPAddress="0.0.0.0" />
    <p1:NetInterface InterfaceSide="External" InterfaceNumber="1" IPAddress="0.0.0.0" />
    <p1:NetInterface InterfaceSide="Pstn" InterfaceNumber="1" IPAddress="0.0.0.0" />
  </p1:Machine>
</p1:Cluster>
<p2:Cluster xmlns:p2="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008" RequiresReplication="true" RequiresSetup="true" Fqdn=" XXXX2.ocsqa.com">
  <p2:ClusterId SiteId="1" Number="2" />
  <p2:Machine OrdinalInCluster="1" Fqdn=" XXXX2.ocsqa.com">
    <p2:NetInterface InterfaceSide="Primary" InterfaceNumber="1" IPAddress="0.0.0.0" />
    <p2:NetInterface InterfaceSide="External" InterfaceNumber="1" IPAddress="0.0.0.0" />
    <p2:NetInterface InterfaceSide="Pstn" InterfaceNumber="1" IPAddress="0.0.0.0" />
  </p2:Machine>
</p2:Cluster>
<p3:Cluster xmlns:p3="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008" RequiresReplication="true" RequiresSetup="true" Fqdn=" XXXX2.ocsqa.com">
  <p3:ClusterId SiteId="1" Number="3" />
  <p3:Machine OrdinalInCluster="1" Fqdn=" XXXX2.ocsqa.com" />
</p3:Cluster>

现在使用下面提到的查询:

select @fqdn = @cluster.value('(./*/*/@Fqdn)[1]','nvarchar(20)') Select @fqdn

注意上面查询中突出显示的索引号。 使用这个查询,我们将能够在 xml 中实现第一个可用的集群,同样我也想寻找其他集群。

所以我想在 while 循环中使用这个查询。为此,我必须传递一个变量而不是硬编码的 int 值。类似于以下内容:

select @fqdn = @cluster.value('(./*/*/@Fqdn)[sql:variable("@test")]','nvarchar(20)')

我推荐了一些帖子 How to use XPath with a variable in Oracle XMLTable? http://www.jasonstrate.com/2011/01/xquery-for-the-non-expert-variable-use/

但我收到如下错误:

消息 2389,第 16 级,状态 1,第 35 行 XQuery [value()]: 'value()' 需要一个单例(或空序列),找到类型为 'xdt:untypedAtomic *' 的操作数 如何将变量传递到 SQL 语句的 XQuery 中?

【问题讨论】:

    标签: sql-server xpath xquery sqlxml


    【解决方案1】:

    您需要告诉 SQL Server 您只对单个节点感兴趣。在末尾添加[1]

    @cluster.value('(./*/*/@Fqdn)[sql:variable("@test")][1]','nvarchar(20)')
    

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2023-03-25
      • 2014-08-22
      • 2010-12-27
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      相关资源
      最近更新 更多