<?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>8bit Blog &#187; Databases</title>
	<atom:link href="http://www.8bit.rs/blog/index.php/category/databases/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.8bit.rs/blog</link>
	<description></description>
	<lastBuildDate>Fri, 05 Feb 2010 12:06:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Common Table Expression For Sorting Hierarchical Records By Depth</title>
		<link>http://www.8bit.rs/blog/index.php/2009/02/common-table-expression-for-sorting-hierarchical-records-by-depth/</link>
		<comments>http://www.8bit.rs/blog/index.php/2009/02/common-table-expression-for-sorting-hierarchical-records-by-depth/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 14:54:11 +0000</pubDate>
		<dc:creator>bdrajer</dc:creator>
				<category><![CDATA[Databases]]></category>

		<guid isPermaLink="false">http://www.8bit.rs/blog/?p=12</guid>
		<description><![CDATA[I tried to find this on the net, and was unable to find a satisfying and simple solution. How do you retrieve hierarchical records (say, from a self-referencing table) sorted by their depth in the hierarchy? Microsoft SQL Server 2005/2008 has a new SQL construct,&#160; Here’s one example: a Category table with an ID (primary [...]]]></description>
			<content:encoded><![CDATA[<p>I tried to find this on the net, and was unable to find a satisfying and simple solution. How do you retrieve hierarchical records (say, from a self-referencing table) sorted by their depth in the hierarchy? Microsoft SQL Server 2005/2008 has a new SQL construct,&#160; Here’s one example: a Category table with an ID (primary key) and ParentID (pointing to the hierarchical parent).</p>
<pre>WITH CategoryCTE(ID, ParentID, Depth)
AS
(
  SELECT ID, ParentID, 0
  FROM Category
  WHERE ParentID IS NULL – root records

  UNION ALL 

  SELECT cRecursive.ID, cRecursive.ParentID, cCte.Depth+1
  FROM Category AS cRecursive JOIN CategoryCTE AS cCte
      ON cRecursive.ParentID = cCte.ID
)
SELECT *
FROM CategoryCTE
ORDER BY Depth</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.8bit.rs/blog/index.php/2009/02/common-table-expression-for-sorting-hierarchical-records-by-depth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
