SQL Server 2008-需要基于另一个XML值报告XML列中的值

SQL Server 2008-需要基于另一个XML值报告XML列中的值,xml,sql-server-2008,xpath,Xml,Sql Server 2008,Xpath,我有一列存储XML数据。在这个数据中,当另一个field=xxxx时,我需要报告一个字段 以下是我的XML: <?xml version="1.0" encoding="utf-16"?> <SourceIdentifierPairs xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas"&g

我有一列存储
XML
数据。在这个数据中,当另一个
field=xxxx
时,我需要报告一个字段

以下是我的XML:

<?xml version="1.0" encoding="utf-16"?>
<SourceIdentifierPairs xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas">
  <SourceIdentifierPair>
    <Source>ABCPN</Source>
    <Identifier>123456789</Identifier>
  </SourceIdentifierPair>
  <SourceIdentifierPair>
    <Source>ABCMR</Source>
    <Identifier>000000123654</Identifier>
  </SourceIdentifierPair>
    <SourceIdentifierPair>
    <Source>PRIM</Source>
    <Identifier>00112233</Identifier>
  </SourceIdentifierPair>
</SourceIdentifierPairs>

ABCPN
123456789
ABCMR
000000123654
一本正经的
00112233

当源为
ABCPN
时,我需要拉取
标识符

您要查找的XPath表达式是

//SourceIdentifierPair[Source='ABCPN']/Identifier
确保注册默认名称空间

示例代码:

declare @x xml = '<SourceIdentifierPairs xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas">
  <SourceIdentifierPair>
    <Source>ABCPN</Source>
    <Identifier>123456789</Identifier>
  </SourceIdentifierPair>
  <SourceIdentifierPair>
    <Source>ABCMR</Source>
    <Identifier>000000123654</Identifier>
  </SourceIdentifierPair>
    <SourceIdentifierPair>
    <Source>PRIM</Source>
    <Identifier>00112233</Identifier>
  </SourceIdentifierPair>
</SourceIdentifierPairs>'

--xpath as above, with default namespace specified
;with xmlnamespaces ('http://schemas' as ns1, default 'http://schemas')
select @x.value('(//SourceIdentifierPair[Source=''ABCPN'']/Identifier)[1]', 'nvarchar(32)') IdentifierABCPN

