4

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!

2
  • Can you add the expected result in table format Commented Oct 10, 2016 at 14:51
  • OK thank you, I've updated the question with the expected result Commented Oct 10, 2016 at 14:58

1 Answer 1

2

I commented the WHERE to illustrate PARTS is Col 2, OR I completely misunderstood your requirements

Declare @YourTable table (Series int,Name varchar(100),LeftMenu xml)
Insert Into @YourTable values
(1000,'Some Series Name','<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>')

Declare @Fetch varchar(100) = 'Parts'

Select A.Series
      ,B.*
 From  @YourTable A
 Cross Apply (
                Select  Col  = B.value('col[1]','int') 
                       ,Name = B.value('IDElement[1]','varchar(100)') 
                       ,URL  = B.value('url[1]','varchar(100)') 
                From LeftMenu.nodes('/menu') AS A(Grp)
                Cross Apply A.Grp.nodes('e') AS B(B)
                --Where  B.value('IDElement[1]','varchar(100)') = @Fetch
             ) B

Returns

Series  Col Name            URL
1000    1   General-Info    /products/pressure-relief/pressure-relief-valves/general-info
1000    2   parts           /products/pressure-relief/pressure-relief-valves/parts
Sign up to request clarification or add additional context in comments.

5 Comments

the example return no values, I had to declare thexml variable, DECLARE @XML xml
@franko_camron Sorry, I made an edit it should be From LeftMenu.nodes('/menu') AS A(Grp)
@franko_camron Updated. Thanks for the catch.
Thank you @john Cappelletti, it throws an error Invalid column name 'LeftMenu', guess because it is out of scope
@franko_camron Runs clean here - new window and all. What is the field name which contains your XML data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.