Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I need to get the result in the format

HeadId MinExp maxEmp Rate
9 4 5 45
29 6 8 76
and so on..

from the following XML stored in sql server table' column named "Configuration" and whose type is "XML"

XML
<PARAMS>
<Experience HeadId="9" MinExp="4" MaxExp="5" Rate="45" />
<Experience HeadId="29" MinExp="6" MaxExp="8" Rate="76" />
<Experience HeadId="31" MinExp="9" MaxExp="12" Rate="90" />
<Experience HeadId="32" MinExp="13" MaxExp="18" Rate="100" />
<Experience HeadId="33" MinExp="19" MaxExp="50" Rate="200" />
</PARAMS>



Please help me.
Posted
Updated 14-Jul-14 19:56pm
v2
Comments
George Jonsson 15-Jul-14 4:31am    
Do you want to do this in a stored procedure or in, for example, c#?

Here is a solution which will be definitely of help to you :-

SQL
declare @val xml
set @val = '<PARAMS>
			<Experience HeadId="9" MinExp="4" MaxExp="5" Rate="45" />
			<Experience HeadId="29" MinExp="6" MaxExp="8" Rate="76" />
			<Experience HeadId="31" MinExp="9" MaxExp="12" Rate="90" />
			<Experience HeadId="32" MinExp="13" MaxExp="18" Rate="100" />
			<Experience HeadId="33" MinExp="19" MaxExp="50" Rate="200" />
		</PARAMS>';

SELECT  x.node.value('(@HeadId)[1]', 'int') As HeadId,
        x.node.value('(@MinExp)[1]', 'int') AS MinExp,
        x.node.value('(@MaxExp)[1]', 'int') AS MaxExp,
        x.node.value('(@Rate)[1]', 'int') AS Rate
FROM    @val.nodes('/PARAMS/Experience') AS x(node)



Here @val is the xml type variable which holds the xml for me.
We can use any xml type column in place of this variable to get the values from it as requirement.
 
Share this answer
 
v2
 
Share this answer
 

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



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