Sql server 在sql中查询XML值

Sql server 在sql中查询XML值,sql-server,xml,Sql Server,Xml,我需要在SQLServer2008中从XML中获取一些信息,但我甚至无法从中获取基本属性。我试过的所有样品都失败了。表名为项,xml列名为数据 简化的xml如下所示: <AnchoredXml xmlns="urn:schema:Microsoft.Rtc.Management.ScopeFramework.2008" SchemaWriteVersion="2"> <Key ScopeClass="Global"> <SchemaId Namespac

我需要在SQLServer2008中从XML中获取一些信息,但我甚至无法从中获取基本属性。我试过的所有样品都失败了。表名为
,xml列名为
数据

简化的xml如下所示:

<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="a3502dd0-8c16-4023-9eea-30ea1c7a3a2b">
        <Topology xmlns="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008">
          <Services>
            <Service RoleVersion="1" ServiceVersion="6" Type="Microsoft.Rtc.Management.Deploy.Internal.ServiceRoles.FileStoreService">
              <ServiceId SiteId="1" RoleName="FileStore" Instance="1" />
              <DependsOn />
              <InstalledOn>
                <ClusterId SiteId="1" Number="1" />
              </InstalledOn>
              <Ports xmlns="urn:schema:Microsoft.Rtc.Management.Deploy.ServiceRoles.2008" />
              <FileStoreService xmlns="urn:schema:Microsoft.Rtc.Management.Deploy.ServiceRoles.2008" ShareName="lyncShare" />
            </Service>
          </Services>
        </Topology>
      </Value>
    </Item>
  </Dictionary>
</AnchoredXml>
我需要的FileStoreService/@ShareName在哪里

我试着从一开始就打印名称空间属性,但是没有示例代码在工作。 几次尝试:

SELECT c.p.value('(@Namespace)[1]', 'varchar(50)') as 'Nmspace'
FROM Item
CROSS APPLY Data.nodes('/AnchoredXml/Key/SchemaId') c(p)
返回空结果集

SELECT Data.value('(/AnchoredXml/Key/SchemaId/@Namespace)[1]', 'varchar(50)') 
FROM Item
返回所有行的NULL

SELECT
It.Data.exist('/AnchoredXml/Key/SchemaId[@Namespace="Microsoft.Rtc.Management.Deploy.Topology.2008"]')
FROM [xds].[dbo].[Item] AS It
返回所有不带引号的行的0(“”)

一个工作的示例代码至少可以获得属性测试,这可能就足够了,剩下的部分我会解决。 你能帮我找出查询中的错误吗?或者你能帮我找出其他问题吗?
谢谢

您忽略了XML文档中的所有XML名称空间!你需要关注并尊重他们

上有XML命名空间:

  • 根节点

    (XML命名空间:
    urn:schema:Microsoft.Rtc.Management.ScopeFramework.2008
  • 子节点

    (xmlns:
    urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008
  • 子节点

    (xmlns:
    urn:schema:Microsoft.Rtc.Management.Deploy.ServiceRoles.2008
试试这个:

-- respect the XML namespaces!!
;WITH XMLNAMESPACES(DEFAULT 'urn:schema:Microsoft.Rtc.Management.ScopeFramework.2008', 
                    'urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008' AS t,
                    'urn:schema:Microsoft.Rtc.Management.Deploy.ServiceRoles.2008' AS fss)
SELECT
    ShareName = Data.value('(/AnchoredXml/Dictionary/Item/Value/t:Topology/t:Services/t:Service/fss:FileStoreService/@ShareName)[1]', 'varchar(50)')
FROM
    dbo.Item
在我的情况下,这返回:

ShareName
-----------
lyncShare

这确实是我问题的原因!非常感谢。只需添加一个过滤器,仅此而已。其中Data.exist(“(/AnchoredXml/Key/SchemaId[@Namespace=“urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008”]”)=1
ShareName
-----------
lyncShare