--more complex version of the above code (may be useful should you have more complex requirements in future)
;with xmlnamespaces ('http://schemas' as ns1)
select @x.value('(/ns1:SourceIdentifierPairs/ns1:SourceIdentifierPair[./ns1:Source/text()[.=''ABCPN'']]/ns1:Identifier/text())[1]', 'nvarchar(32)') IdentifierABCPN
declare@xxml='1〕
ABCPN
123456789
ABCMR
000000123654
一本正经的
00112233
'
--xpath如上所述,并指定了默认名称空间
;使用xmlnamespaces('http://schemas'作为ns1,默认'http://schemas')
选择@x.value('(//SourceIdentifierPair[Source='ABCPN']/Identifier)[1],'nvarchar(32)')identificatablecpn
--上述代码的更复杂版本(如果将来有更复杂的需求,可能会很有用)
;使用xmlnamespaces('http://schemas“作为ns1)
选择@x.value(“(/ns1:SourceIdentifierPairs/ns1:SourceIdentifierPairs[。/ns1:Source/text()[.=''ABCPN'')]/ns1:Identifier/text())[1],'nvarchar(32')可识别CPN

您要查找的XPath表达式是

//SourceIdentifierPair[Source='ABCPN']/Identifier
确保注册默认名称空间

示例代码:

declare @x xml = '<SourceIdentifierPairs xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas">
  <SourceIdentifierPair>
    <Source>ABCPN</Source>
    <Identifier>123456789</Identifier>
  </SourceIdentifierPair>
  <SourceIdentifierPair>
    <Source>ABCMR</Source>
    <Identifier>000000123654</Identifier>
  </SourceIdentifierPair>
    <SourceIdentifierPair>
    <Source>PRIM</Source>
    <Identifier>00112233</Identifier>
  </SourceIdentifierPair>
</SourceIdentifierPairs>'

--xpath as above, with default namespace specified
;with xmlnamespaces ('http://schemas' as ns1, default 'http://schemas')
select @x.value('(//SourceIdentifierPair[Source=''ABCPN'']/Identifier)[1]', 'nvarchar(32)') IdentifierABCPN

--more complex version of the above code (may be useful should you have more complex requirements in future)
;with xmlnamespaces ('http://schemas' as ns1)
select @x.value('(/ns1:SourceIdentifierPairs/ns1:SourceIdentifierPair[./ns1:Source/text()[.=''ABCPN'']]/ns1:Identifier/text())[1]', 'nvarchar(32)') IdentifierABCPN
declare@xxml='1〕
ABCPN
123456789
ABCMR
000000123654
一本正经的
00112233
'
--xpath如上所述,并指定了默认名称空间
;使用xmlnamespaces('http://schemas'作为ns1,默认'http://schemas')
选择@x.value('(//SourceIdentifierPair[Source='ABCPN']/Identifier)[1],'nvarchar(32)')identificatablecpn
--上述代码的更复杂版本(如果将来有更复杂的需求,可能会很有用)
;使用xmlnamespaces('http://schemas“作为ns1)
选择@x.value(“(/ns1:SourceIdentifierPairs/ns1:SourceIdentifierPairs[。/ns1:Source/text()[.=''ABCPN'')]/ns1:Identifier/text())[1],'nvarchar(32')可识别CPN

您要查找的XPath表达式是

//SourceIdentifierPair[Source='ABCPN']/Identifier
确保注册默认名称空间

示例代码:

declare @x xml = '<SourceIdentifierPairs xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas">
  <SourceIdentifierPair>
    <Source>ABCPN</Source>
    <Identifier>123456789</Identifier>
  </SourceIdentifierPair>
  <SourceIdentifierPair>
    <Source>ABCMR</Source>
    <Identifier>000000123654</Identifier>
  </SourceIdentifierPair>
    <SourceIdentifierPair>
    <Source>PRIM</Source>
    <Identifier>00112233</Identifier>
  </SourceIdentifierPair>
</SourceIdentifierPairs>'

--xpath as above, with default namespace specified
;with xmlnamespaces ('http://schemas' as ns1, default 'http://schemas')
select @x.value('(//SourceIdentifierPair[Source=''ABCPN'']/Identifier)[1]', 'nvarchar(32)') IdentifierABCPN

--more complex version of the above code (may be useful should you have more complex requirements in future)
;with xmlnamespaces ('http://schemas' as ns1)
select @x.value('(/ns1:SourceIdentifierPairs/ns1:SourceIdentifierPair[./ns1:Source/text()[.=''ABCPN'']]/ns1:Identifier/text())[1]', 'nvarchar(32)') IdentifierABCPN
declare@xxml='1〕
ABCPN
123456789
ABCMR
000000123654
一本正经的
00112233
'
--xpath如上所述,并指定了默认名称空间
;使用xmlnamespaces('http://schemas'作为ns1,默认'http://schemas')
选择@x.value('(//SourceIdentifierPair[Source='ABCPN']/Identifier)[1],'nvarchar(32)')identificatablecpn
--上述代码的更复杂版本(如果将来有更复杂的需求,可能会很有用)
;使用xmlnamespaces('http://schemas“作为ns1)
选择@x.value(“(/ns1:SourceIdentifierPairs/ns1:SourceIdentifierPairs[。/ns1:Source/text()[.=''ABCPN'')]/ns1:Identifier/text())[1],'nvarchar(32')可识别CPN

您要查找的XPath表达式是

//SourceIdentifierPair[Source='ABCPN']/Identifier
确保注册默认名称空间

示例代码:

declare @x xml = '<SourceIdentifierPairs xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas">
  <SourceIdentifierPair>
    <Source>ABCPN</Source>
    <Identifier>123456789</Identifier>
  </SourceIdentifierPair>
  <SourceIdentifierPair>
    <Source>ABCMR</Source>
    <Identifier>000000123654</Identifier>
  </SourceIdentifierPair>
    <SourceIdentifierPair>
    <Source>PRIM</Source>
    <Identifier>00112233</Identifier>
  </SourceIdentifierPair>
</SourceIdentifierPairs>'

--xpath as above, with default namespace specified
;with xmlnamespaces ('http://schemas' as ns1, default 'http://schemas')
select @x.value('(//SourceIdentifierPair[Source=''ABCPN'']/Identifier)[1]', 'nvarchar(32)') IdentifierABCPN

--more complex version of the above code (may be useful should you have more complex requirements in future)
;with xmlnamespaces ('http://schemas' as ns1)
select @x.value('(/ns1:SourceIdentifierPairs/ns1:SourceIdentifierPair[./ns1:Source/text()[.=''ABCPN'']]/ns1:Identifier/text())[1]', 'nvarchar(32)') IdentifierABCPN
declare@xxml='1〕
ABCPN
123456789
ABCMR
000000123654
一本正经的
00112233
'
--xpath如上所述,并指定了默认名称空间
;使用xmlnamespaces('http://schemas'作为ns1,默认'http://schemas')
选择@x.value('(//SourceIdentifierPair[Source='ABCPN']/Identifier)[1],'nvarchar(32)')identificatablecpn
--上述代码的更复杂版本(如果将来有更复杂的需求,可能会很有用)
;使用xmlnamespaces('http://schemas“作为ns1)
选择@x.value(“(/ns1:SourceIdentifierPairs/ns1:SourceIdentifierPairs[。/ns1:Source/text()[.=''ABCPN'')]/ns1:Identifier/text())[1],'nvarchar(32')可识别CPN

所以我理解这一点,但XML会随着每条消息的变化而变化。这些是每天都会出现的记录,我需要提取ABCPN并对其进行报告。所以我理解这一点,但XML会随着每条消息而变化。这些是每天都会出现的记录,我需要提取ABCPN并对其进行报告。所以我理解这一点,但XML会随着每条消息而变化。这些是每天都会出现的记录,我需要提取ABCPN并对其进行报告。所以我理解这一点,但XML会随着每条消息而变化。这些记录每天都有,我需要调出ABCPN并报告。