I have the following table in SQL Server 2014:
Table [Product]
Serie, Int (primary key column)
Name, varchar(100)
LeftMenu, xml
Here is an example of the XML in the LeftMenu column each row could have:
<menu>
<e>
<col>1</col>
<url>
/products/pressure-relief/pressure-relief-valves/general-info
</url>
<IDElement>General-Info
</IDElement>
</e>
<e>
<col>2</col>
<url>
/products/pressure-relief/pressure-relief-valves/parts
</url>
<IDElement>parts
</IDElement>
</e>
</menu>
The expected result is like the following
Serie | col | name
-------------------
1000 | 1 | parts
From a given Serie (the primary key), I want to get the value of the node <col> passing the value of the <IDElement> tag.
It is like searching inside each <IDElement> in the XML and return the value of the <col> tag that matches that group of elements.
I am trying the following but somehow it is not working:
select p.serie, p.name,
pref.value('(col())[1]', 'varchar(max)') as MenuName,
from prodInfo p
p.[left-menu].nodes('menu/e') as names (pref)
WHERE
pre.value('(IDElement())[1]', 'varchar(max)') == @IDElement
AND p.serie =@serie
Could you please tell me what is wrong?
Other option my partner suggested is to do it as in old times and create a new table instead of using XML, any suggestions?
Thank you!