Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am using the XML SET and selecting a records from xml, but problem is that in result i am getting the cartesian product as a result. And i want simple query result.
Following is the code for same

DECLARE @parameters XML SET

@parameters='<Parameter><id>3</id><key>ip</key><val>10.97.151.46</val></Parameter>
<Parameter><id>6</id><key>pno</key><val>107</val></Parameter>'

SELECT RecId.ID.value('.','VARCHAR(20)'), RecKey.KY.value('.','VARCHAR(20)'),
       RecVal.VL.value('.','VARCHAR(20)')
FROM @parameters.nodes('/Parameter/id') as RecId(ID), 
     @parameters.nodes('/Parameter/key') as RecKey(KY),
     @parameters.nodes('/Parameter/val') as RecVal(VL)

Output
=====
VB
3   ip   10.97.151.46
3   pno  10.97.151.46
3   ip   107
3   pno  107
6   ip   10.97.151.46
6   pno  10.97.151.46
6   ip   107
6   pno  107

Expected Output
============
VB
3   ip   10.97.151.46
6   pno  107


Please help me into this.
Thanks.
Posted
Updated 2-Jan-12 19:05pm
v2

1 solution

You don't need three different tables to get the desired result. Consider the following:
SQL
SELECT RecId.ID.query('key').value('.','VARCHAR(20)'),
        RecId.ID.query('val').value('.','VARCHAR(20)')
FROM @parameters.nodes('/Parameter') as RecId(ID)
 
Share this answer
 
Comments
LaxmikantYadav 3-Jan-12 1:29am    
Thank you very much its slove my problem.
My 5
Wendelius 3-Jan-12 8:42am    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900