 




<?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>SQL Server with Mr. Denny &#187; Common Table Expressions</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/sql-server/tag/common-table-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/sql-server</link>
	<description></description>
	<lastBuildDate>Fri, 17 May 2013 17:04:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Back To Basics: Using Common Table Expressions</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/using-common-table-expressions/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/using-common-table-expressions/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 11:00:27 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[Back To Basics]]></category>
		<category><![CDATA[Common Table Expressions]]></category>
		<category><![CDATA[CTE]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[T/SQL]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/using-common-table-expressions/</guid>
		<description><![CDATA[CTEs (Common Table Expressions) are one of the very cool features introduced in SQL Server 2005.  In there simplest most common form, think of them as a temporary single use view who's context is only within the command which follows them directly.]]></description>
				<content:encoded><![CDATA[<p>CTEs (Common Table Expressions) are one of the very cool features introduced in SQL Server 2005.  In there simplest most common form, think of them as a temporary single use view who&#8217;s context is only within the command which follows them directly.  The syntax of a CTE is very basic.</p>
<p><code>WITH <em>CTE_Name </em>(ColumnName, ColumnName) AS<br />
(SELECT *<br />
FROM Table)<br />
SELECT *<br />
FROM CTE_Name</code></p>
<p>The list of column names as part of the CTE defination is optional.  If all the columns are named this portion is not needed.  Here is an example from the AdventureWorks database.</p>
<p><code><font size="2" color="#0000ff">WITH<font size="2"> EmployeeData </font><font size="2" color="#0000ff">AS</font><br />
</font><font size="2">(<br />
</font><font size="2" color="#0000ff">SELECT</font><font size="2"> </font><font size="2">e.[EmployeeID]<br />
,c.[Title]<br />
,c.[FirstName]<br />
,c.[MiddleName]<br />
,c.[LastName]<br />
,c.[Suffix]<br />
,e.[Title] </font><font size="2" color="#0000ff">AS</font><font size="2"> [JobTitle] </font><font size="2">,c.[Phone]<br />
,c.[EmailAddress]<br />
,c.[EmailPromotion]<br />
,a.[AddressLine1]<br />
,a.[AddressLine2]<br />
,a.[City]<br />
,sp.[Name] </font><font size="2" color="#0000ff">AS</font><font size="2"> [StateProvinceName] </font><font size="2">,a.[PostalCode]<br />
,cr.[Name] </font><font size="2" color="#0000ff">AS</font><font size="2"> [CountryRegionName] </font><font size="2">,c.[AdditionalContactInfo]<br />
</font><font size="2" color="#0000ff">FROM</font><font size="2"> [HumanResources].[Employee] e</font><font size="2"><font size="2" color="#0000ff">INNER</font><font size="2"> </font><font size="2" color="#0000ff">JOIN</font><font size="2"> [Person].[Contact] c </font><br />
</font><font size="2" color="#0000ff">ON</font><font size="2"> c.[ContactID] = e.[ContactID]</font><font size="2"><font size="2" color="#0000ff">INNER</font><font size="2"> </font><font size="2" color="#0000ff">JOIN</font><font size="2"> [HumanResources].[EmployeeAddress] ea </font><br />
</font><font size="2" color="#0000ff">ON</font><font size="2"> e.[EmployeeID] = ea.[EmployeeID] </font><font size="2"><font size="2" color="#0000ff">INNER</font><font size="2"> </font><font size="2" color="#0000ff">JOIN</font><font size="2"> [Person].[Address] a </font><br />
</font><font size="2" color="#0000ff">ON</font><font size="2"> ea.[AddressID] = a.[AddressID]</font><font size="2"><font size="2" color="#0000ff">INNER</font><font size="2"> </font><font size="2" color="#0000ff">JOIN</font><font size="2"> [Person].[StateProvince] sp </font><br />
</font><font size="2" color="#0000ff">ON</font><font size="2"> sp.[StateProvinceID] = a.[StateProvinceID]</font><font size="2"><font size="2" color="#0000ff">INNER</font><font size="2"> </font><font size="2" color="#0000ff">JOIN</font><font size="2"> [Person].[CountryRegion] cr </font><br />
</font><font size="2" color="#0000ff">ON</font><font size="2"> cr.[CountryRegionCode] = sp.[CountryRegionCode]</font><font size="2">)<br />
</font><font size="2" color="#0000ff">SELECT<font size="2"> *</font><br />
</font><font size="2" color="#0000ff">FROM<font size="2"> EmployeeData</font><br />
</font><font size="2" color="#0000ff">WHERE<font size="2"> CountryRegionName = </font><font size="2" color="#ff0000">'United States'</font><br />
</font></code></p>
<p>When done correctly CTEs can be used to link back to themselves to join child data up the chain so you can access the parent record. This is called a recursive common table expression and is done with a UNION ALL between two queries within the CTE like so.</p>
<p><code><font size="2" color="#0000ff">WITH</font><font size="2"> DirectReports(ManagerID, EmployeeID, EmployeeLevel) </font><font size="2" color="#0000ff">AS</font><font size="2"> </font><font size="2">(<br />
</font><font size="2" color="#0000ff">SELECT</font><font size="2"> ManagerID, EmployeeID, 0 </font><font size="2" color="#0000ff">AS</font><font size="2"> EmployeeLevel</font><font size="2"><font size="2" color="#0000ff">FROM</font><font size="2"> HumanResources.Employee<font size="2" color="#0000ff">WHERE</font><font size="2"> ManagerID </font><font size="2" color="#0000ff">IS</font><font size="2"> </font><font size="2" color="#0000ff">NULL</font><br />
</font><br />
</font><font size="2"><font size="2" color="#0000ff">UNION</font><font size="2"> </font><font size="2" color="#0000ff">ALL</font><br />
</font><font size="2" color="#0000ff">SELECT</font><font size="2"> e.ManagerID, e.EmployeeID, EmployeeLevel + 1</font><font size="2"><font size="2" color="#0000ff">FROM</font><font size="2"> HumanResources.Employee e</font><br />
</font><font size="2" color="#0000ff">INNER</font><font size="2"> </font><font size="2" color="#0000ff">JOIN</font><font size="2"> DirectReports d</font><font size="2"><font size="2" color="#0000ff">ON</font><font size="2"> e.ManagerID = d.EmployeeID)<br />
</font><br />
</font><font size="2" color="#0000ff">SELECT<font size="2"> ManagerID, EmployeeID, EmployeeLevel </font><br />
</font><font size="2" color="#0000ff">FROM<font size="2"> DirectReports ;</font><br />
</font><font size="2" color="#0000ff">GO</font></code><br />
The first part of the UNION ALL command shows us the top level employees who have no manager. The second query is used to link back to the managers to show the employee information including how many levels down the chain the record is.</p>
<p>Extreme care must be used when using recursive common table expressions as doing this incorrectly can put the SQL Server into a never ending loop while SQL is trying to recurse up the never ending tree.</p>
<p>Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/using-common-table-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
