Click here to Skip to main content
15,921,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All

Please help out with an sql command that gives xml as an resultant(output). Below example give you a brief info about my requirement.

CustNo Pname   Pid
1           Appel     AP
1           Orange     OR
1          Mango     MG
2           Appel     AP
2           Orange     OR
3           Appel     AP


that means, i have three columns. Now i need an Below xml from Sql select command.

XML
<Sample>
<custNo  ID="1">
<seq Appel= "AP" Orange="OR" MANGO="MG"/>
</custNo>
<custNo  ID="2">
<seq Appel= "AP" Orange="OR" />
</custNo>
<custNo  ID="3">
<seq Appel= "AP"/>
</custNo>
</Sample>



Thanks & Regardes
Chiranjeevi
Posted
Comments
CHill60 10-May-13 4:40am    
What have you tried so far?
Zoltán Zörgő 10-May-13 5:48am    
I am sure this can be done in t-sql but do you really need to do this in sql? It is far more easier in an above layer where you have a high level language - this is even more valid if the list of fruits is not limited. I can imagine what you want to achieve, but you could achieve this in some other form, like storing the attributes of a customer in an xml field, and not every property in a record.

SQL
select
	custNo.HUEid
	,( 
		select [Appel],[Orange],[Mango] from 
		(select *  from hue h where HUEid = custNo.HUEid) p
		Pivot(
		max(pid) for HUENome in ( [Appel],[Orange],[Mango])
		) as seq
		For xml auto  ,type 
	)
 from hue custNo
 group by hueid 
For xml auto ,root ('Sample')


This is my T-Sql version using Pivot and For Xml, hope this help =)
 
Share this answer
 
Comments
RedDk 10-May-13 16:29pm    
Wow, certainly insightful ... going to be thinking about this style for a while. Thanks!
Fabricio Dos Santos Antunes 10-May-13 20:28pm    
Thank you, Pivot is very usefull in many ways
VB
// Start XML file, echo parent node
echo '<sample>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE

  echo 'custNo="' . parseToXML($row['ID']) . '" ';
  echo 'seq="' . $row['AP'] . '" ';
  echo '/>';
}

// End XML file
echo '</sample>';
 
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