<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Yet2come's Blog</title>
	<atom:link href="http://yet2come.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://yet2come.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Fri, 01 May 2009 10:44:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='yet2come.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Yet2come's Blog</title>
		<link>http://yet2come.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://yet2come.wordpress.com/osd.xml" title="Yet2come&#039;s Blog" />
	<atom:link rel='hub' href='http://yet2come.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Using FOR XML with Sql server</title>
		<link>http://yet2come.wordpress.com/2009/05/01/using-for-xml-with-sql-server/</link>
		<comments>http://yet2come.wordpress.com/2009/05/01/using-for-xml-with-sql-server/#comments</comments>
		<pubDate>Fri, 01 May 2009 10:44:08 +0000</pubDate>
		<dc:creator>yet2come</dc:creator>
				<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://yet2come.wordpress.com/?p=3</guid>
		<description><![CDATA[using For Xml with Sql server 2005<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yet2come.wordpress.com&amp;blog=7576847&amp;post=3&amp;subd=yet2come&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In SQL Server 2000, the <strong>FOR XML </strong>clause was added as an <strong>extension to SELECT</strong>, which <strong>transforms the rowset output to an XML stream</strong>. This is a very powerful extension to the SELECT command. The XML stream thus formed can have the following formants: RAW, AUTO or</p>
<p>EXPLICIT. The new syntax of the SELECT statement is as follows:</p>
<p>====================================================</p>
<p>SELECT</p>
<p>column list</p>
<p>FROM</p>
<p>table list</p>
<p>WHERE</p>
<p>filter criteria</p>
<p>FOR XML RAW | AUTO | EXPLICIT [, XMLDATA] [, ELEMENTS] [, BINARY BASE64]</p>
<p>=======================================================</p>
<p>Out of the RAW / AUTO / EXPLICIT, explicit is the most complex one, but it has many options which are very useful to us. Explicit contains RAW/ AUTO, both. Thus we will pick it at last.</p>
<p><strong>FYI: I have used AdventureWorks database in the examples, which I think is much more better then Northwind database.</strong></p>
<p>The <strong>XML RAW</strong> is the most simplest of the forms and probably the <strong>most fastest of all the options</strong>. This format provides the output of each row within a generic node called &#8220;row&#8221;. All the columns of data are transformed as attributes.</p>
<p>****************************************************************</p>
<p>Select ProductID, [Name] from  Production.Product for XML RAW</p>
<p>[Output]</p>
<p>&lt;row ProductID=&#8221;1&#8243; /&gt;</p>
<p>&lt;row ProductID=&#8221;879&#8243; /&gt;</p>
<p>&lt;row ProductID=&#8221;712&#8243; /&gt;</p>
<p>&lt;row ProductID=&#8221;3&#8243; /&gt;</p>
<p>&lt;row ProductID=&#8221;2&#8243; /&gt;</p>
<p>&lt;row ProductID=&#8221;877&#8243; /&gt;</p>
<p>****************************************************************</p>
<p>The <strong>XML AUTO</strong> is the next form. It provides more control over XML output then RAW mode. In this case, each row is named after the table name from where that row comes from.</p>
<p>************************************************************</p>
<p>Select ProductID, [Name] from  Production.Product for XML AUTO</p>
<p>[Output]</p>
<p>&lt;Production.Product ProductID=&#8221;1&#8243; /&gt;</p>
<p>&lt;Production.Product ProductID=&#8221;879&#8243; /&gt;</p>
<p>&lt;Production.Product ProductID=&#8221;712&#8243; /&gt;</p>
<p>&lt;Production.Product ProductID=&#8221;3&#8243; /&gt;</p>
<p>&lt;Production.Product ProductID=&#8221;2&#8243; /&gt;</p>
<p>&lt;Production.Product ProductID=&#8221;877&#8243; /&gt;</p>
<p>******************************************************************</p>
<p>There is one more difference between <strong>XML RAW and XML AUTO</strong>, is the way they handles <strong>JOINS. </strong>Have a look at the difference :</p>
<p>****************************************************************</p>
<p><strong>XML ROW</strong></p>
<p>SELECT  top 2  PSC.Name AS ProductSubCategoryName, PP.Name AS ProductName</p>
<p>FROM  Production.ProductSubcategory AS PSC INNER JOIN Production.Product AS PP</p>
<p>ON PSC.ProductSubcategoryID = PP.ProductSubcategoryID for XML RAW</p>
<p>[Output]<strong></strong></p>
<p>&lt;row ProductSubCategoryName=&#8221;Road Frames&#8221; ProductName=&#8221;HL Road Frame &#8211; Black, 58&#8243; /&gt;</p>
<p>&lt;row ProductSubCategoryName=&#8221;Road Frames&#8221; ProductName=&#8221;HL Road Frame &#8211; Red, 58&#8243; /&gt;</p>
<p><strong>XML AUTO</strong></p>
<p><strong> </strong></p>
<p>SELECT  top 2  PSC.Name AS ProductSubCategoryName, PP.Name AS ProductName</p>
<p>FROM  Production.ProductSubcategory AS PSC INNER JOIN Production.Product AS PP</p>
<p>ON PSC.ProductSubcategoryID = PP.ProductSubcategoryID for XML AUTO</p>
<p>[OUTPUT]</p>
<p>&lt;PSC ProductSubCategoryName=&#8221;Road Frames&#8221;&gt;</p>
<p>&lt;PP ProductName=&#8221;HL Road Frame &#8211; Black, 58&#8243; /&gt;</p>
<p>&lt;PP ProductName=&#8221;HL Road Frame &#8211; Red, 58&#8243; /&gt;</p>
<p>&lt;/PSC&gt;</p>
<p>*********************************************************************</p>
<p>It is quite easy to observe that <strong>XML RAW</strong> format <em>does not show anything in the output that a JOIN has happened</em>. All the columns are rendered as attribute.</p>
<p>In case of <strong>XML AUTO</strong>, the output is nested within the parent and child elements, which provides a clear view about the relationship between the tables involved in JOIN.</p>
<p><strong>XMLDATA</strong> option will output a whole schema. We will pick it later otherwise, it will confuse everybody.</p>
<p>Just for an example :</p>
<p>**********************************************************************</p>
<p><strong>XML AUTO</strong></p>
<p>select ProductID, [Name] from  Production.Product for XML AUTO, XMLDATA</p>
<p>[OUTPUT]</p>
<p>&lt;Schema xmlns=&#8221;urn:schemas-microsoft-com:xml-data&#8221; xmlns:dt=&#8221;urn:schemas-microsoft-com:datatypes&#8221;&gt;</p>
<p>&lt;ElementType content=&#8221;empty&#8221; model=&#8221;closed&#8221;&gt;</p>
<p>&lt;AttributeType dt:type=&#8221;i4&#8243; /&gt;</p>
<p>&lt;AttributeType dt:type=&#8221;string&#8221; /&gt;</p>
<p>&lt;attribute /&gt;</p>
<p>&lt;attribute /&gt;</p>
<p>&lt;/ElementType&gt;</p>
<p>&lt;/Schema&gt;</p>
<p>&lt;Production.Product xmlns=&#8221;x-schema:#Schema4&#8243; ProductID=&#8221;1&#8243; /&gt;</p>
<p>&lt;Production.Product xmlns=&#8221;x-schema:#Schema4&#8243; ProductID=&#8221;879&#8243; /&gt;</p>
<p>&lt;Production.Product xmlns=&#8221;x-schema:#Schema4&#8243; ProductID=&#8221;712&#8243; /&gt;</p>
<p>&lt;Production.Product xmlns=&#8221;x-schema:#Schema4&#8243; ProductID=&#8221;3&#8243; /&gt;</p>
<p>&lt;Production.Product xmlns=&#8221;x-schema:#Schema4&#8243; ProductID=&#8221;2&#8243; /&gt;</p>
<p>**********************************************************************</p>
<p>We would try for the ELEMENTS option, which makes the out much more understandable then others. Look below for the <strong>ELEMENTS</strong> option:</p>
<p>*********************************************************************</p>
<p><strong>XML RAW</strong></p>
<p>SELECT  top 2  PSC.Name AS ProductSubCategoryName, PP.Name AS ProductName</p>
<p>FROM  Production.ProductSubcategory AS PSC INNER JOIN Production.Product AS PP</p>
<p>ON PSC.ProductSubcategoryID = PP.ProductSubcategoryID for XML RAW, ELEMENTS</p>
<p>[output]</p>
<p>&lt;row&gt;</p>
<p>&lt;ProductSubCategoryName&gt;Road Frames&lt;/ProductSubCategoryName&gt;</p>
<p>&lt;ProductName&gt;HL Road Frame &#8211; Black, 58&lt;/ProductName&gt;</p>
<p>&lt;/row&gt;</p>
<p>&lt;row&gt;</p>
<p>&lt;ProductSubCategoryName&gt;Road Frames&lt;/ProductSubCategoryName&gt;</p>
<p>&lt;ProductName&gt;HL Road Frame &#8211; Red, 58&lt;/ProductName&gt;</p>
<p>&lt;/row&gt;</p>
<p><strong>XML AUTO</strong></p>
<p><strong> </strong></p>
<p>SELECT  top 2  PSC.Name AS ProductSubCategoryName, PP.Name AS ProductName</p>
<p>FROM  Production.ProductSubcategory AS PSC INNER JOIN Production.Product AS PP</p>
<p>ON PSC.ProductSubcategoryID = PP.ProductSubcategoryID for XML AUTO, ELEMENTS</p>
<p>[OUTPUT]</p>
<p>&lt;PSC&gt;</p>
<p>&lt;ProductSubCategoryName&gt;Road Frames&lt;/ProductSubCategoryName&gt;</p>
<p>&lt;PP&gt;</p>
<p>&lt;ProductName&gt;HL Road Frame &#8211; Black, 58&lt;/ProductName&gt;</p>
<p>&lt;/PP&gt;</p>
<p>&lt;PP&gt;</p>
<p>&lt;ProductName&gt;HL Road Frame &#8211; Red, 58&lt;/ProductName&gt;</p>
<p>&lt;/PP&gt;</p>
<p>&lt;/PSC&gt;</p>
<p>******************************************************************</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yet2come.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yet2come.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yet2come.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yet2come.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/yet2come.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/yet2come.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/yet2come.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/yet2come.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yet2come.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yet2come.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yet2come.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yet2come.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yet2come.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yet2come.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yet2come.wordpress.com&amp;blog=7576847&amp;post=3&amp;subd=yet2come&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://yet2come.wordpress.com/2009/05/01/using-for-xml-with-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/46ff0bbb9dec9077f709d1026ac3ea3a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yet2come</media:title>
		</media:content>
	</item>
	</channel>
</rss>
