2017-07-19 69 views
0

我试图使用where子句中的exists()方法在SQL Server中查询xml。 查询的select部分首次出现“SourceIndex”,但where子句根本没有任何影响。 我想获得第一次出现的“SourceIndex”,其中“Source”是给定的OID。 我也查看了nodes()方法,但无法使用where子句进行工作。存在名称空间的SQL Server XML查询存在于

这里是我的查询

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; 
+0

你需要XML查询,而不是存在。阅读更多在这里https://stackoverflow.com/questions/8467006/sql-server-xml-exist – BobNoobGuy

+0

感谢@BobNoobGuy似乎工作。 这里是SQL语句 'Select tcquery('./*:SourceIndex')。value('。','varchar(50)')作为Ident 从@ xml.nodes('/ *:PersonIdentity/*:MasterIndexes/*:PersonIndex')as t(c) where tcexist('./ Source [text()=“3.57.1​​.3.43.1.1.8.10”]')= 1; – David

回答

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' 
相关问题