<?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>Rob Garrett - Blog &#187; SQL</title>
	<atom:link href="http://blog.robgarrett.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.robgarrett.com</link>
	<description>Software and Technology Tid-bits</description>
	<lastBuildDate>Fri, 03 Feb 2012 21:52:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.robgarrett.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/aab8ef1df0f0164f4c766c98b644e9fb?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Rob Garrett - Blog &#187; SQL</title>
		<link>http://blog.robgarrett.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.robgarrett.com/osd.xml" title="Rob Garrett - Blog" />
	<atom:link rel='hub' href='http://blog.robgarrett.com/?pushpress=hub'/>
		<item>
		<title>T-SQL Transactions</title>
		<link>http://blog.robgarrett.com/2005/12/08/t-sql-transactions/</link>
		<comments>http://blog.robgarrett.com/2005/12/08/t-sql-transactions/#comments</comments>
		<pubDate>Fri, 09 Dec 2005 00:03:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2005/12/08/1744.aspx</guid>
		<description><![CDATA[I do not confess to being a database expert, nor a guru in T-SQL, which is why I have only just found out how crap transactions in T-SQL on SQL Server 2000 can be.&#160; Take a look at the following piece of T-SQL: CREATE TABLE #Foo ( PK int NOT NULL ) CREATE TABLE #Foo2 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1744&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I do not confess to being a database expert, nor a guru in T-SQL, which is why I have only just found out how crap transactions in T-SQL on SQL Server 2000 can be.&nbsp; Take a look at the following piece of T-SQL:</p>
<p>CREATE TABLE #Foo<br />
(<br />
PK int NOT NULL<br />
)</p>
<p>CREATE TABLE #Foo2<br />
(<br />
PK int NOT NULL<br />
)</p>
<p>BEGIN TRANSACTION</p>
<p>INSERT INTO #Foo (PK) VALUES (1)<br />
INSERT INTO #Foo2 (PK) VALUES (NULL)</p>
<p>COMMIT TRANSACTION</p>
<p>SELECT * FROM #Foo</p>
<p>DROP TABLE #Foo2<br />
DROP TABLE #Foo</p>
<p>The above script creates two temporary tables, attempts to insert a value in the first, and then a NULL in the second table.&nbsp; Since #Foo2.PK does not allow NULL values, the second insert fails.&nbsp; I naively assumed that because I have wrapped the insert statements in a transaction, the commit will only complete if no error occurs.&nbsp; This is not the case.&nbsp; The commit statement commits the first insert but not the second &#8211; not exactly what I had in mind.&nbsp;&nbsp; </p>
<p>The problem experienced above, is partially due to bad coding on my part because I did not initially understand the intricacies of how SQL Server 2000 manages transactions &#8211; the user is responsible for managing the roll back of a transaction after a statement error. The correct approach would have been to check the @@ERROR value for non-zero after each statement in the transaction block to see if a roll back was required. This can be a real chore if a transaction has many statements, and especially annoying if the error number of any one statement be reported, and the transaction aborted upon an error (typical behavior).&nbsp; Fortunately, SQL Server 2005 comes to the rescue with try-catch &#8211; similar to that of how ADO.NET deals with transactions.&nbsp; The following code will produce the desired results in SQL Server 2005:</p>
<p>CREATE TABLE #Foo<br />
(<br />
PK int NOT NULL<br />
)</p>
<p>CREATE TABLE #Foo2<br />
(<br />
PK int NOT NULL<br />
)</p>
<p>BEGIN TRANSACTION</p>
<p>BEGIN TRY</p>
<p>INSERT INTO #Foo (PK) VALUES (1)<br />
INSERT INTO #Foo2 (PK) VALUES (NULL)</p>
<p>COMMIT TRANSACTION</p>
<p>END TRY<br />
BEGIN CATCH</p>
<p>ROLLBACK TRANSACTION<br />
PRINT &#8216;It broke&#8217;</p>
<p>END CATCH</p>
<p>SELECT * FROM #Foo</p>
<p>DROP TABLE #Foo2<br />
DROP TABLE #Foo</p>
<p></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1744/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1744/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1744/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1744/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1744/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1744/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1744/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1744/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1744/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1744/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1744/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1744/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1744/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1744/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1744/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1744/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1744&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2005/12/08/t-sql-transactions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>FOR XML and ADO.NET</title>
		<link>http://blog.robgarrett.com/2005/08/01/for-xml-and-ado-net/</link>
		<comments>http://blog.robgarrett.com/2005/08/01/for-xml-and-ado-net/#comments</comments>
		<pubDate>Mon, 01 Aug 2005 19:23:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2005/08/01/1461.aspx</guid>
		<description><![CDATA[My esteemed colleague Sahil Malik has blogged about using FOR XML in SQL server.&#160; The following C# example demonstrates how to retrieve XML data from a SQL Server 2K5 database as XML: using (SqlConnection conn = new SqlConnection(&#8220;&#8230;&#8221;)) { using (SqlCommand cmd = new SqlCommand( &#8220;select&#8217;Rob&#8217; as [Employee/@EmployeeName],&#8217;A cool dude&#8217; as [Employee] FOR XML PATH(&#8216;Staff&#8217;)&#8221;, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1461&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>My esteemed colleague <a href="http://codebetter.com/blogs/sahil.malik/default.aspx">Sahil Malik</a> has <a href="http://codebetter.com/blogs/sahil.malik/archive/2005/08/01/130064.aspx">blogged</a> about using <b>FOR XML</b> in SQL server.&nbsp; The following C# example demonstrates how to retrieve XML data from a SQL Server 2K5 database as XML:</p>
<p>using (SqlConnection conn = new SqlConnection(&#8220;&#8230;&#8221;))<br />
{<br />
    using (SqlCommand cmd = new SqlCommand(<br />
        &#8220;select&#8217;Rob&#8217; as [Employee/@EmployeeName],&#8217;A cool dude&#8217; as [Employee] FOR XML PATH(&#8216;Staff&#8217;)&#8221;, conn))<br />
    {<br />
        conn.Open();<br />
        XmlReader reader = cmd.ExecuteXmlReader();<br />
        while (reader.Read())<br />
        {<br />
            switch (reader.NodeType)<br />
            {<br />
                case XmlNodeType.Element:<br />
                    if (reader.HasAttributes)<br />
                    {<br />
                        Console.Write(&#8220;&#8221;);<br />
                    }<br />
                    else<br />
                        Console.Write(&#8220;&#8221;, reader.Name);<br />
                    break;<br />
                case XmlNodeType.Text:<br />
                    Console.Write(reader.Value);<br />
                    break;<br />
            }<br />
        }<br />
    }<br />
}</p>
<p></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1461/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1461/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1461/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1461/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1461/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1461&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2005/08/01/for-xml-and-ado-net/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Common Table Expressions</title>
		<link>http://blog.robgarrett.com/2005/05/20/common-table-expressions/</link>
		<comments>http://blog.robgarrett.com/2005/05/20/common-table-expressions/#comments</comments>
		<pubDate>Sat, 21 May 2005 01:45:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2005/05/20/661.aspx</guid>
		<description><![CDATA[Today I was delighted to learn about common table expressions in SQL Server 2005. Essentially, CTEs replace the need for cursors when executing statements against rows in a result set. Here are some examples&#8230; Let us assume that we have an employee table, and the employee table lists all employees in the company. Each employee [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=661&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I was delighted to learn about common table expressions in SQL Server 2005. Essentially, CTEs replace the need for cursors when executing statements against rows in a result set. Here are some examples&#8230;</p>
<p>Let us assume that we have an employee table, and the employee table lists all employees in the company. Each employee has a unique ID &#8211; EmployeeID, which is the primary key in the table. Each employee reports to one manager and this is indicated by the managers EmployeeID in the ManagerID column for a given employee. e.g</p>
<table border="1">
<tbody>
<tr>
<td><b>EmployeeID</b></td>
<td><b>Name</b></td>
<td><b>ManagerID</b></td>
</tr>
<tr>
<td><b>1</b></td>
<td>Bob (CEO)</td>
<td>NULL</td>
</tr>
<tr>
<td><b>2</b></td>
<td>Sally</td>
<td>1</td>
</tr>
<tr>
<td><b>3</b></td>
<td>Jim</td>
<td>1</td>
</tr>
<tr>
<td><b>4</b></td>
<td>Paul</td>
<td>2</td>
</tr>
<tr>
<td><b>5</b></td>
<td>Simon</td>
<td>4</td>
</tr>
</tbody>
</table>
<p></p>
<p>As shown above, Bob is the CEO and reports to nobody, Sally and Jim report to Bob, Paul reports to Sally, and Simon reports to Paul.</p>
<p>Let&#8217;s say that we want to list how many employees report to each manager. We could write a query using the <i>group by </i>clause (assuming we don&#8217;t want to join the result set back with the employees table to match the name with EmployeeID), as follows:</p>
<p>SELECT<br />
ManagerID, count(*)<br />
FROM<br />
#employees<br />
WHERE<br />
ManagerID IS NOT NULL<br />
GROUP BY ManagerID</p>
<p>What if we wanted to display those managers that had more than one person reporting to them? This result could be obtained by storing the results of the previous query in a temporary table and then running a separate query, or using a cursor. However, using a CTE it is possible to gain the same result with one statement, as follows: </p>
<p>WITH my_CTE (ManagerID, Employees) AS<br />
(<br />
SELECT<br />
e.ManagerID, count(*) AS Employees<br />
FROM<br />
#employees e<br />
WHERE e.ManagerID IS NOT NULL<br />
GROUP BY e.ManagerID<br />
)<br />
SELECT e.[Name], cte.*<br />
FROM<br />
my_CTE cte<br />
INNER JOIN #employees e ON e.EmployeeID = cte.ManagerID<br />
WHERE<br />
Employees &gt; 1<br />
</p>
<p>How about listing the company hierarchy in as a tabular list, indicating rank, starting with the CEO, and following with the direct reports at each level throughout the company? A CTE is great for this problem:</p>
<p>WITH my_CTE (ManagerID, EmployeeID, Rank) AS<br />
(<br />
SELECT<br />
e.ManagerID, e.EmployeeID, 0 AS Rank<br />
FROM<br />
#employees e<br />
WHERE<br />
e.ManagerID IS NULL<br />
UNION ALL<br />
SELECT<br />
e.ManagerID, e.EmployeeID, cte.Rank + 1<br />
FROM<br />
#employees e<br />
INNER JOIN my_CTE cte ON e.ManagerID = cte.EmployeeID<br />
)<br />
SELECT<br />
e.[Name], cte.*<br />
FROM<br />
my_CTE cte<br />
INNER JOIN #employees e ON e.EmployeeID = cte.EmployeeID<br />
</p>
<p>The last example uses recursion to produce the ranking. This query will work no matter how many employees work for the company and no matter how many levels of management. Prior to CTEs, obtaining the same result would have involved some sophisticated SQL. </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/661/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/661/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/661/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/661/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/661/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/661/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/661/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/661/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/661/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/661/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/661/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/661/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/661/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/661/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/661/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/661/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=661&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2005/05/20/common-table-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
	</channel>
</rss>
