繁体   English   中英

在其中存在命名空间的SQL Server XML查询

[英]SQL Server XML query with namespaces in the where exist

我正在尝试使用where子句中的exist()方法在SQL Server中查询xml。 查询的选择部分将首先出现“ SourceIndex”,但where子句完全没有影响。 我想第一次出现“ SourceIndex”,其中“ Source”是给定的OID。 我也查看了nodes()方法,但也无法通过where子句实现该功能。

这是我的查询

 Create table #temp ( identXml xml)
Select 
        #temp.identXml.value('(/*:PersonIdentity/*:MasterIndexes/*:PersonIndex/*:SourceIndex)[1]','varchar(100)') as Ident
        ,#temp.identXml.value('(/*:PersonIdentity/*:MasterIndexes/*:PersonIndex/*:Source)[1]','varchar(100)') as SourceOID
        from #temp
         WHERE #temp.identXml.exist('(/*:PersonIdentity/*:MasterIndexes/*:PersonIndex/*:Source)[text() = "00.000.000.00.1.3.43.1.1.8.10"]')=1

这是xml的示例

    Declare @xml xml

Set @xml= '<PersonIdentity xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <CurrentID>87bf-4fcb-8dd9-4e2c43ec73ba</CurrentID>
  <MasterIndexes>
    <PersonIndex>
      <CreationDate>2017-04-27 12:00:00 A.M.</CreationDate>
      <Source>3.57.1.3.43.1.1.8.10</Source>
      <SourceIndex>Foo1737</SourceIndex>
      <SourceType>SYS</SourceType>

    </PersonIndex>
    <PersonIndex>
      <CreationDate>2017-04-25 12:00:00 A.M.</CreationDate>
      <Source>3.57.1.3.43.1.4.1.8.6</Source>
      <SourceIndex>Foo002194</SourceIndex>
      <SourceType>Foo2</SourceType>



    </PersonIndex>
    <PersonIndex>
      <CreationDate>2017-04-25 12:00:00 A.M.</CreationDate>
      <Source>3.57.1.3.43.102.1.8.1</Source>
      <SourceIndex>f00189854</SourceIndex>
      <SourceType>SYS</SourceType>

    </PersonIndex>
    <PersonIndex>
      <CreationDate>2017-07-05 12:00:00 A.M.</CreationDate>
      <Source>3.57.1.3.43.2.1.8.6</Source>
      <SourceIndex>foo379</SourceIndex>
      <SourceType>SYS</SourceType>

    </PersonIndex>
  </MasterIndexes>
</PersonIdentity>'


DECLARE @exist BIT;

SET @exist = @xml.exist('(/*:PersonIdentity/*:MasterIndexes/*:PersonIndex/*:Source)[text() = "3.57.1.3.43.1.1.8.10"]');
SELECT @exist;

更新资料

根据下面的反馈,我想出了此SQL,它似乎有效。 我试图将代码发布在下面的注释中,但无法弄清楚格式。

   Select 
    t.c.query('./*:SourceIndex').value('.', 'varchar(50)') as Ident
    From @xml.nodes('/*:PersonIdentity/*:MasterIndexes/*:PersonIndex') as t(c)
    Where  t.c.exist('./Source[text() = "3.57.1.3.43.1.1.8.10"]') =1;

取而代之的#temp.identXml.exist您可能需要使用#temp.identXml.query 您可以在此处阅读有关此内容的更多信息SQL Server XML exist()

我相信你也可以这样使用

Select 
#temp.identXml.value('(/*:PersonIdentity/*:MasterIndexes/*:PersonIndex/*:SourceIndex)[1]','varchar(100)') as Ident
,#temp.identXml.value('(/*:PersonIdentity/*:MasterIndexes/*:PersonIndex/*:Source)[1]','varchar(100)') as SourceOID
from #temp
WHERE #temp.identXml.query('(/*:PersonIdentity/*:MasterIndexes/*:PersonIndex/*:Source)[1]').value('.', 'varchar(100)') = 'Something'

暂无
暂无

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

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