选择xml文档的特定属性值,并在SQL中存储多个元素

时间:2017-07-24 09:18:40

标签: xml tsql

我已将下面显示的以下XML文档存储在我的表xmlfile的NVARCHAR列exampletable中。 property-elements的顺序和元素的数量不同。

<properties>
    <property name="LEAD" newValue="0" oldValue="" />
    <property name="CONTACT" newValue="0" oldValue="" />
    <property name="PROSPECT" newValue="0" oldValue="" />
</properties>

只要我用以下内容指定正确的元素,我就可以选择我需要的内容:

SELECT * FROM exampletable
WHERE CONVERT(xml,xmlfile).value('(/properties/property/@name)[1]', 'nvarchar(max)') = 'PROSPECT'

但是,无论元素位于哪个位置,我怎样才能动态查找所需的属性?

以下是创建示例的SQL:

CREATE TABLE #exampletable
(     
    ID int IDENTITY(1,1) PRIMARY KEY,
    xmlfile NVARCHAR(max)
);
INSERT INTO #exampletable (xmlfile) 
VALUES ('<properties><property name="LEAD" newValue="0" oldValue="" /><property name="CONTACT" newValue="0" oldValue="" /><property name="PROSPECT" newValue="0" oldValue="" /></properties>')

非常感谢!

2 个答案:

答案 0 :(得分:2)

这很简单XPath,可以在没有任何CROSS APPLY语句的情况下完成。

下面是另一条记录的示例数据,其中&#34; @name = LEAD&#34;元素不是第一个。

-- Sample data with another record
USE tempdb
GO
IF OBJECT_ID('#exampletable') IS NOT NULL DROP TABLE #exampletable;
GO
CREATE TABLE #exampletable
(     
    ID int IDENTITY(1,1) PRIMARY KEY,
    xmlfile NVARCHAR(max)
);
INSERT INTO #exampletable (xmlfile)
VALUES 
('<properties>
<property name="LEAD" newValue="0" oldValue="" />
<property name="CONTACT" newValue="0" oldValue="" />
<property name="PROSPECT" newValue="0" oldValue="" />
</properties>'),
('<properties>
<property name="CONTACT" newValue="10" oldValue="5" />
<property name="LEAD" newValue="2" oldValue="1" />
<property name="PROSPECT" newValue="6" oldValue="" />
</properties>');
GO

-- Solution
SELECT 
  ID,
  oldValue = CAST(XMLfile AS xml).value('(/properties/property[@name="LEAD"]/@oldValue)[1]','varchar(10)'),
  newValue = CAST(XMLfile AS xml).value('(/properties/property[@name="LEAD"]/@newValue)[1]','varchar(10)')
FROM #exampletable;

结果:

ID          oldValue   newValue
----------- ---------- ----------
1                      0
2           1          2

答案 1 :(得分:0)

我自己在https://stackoverflow.com/a/15136634/2893242

的帮助下得到了它
SELECT
    T.N.value('@name', 'varchar(50)') as name,
    T.N.value('@oldValue', 'varchar(50)') as oldValue,
    T.N.value('@newValue', 'varchar(50)') as newValue
FROM exampletable xt
cross apply (select cast(xmlfile as xml)) as S(F) 
cross apply S.F.nodes('/properties/property') T(N)
WHERE T.N.value('@name', 'varchar(50)') = 'LEAD' AND SA.id = @id