I have got the following XML document shown below stored in the NVARCHAR column xmlfile of my table exampletable. The order of the property-elements differs as well as the amount of elements.
<properties>
<property name="LEAD" newValue="0" oldValue="" />
<property name="CONTACT" newValue="0" oldValue="" />
<property name="PROSPECT" newValue="0" oldValue="" />
</properties>
I'm able to select what I need as long as I specify the correct element with:
SELECT * FROM exampletable
WHERE CONVERT(xml,xmlfile).value('(/properties/property/@name)[1]', 'nvarchar(max)') = 'PROSPECT'
But how can I dynamically look for the needed attribute no matter on which position the element is?
Here is the SQL to create the example:
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>')
Thanks a lot!