使用where子句SQL Server选择XML元素

时间:2016-06-13 15:26:18

标签: sql-server xml

我在SQL Server中有一个XML Store,其结构如下:

<ecHeaderData>
    <FirstVersionSource>System1</FirstVersionSource>
    <DMSEntryUserID>jsmith</DMSEntryUserID>
</ecHeaderData>

我想返回DMSEntryUserID不为空的所有行。我想在查询中选择FirstVersionSource和DMSEntryuserID。

这样的事情:

select deal_jacket_xml('/ecHeader/FirstVersionSource') as FirstVersionSource, 
deal_jacket_xml('/ecHeader/DMSEntryUserID') as DMSEntryUserID
from deal_jacket_xml
where deal_jacket_xml('NotBlank(/ecHeader/DMSEntryUserID'))=1
order by [deal_jacket_xml_id] desc

1 个答案:

答案 0 :(得分:0)

这是我想出的。关键是括号和[1]的索引规范:

select distinct deal_jacket_xml.value('(/eContractingData/ecHeaderData/FirstVersionSource)[1]', 'nvarchar(max)') as FirstVersionSource
from deal_jacket_xml
where deal_jacket_xml.value('(/eContractingData/ecHeaderData/FirstVersionSource)[1]', 'nvarchar(max)') is not null
AND deal_jacket_xml.value('(/eContractingData/ecHeaderData/DMSEntryUserID)[1]', 'nvarchar(max)') is not null