<?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>Ryan Cromwell &#187; SQL Server</title>
	<atom:link href="http://blog.cromwellhaus.com/index.php/category/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cromwellhaus.com</link>
	<description>Improving my craft...</description>
	<lastBuildDate>Tue, 07 Feb 2012 15:06:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>STUFF &amp; FOR XML PATH</title>
		<link>http://blog.cromwellhaus.com/2009/03/stuff-amp-for-xml-path/</link>
		<comments>http://blog.cromwellhaus.com/2009/03/stuff-amp-for-xml-path/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 15:15:31 +0000</pubDate>
		<dc:creator>cromwellryan</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">/blogs/ryanc/archive/2009/03/02/stuff-amp-for-xml-path.aspx</guid>
		<description><![CDATA[On the get the job done front, my client had a question today that, while it makes me cringe a little to answer the way I did, it got him moving again. [Thanks to Mike Levy for pointing me in the right direction] My client has a small administrative utility that displays Things.&#160; Things can [...]]]></description>
			<content:encoded><![CDATA[<p>On the get the job done front, my client had a question today that, while it makes me cringe a little to answer the way I did, it got him moving again.</p>
<p>[Thanks to <a href="http://twitter.com/malevy">Mike Levy</a> for pointing me in the right direction]</p>
<p>My client has a small administrative utility that displays Things.&#160; Things can be applied to 0 or more categories.&#160; Looks kinda’ like this:</p>
<p><a href="http://cromwellhaus.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/ryanc/image_5F00_201FA719.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="image" border="0" alt="image" src="http://cromwellhaus.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/ryanc/image_5F00_thumb_5F00_064B70EA.png" width="308" height="101" /></a></p>
<p>He’s using a 3rd party control for the grid itself which provides really easy filtering, but it wouldn’t work with the data structures to which he had been binding.&#160; So he wanted to know how he could return the category listing as a delimited string directly from the T-SQL.</p>
<p>Here’s the gist of his schema (nothing fancy):</p>
<p><a href="http://cromwellhaus.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/ryanc/image_5F00_25FA4AB2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Schema" border="0" alt="Schema" src="http://cromwellhaus.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/ryanc/image_5F00_thumb_5F00_1E02A850.png" width="240" height="142" /></a> </p>
<p>&#160;</p>
<p>How we solved it:</p>
<blockquote><p>SELECT Id, Name, STUFF(      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (SELECT cat.Name + &#8216;; &#8216; AS [text()]      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; FROM Category AS cat       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; INNER JOIN ItemCategories AS ic ON cat.Id = ic.CategoryId      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHERE (ic.ItemId = Item.Id) FOR XML PATH(&#8221;)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ), 1, 0, &#8221;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; )      <br />FROM Item</p>
</blockquote>
<p>Obviously we would like to move the UI junk out of the DB, but he can deal with that when he understands the 3rd party control a little better.&#160; Right now, he’s giving his customers some more features.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cromwellhaus.com/2009/03/stuff-amp-for-xml-path/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server 2000 vs 2005 Lazy XML Validation</title>
		<link>http://blog.cromwellhaus.com/2008/08/sql-server-2000-vs-2005-lazy-xml-validation/</link>
		<comments>http://blog.cromwellhaus.com/2008/08/sql-server-2000-vs-2005-lazy-xml-validation/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 16:55:39 +0000</pubDate>
		<dc:creator>cromwellryan</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">/blogs/ryanc/archive/2008/08/19/sql-server-2000-vs-2005-lazy-xml-validation.aspx</guid>
		<description><![CDATA[Someone was looking to do quick-and-dirty validation of an Xml document inside a SQL Server procedure today.&#160; I assumed, and regretted, that he meant 2005, which would have looked like this: declare @xml1 xml&#160; declare @xml2 xml&#160; set @xml1 = &#8216;&#60;Node1&#62;&#60;Node2 attrib=&#8221;This is it.&#8221; /&#62;&#60;/Node1&#62;&#8217;set @xml2 = &#8216;&#60;Node1&#62;&#60;Node2 /&#62;&#60;/Node1&#62;&#8217;select Col.value(&#8216;count(./@attrib)&#8217;, &#8216;int&#8217;) as countfrom @xml1.nodes(&#8216;Node1/Node2&#8242;) as [...]]]></description>
			<content:encoded><![CDATA[<p>Someone was looking to do quick-and-dirty validation of an Xml document inside a SQL Server procedure today.&nbsp; I assumed, and regretted, that he meant 2005, which would have looked like this:</p>
<blockquote><p>declare @xml1 xml&nbsp; <br />declare @xml2 xml&nbsp; <br />set @xml1 = &#8216;&lt;Node1&gt;&lt;Node2 attrib=&#8221;This is it.&#8221; /&gt;&lt;/Node1&gt;&#8217;<br />set @xml2 = &#8216;&lt;Node1&gt;&lt;Node2 /&gt;&lt;/Node1&gt;&#8217;<br />select Col.value(&#8216;count(./@attrib)&#8217;, &#8216;int&#8217;) as count<br />from @xml1.nodes(&#8216;Node1/Node2&#8242;) as Tbl(Col)
<p>select Col.value(&#8216;count(./@attrib)&#8217;, &#8216;int&#8217;) as count<br />from @xml2.nodes(&#8216;Node1/Node2&#8242;) as Tbl(Col)</p>
</blockquote>
<p>After being told, &#8220;that blows up&#8221;, I jogged the memory a bit and came up with this mess:</p>
<blockquote><p>/* THE STUPID WAY */<br />DECLARE @hdoc int<br />DECLARE @doc1 varchar(MAX)<br />DECLARE @doc2 varchar(MAX)<br />set @doc1 = &#8216;&lt;Node1&gt;&lt;Node2 attrib=&#8221;This is it.&#8221; /&gt;&lt;/Node1&gt;&#8217;<br />set @doc2 = &#8216;&lt;Node1&gt;&lt;Node2 /&gt;&lt;/Node1&gt;&#8217;<br />/* DOC 1 */<br />EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc1
<p>IF EXISTS( SELECT&nbsp;&nbsp;&nbsp; *<br />FROM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OPENXML (@hdoc, &#8216;/Node1/Node2/@attrib&#8217;,1)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WITH (attrib&nbsp; varchar(20)))<br />BEGIN<br />PRINT &#8216;Dumb ass&#8217;<br />END<br />exec sp_xml_removedocument @hdoc
<p>/* DOC 2 */<br />EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc2
<p>IF EXISTS( SELECT&nbsp;&nbsp;&nbsp; *<br />FROM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OPENXML (@hdoc, &#8216;/Node1/Node2/@attrib&#8217;,1)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WITH (attrib&nbsp; varchar(20)))<br />BEGIN<br />PRINT &#8216;Dumb ass&#8217;<br />END<br />exec sp_xml_removedocument @hdoc
<p>&nbsp;</p>
</blockquote>
<p>Moral of the store?&nbsp; UPGRADE!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cromwellhaus.com/2008/08/sql-server-2000-vs-2005-lazy-xml-validation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

