我需要一个 SQL 查询来从 XML 元素中获取最后一个属性值
I need a SQL query to fetch last attribute value from an XML element
我需要一个 SQL 查询来从 XML 元素中获取最后一个属性值。我有 XML 如下所示。我需要从 legalNames/Name 路径中获取 'Current State Contracting' 的名称值。对此的任何帮助表示赞赏..
<legalNames id="l5922F64208E14D97BEE4C2E94B670146" deleted="0">
<operatingNames id="o370FA0A9EAE54290B132211A1EB28D8D" deleted="0">
<Name>CSR Contracting</Name>
</operatingNames>
<Name prior="" id="N45BF84B89B4F4DB987C07EF568E585D9">Current State Contracting</Name>
</legalNames>
像这样尝试(SQL-服务器语法):
DECLARE @xml XML=
'<legalNames id="l5922F64208E14D97BEE4C2E94B670146" deleted="0">
<operatingNames id="o370FA0A9EAE54290B132211A1EB28D8D" deleted="0">
<Name>CSR Contracting</Name>
</operatingNames>
<Name prior="" id="N45BF84B89B4F4DB987C07EF568E585D9">Current State Contracting</Name>
</legalNames>';
select @xml.value('(/legalNames/Name)[1]','nvarchar(max)')
抱歉延迟回复,我实际上找到了解决方案。
使用 (.) 而不是列将解决这样的问题 ('(.)[1]', 'varchar(500)')。下面是我从 LegalNames/Name 路径获取所有值的确切代码。
SELECT HistoryID,
x.r.value('(@id)[1]', 'varchar(500)') AS NameID,
x.r.value('(@prior)[1]', 'varchar(500)') AS Name_prior,
x.r.value('(.)[1]', 'varchar(500)') AS Name
From
(select HistoryID,XMLData
FROM [Compass Source_Test].dbo.Temp_History_table_july11) s
CROSS apply s.XMLData.nodes('legalNames/Name') as x(r)
我需要一个 SQL 查询来从 XML 元素中获取最后一个属性值。我有 XML 如下所示。我需要从 legalNames/Name 路径中获取 'Current State Contracting' 的名称值。对此的任何帮助表示赞赏..
<legalNames id="l5922F64208E14D97BEE4C2E94B670146" deleted="0">
<operatingNames id="o370FA0A9EAE54290B132211A1EB28D8D" deleted="0">
<Name>CSR Contracting</Name>
</operatingNames>
<Name prior="" id="N45BF84B89B4F4DB987C07EF568E585D9">Current State Contracting</Name>
</legalNames>
像这样尝试(SQL-服务器语法):
DECLARE @xml XML=
'<legalNames id="l5922F64208E14D97BEE4C2E94B670146" deleted="0">
<operatingNames id="o370FA0A9EAE54290B132211A1EB28D8D" deleted="0">
<Name>CSR Contracting</Name>
</operatingNames>
<Name prior="" id="N45BF84B89B4F4DB987C07EF568E585D9">Current State Contracting</Name>
</legalNames>';
select @xml.value('(/legalNames/Name)[1]','nvarchar(max)')
抱歉延迟回复,我实际上找到了解决方案。 使用 (.) 而不是列将解决这样的问题 ('(.)[1]', 'varchar(500)')。下面是我从 LegalNames/Name 路径获取所有值的确切代码。
SELECT HistoryID,
x.r.value('(@id)[1]', 'varchar(500)') AS NameID,
x.r.value('(@prior)[1]', 'varchar(500)') AS Name_prior,
x.r.value('(.)[1]', 'varchar(500)') AS Name
From
(select HistoryID,XMLData
FROM [Compass Source_Test].dbo.Temp_History_table_july11) s
CROSS apply s.XMLData.nodes('legalNames/Name') as x(r)