<?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/"
	>

<channel>
	<title> &#187; XML</title>
	<atom:link href="http://benchmarkitconsulting.com/category/sql-server/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://benchmarkitconsulting.com</link>
	<description></description>
	<lastBuildDate>Mon, 06 Feb 2012 16:34:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Parsing XML into a Table Structure&#8230; Possible? UPDATE</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2009/01/15/parsing-xml-into-a-table-structure-possible-update/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2009/01/15/parsing-xml-into-a-table-structure-possible-update/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 20:04:07 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[Brent Ozar]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=323</guid>
		<description><![CDATA[So going back to http://benchmarkitconsulting.com/colin-stasiuk/2009/01/14/parsing-xml-into-a-table-structure-possible/ Can it be done?  Yes&#8230; it&#8217;s not pretty but yes it can be done.  I emailed this to Brent Ozar to see if he was an XML guru as we had gone back on forth on Twitter about whether or not it&#8217;s possible.  He pointed me to StackOverflow and 1 [...]]]></description>
			<content:encoded><![CDATA[<p>So going back to</p>
<p><span style="text-decoration: underline;"><span style="color: #800080;"><a href="http://benchmarkitconsulting.com/colin-stasiuk/2009/01/14/parsing-xml-into-a-table-structure-possible/" target="_blank">http://benchmarkitconsulting.com/colin-stasiuk/2009/01/14/parsing-xml-into-a-table-structure-possible/</a></span></span></p>
<p>Can it be done?  Yes&#8230; it&#8217;s not pretty but yes it can be done.  I emailed this to Brent Ozar to see if he was an XML guru as we had gone back on forth on Twitter about whether or not it&#8217;s possible.  He pointed me to <a href="http://stackoverflow.com/questions/444678/parsing-xml-into-a-sql-table-without-predefining-structure-possible" target="_blank">StackOverflow</a> and 1 day later a user by the name FreddyB solved the problem.</p>
<p> <code style="font-size: 12px;"><span style="color:blue">DECLARE&nbsp;</span><span style="color:#434343">@tbl_xml&nbsp;</span><span style="color:blue">XML&nbsp; <br />SET&nbsp;</span><span style="color:#434343">@tbl_xml&nbsp;</span><span style="color:blue">=&nbsp;</span><span style="color:gray">( <br />&nbsp;&nbsp;</span><span style="color:blue">SELECT&nbsp;</span><span style="color:#434343">@xml_data</span><span style="color:black">.query</span><span style="color:gray">(</span><span style="color:red">&#39; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;table&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{for&nbsp;$elem&nbsp;in&nbsp;/descendant::node()[local-name()&nbsp;!=&nbsp;&quot;&quot;]&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&lt;row&nbsp;name=&quot;{local-name($elem)}&quot;&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{for&nbsp;$attr&nbsp;in&nbsp;$elem/@* <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&lt;col&nbsp;name=&quot;{local-name($attr)}&quot;&nbsp;value=&quot;{$attr}&quot;&nbsp;/&gt;} <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/row&gt;} <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/table&gt;&#39; <br />&nbsp;&nbsp;</span><span style="color:gray">) <br />) </p>
<p></span><span style="color:blue">DECLARE&nbsp;</span><span style="color:#434343">@sql_def_tbl&nbsp;</span><span style="color:blue">VARCHAR</span><span style="color:gray">(</span><span style="color:blue">MAX</span><span style="color:gray">) <br /></span><span style="color:blue">SELECT&nbsp;</span><span style="color:#434343">@sql_def_tbl&nbsp;</span><span style="color:blue">=&nbsp; <br />&nbsp;&nbsp;</span><span style="color:magenta">COALESCE</span><span style="color:gray">(</span><span style="color:#434343">@sql_def_tbl</span><span style="color:gray">,</span><span style="color:red">&#39;&#39;</span><span style="color:gray">) <br />&nbsp;&nbsp;&nbsp;&nbsp;+</span><span style="color:red">&#39;declare&nbsp;@tbl&nbsp;table&nbsp;(&#39;</span><span style="color:gray">+</span><span style="color:magenta">SUBSTRING</span><span style="color:gray">(</span><span style="color:black">csv</span><span style="color:gray">,</span><span style="color:black">1</span><span style="color:gray">,</span><span style="color:magenta">LEN</span><span style="color:gray">(</span><span style="color:black">csv</span><span style="color:gray">)-</span><span style="color:black">1</span><span style="color:gray">)+</span><span style="color:red">&#39;)&nbsp;&#39; <br />&nbsp;&nbsp;</span><span style="color:blue">FROM&nbsp;</span><span style="color:gray">( <br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:blue">SELECT&nbsp;</span><span style="color:gray">( <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:blue">SELECT&nbsp;</span><span style="color:red">&#39;&#39;</span><span style="color:gray">+</span><span style="color:black">col.value</span><span style="color:gray">(</span><span style="color:red">&#39;@name&#39;</span><span style="color:gray">,</span><span style="color:red">&#39;varchar(max)&#39;</span><span style="color:gray">)+</span><span style="color:red">&#39;&nbsp;varchar(max),&#39; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:blue">FROM&nbsp;</span><span style="color:black">row.nodes</span><span style="color:gray">(</span><span style="color:red">&#39;col&#39;</span><span style="color:gray">)&nbsp;</span><span style="color:black">r</span><span style="color:gray">(</span><span style="color:black">col</span><span style="color:gray">)&nbsp;</span><span style="color:blue">FOR&nbsp;XML&nbsp;</span><span style="color:black">path</span><span style="color:gray">(</span><span style="color:red">&#39;&#39;</span><span style="color:gray">) <br />&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;</span><span style="color:black">csv&nbsp;</span><span style="color:blue">FROM&nbsp;</span><span style="color:#434343">@tbl_xml</span><span style="color:black">.nodes</span><span style="color:gray">(</span><span style="color:red">&#39;//row[1]&#39;</span><span style="color:gray">)&nbsp;</span><span style="color:black">n</span><span style="color:gray">(</span><span style="color:black">row</span><span style="color:gray">) <br />&nbsp;&nbsp;)&nbsp;</span><span style="color:black">x </p>
<p></span><span style="color:blue">DECLARE&nbsp;</span><span style="color:#434343">@sql_ins_rows&nbsp;</span><span style="color:blue">VARCHAR</span><span style="color:gray">(</span><span style="color:blue">MAX</span><span style="color:gray">) <br /></span><span style="color:blue">SELECT&nbsp;</span><span style="color:#434343">@sql_ins_rows&nbsp;</span><span style="color:blue">=&nbsp; <br />&nbsp;&nbsp;</span><span style="color:magenta">COALESCE</span><span style="color:gray">(</span><span style="color:#434343">@sql_ins_rows</span><span style="color:gray">,</span><span style="color:red">&#39;&#39;</span><span style="color:gray">) <br />&nbsp;&nbsp;&nbsp;&nbsp;+</span><span style="color:red">&#39;insert&nbsp;@tbl&nbsp;values&nbsp;(&#39;</span><span style="color:gray">+</span><span style="color:magenta">SUBSTRING</span><span style="color:gray">(</span><span style="color:black">colcsv</span><span style="color:gray">,</span><span style="color:black">1</span><span style="color:gray">,</span><span style="color:magenta">LEN</span><span style="color:gray">(</span><span style="color:black">colcsv</span><span style="color:gray">)-</span><span style="color:black">1</span><span style="color:gray">)+</span><span style="color:red">&#39;)&nbsp;&#39; <br />&nbsp;&nbsp;</span><span style="color:blue">FROM&nbsp;</span><span style="color:gray">( <br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:blue">SELECT&nbsp;</span><span style="color:gray">( <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:blue">SELECT&nbsp;</span><span style="color:red">&#39;&#39;&#39;&#39;</span><span style="color:gray">+</span><span style="color:black">col.value</span><span style="color:gray">(</span><span style="color:red">&#39;@value&#39;</span><span style="color:gray">,</span><span style="color:red">&#39;varchar(max)&#39;</span><span style="color:gray">)+</span><span style="color:red">&#39;&#39;&#39;,&#39; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:blue">FROM&nbsp;</span><span style="color:black">row.nodes</span><span style="color:gray">(</span><span style="color:red">&#39;col&#39;</span><span style="color:gray">)&nbsp;</span><span style="color:black">r</span><span style="color:gray">(</span><span style="color:black">col</span><span style="color:gray">)&nbsp;</span><span style="color:blue">FOR&nbsp;XML&nbsp;</span><span style="color:black">path</span><span style="color:gray">(</span><span style="color:red">&#39;&#39;</span><span style="color:gray">) <br />&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;</span><span style="color:black">colcsv&nbsp;</span><span style="color:blue">FROM&nbsp;</span><span style="color:#434343">@tbl_xml</span><span style="color:black">.nodes</span><span style="color:gray">(</span><span style="color:red">&#39;//row&#39;</span><span style="color:gray">)&nbsp;</span><span style="color:black">t</span><span style="color:gray">(</span><span style="color:black">row</span><span style="color:gray">) <br />&nbsp;&nbsp;)&nbsp;</span><span style="color:black">x </p>
<p></span><span style="color:blue">EXEC&nbsp;</span><span style="color:gray">(</span><span style="color:#434343">@sql_def_tbl&nbsp;</span><span style="color:gray">+&nbsp;</span><span style="color:#434343">@sql_ins_rows&nbsp;</span><span style="color:gray">+&nbsp;</span><span style="color:red">&#39;select&nbsp;*&nbsp;from&nbsp;@tbl&#39;</span><span style="color:gray">)</span></code></p>
<p>Now I&#8217;m sure that now the base structure is there and works there is probably some optimizations that can be made for performance&#8230; but there ya go&#8230; it CAN be done.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Parsing+XML+into+a+Table+Structure%26%238230%3B+Possible%3F+UPDATE+http://tinyurl.com/7egy2w" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/01/15/parsing-xml-into-a-table-structure-possible-update/&amp;title=Parsing+XML+into+a+Table+Structure%26%238230%3B+Possible%3F+UPDATE" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/01/15/parsing-xml-into-a-table-structure-possible-update/&amp;title=Parsing+XML+into+a+Table+Structure%26%238230%3B+Possible%3F+UPDATE" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/01/15/parsing-xml-into-a-table-structure-possible-update/&amp;title=Parsing+XML+into+a+Table+Structure%26%238230%3B+Possible%3F+UPDATE" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2009/01/15/parsing-xml-into-a-table-structure-possible-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parsing XML into a Table Structure&#8230; possible?</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2009/01/14/parsing-xml-into-a-table-structure-possible/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2009/01/14/parsing-xml-into-a-table-structure-possible/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 18:19:15 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=310</guid>
		<description><![CDATA[Well let me elaborate a bit. The use case (which I&#8217;m now hating R.K. for bringing up cause I wont let it go now LOL ) where this applies is&#8230;. &#8220;I want to insert into a temp table the results of a dynamic sql statement&#8221; The caveat being this has to be done WITHOUT knowing [...]]]></description>
			<content:encoded><![CDATA[<p>Well let me elaborate a bit.</p>
<p>The use case (which I&#8217;m now hating R.K. for bringing up cause I wont let it go now LOL <img src='http://benchmarkitconsulting.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) where this applies is&#8230;.</p>
<p>&#8220;I want to insert into a temp table the results of a dynamic sql statement&#8221;</p>
<p>The caveat being this has to be done WITHOUT knowing the structure or being able to predefine it.</p>
<p>This is where I&#8217;m at right now&#8230;.</p>
<div><code style="font-size: 12px;"><span style="color: #000000;"></p>
<div><span style="color: #000000;"> </span></div>
<p></span></code></div>
<p><code style="font-size: 12px;"><span style="color: #000000;"><span style="color: #000000;"><span style="color: #0000ff;">DECLARE </span><span style="color: #434343;">@receiveTable </span><span style="color: #0000ff;">TABLE</span><span style="color: #808080;">(</span><span style="color: #000000;">xml_data </span><span style="color: #0000ff;">XML</span><span style="color: #808080;">)<br />
</span><span style="color: #0000ff;">DECLARE </span><span style="color: #434343;">@xml_data </span><span style="color: #0000ff;">XML<br />
DECLARE </span><span style="color: #434343;">@strSQL </span><span style="color: #0000ff;">NVARCHAR</span><span style="color: #808080;">(</span><span style="color: #000000;">2000</span><span style="color: #808080;">)<br />
</span><span style="color: #0000ff;">SET </span><span style="color: #434343;">@strSQL </span><span style="color: #0000ff;">= </span><span style="color: #ff0000;">&#39;SELECT * INTO #tmp1 FROM sysobjects;DECLARE @tbl TABLE(xml_data xml); DECLARE @xml xml; </span></p>
<p></span></span></p>
<div><span style="color: #ff0000;">Set @xml = (Select * from #tmp1 FOR XML AUTO); INSERT INTO @tbl(xml_data) SELECT @xml; SELECT * FROM @tbl&#39;</span></div>
<p><span style="color: #ff0000;"><span style="color: #0000ff;">INSERT INTO </span><span style="color: #434343;">@receiveTable<br />
</span><span style="color: #0000ff;">EXEC </span><span style="color: #808080;">(</span><span style="color: #434343;">@strSQL</span></p>
<div><span style="color: #808080;">)</span></div>
<p></span><span style="color: #808080;"><span style="color: #0000ff;">SET </span><span style="color: #434343;">@xml_data </span><span style="color: #0000ff;">= </span><span style="color: #808080;">(</span><span style="color: #0000ff;">SELECT </span><span style="color: #808080;">* </span><span style="color: #0000ff;">FROM </span><span style="color: #434343;">@receiveTable</span></p>
<div><span style="color: #808080;">)</span></div>
<p></span><span style="color: #808080;"><span style="color: #0000ff;">SELECT </span><span style="color: #434343;">@xml_data</span></p>
<p></span> </p>
<p></code></p>
<p>So how do I now take the XML and parse it back into a table structure format WITHOUT predefining the structure?  Any XML gurus out there that are reading this&#8230; help would be greatly appreciated!! :)</p>
<p>(Now the question is not whether or not I should or shouldn&#8217;t be doing this with the dynamic sql and the xml and my formatting, etc&#8230; this is strictly a &#8220;Can it be done&#8221; question)</p>
<p>I&#8217;ll also be updating this and posting the solution or my acceptance that it just can&#8217;t be done <img src='http://benchmarkitconsulting.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Parsing+XML+into+a+Table+Structure%26%238230%3B+possible%3F+http://tinyurl.com/ay89ko" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/01/14/parsing-xml-into-a-table-structure-possible/&amp;title=Parsing+XML+into+a+Table+Structure%26%238230%3B+possible%3F" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/01/14/parsing-xml-into-a-table-structure-possible/&amp;title=Parsing+XML+into+a+Table+Structure%26%238230%3B+possible%3F" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/01/14/parsing-xml-into-a-table-structure-possible/&amp;title=Parsing+XML+into+a+Table+Structure%26%238230%3B+possible%3F" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2009/01/14/parsing-xml-into-a-table-structure-possible/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

