<?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>Journey of a Software Developer &#187; TSQL</title>
	<atom:link href="http://www.tyronedavisjr.com/category/tsql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tyronedavisjr.com</link>
	<description>HTML &#124; CSS &#124; JavaScript &#124; ASP.NET &#124; PHP &#124; C# &#124; Java &#124; VB.NET &#124; MS SQL &#124; MySQL &#124; Flash</description>
	<lastBuildDate>Sun, 17 Apr 2011 03:51:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>TSQL, Looping through a result set without using database cursor</title>
		<link>http://www.tyronedavisjr.com/2007/12/12/tsql-looping-through-a-result-set-without-using-database-cursor/</link>
		<comments>http://www.tyronedavisjr.com/2007/12/12/tsql-looping-through-a-result-set-without-using-database-cursor/#comments</comments>
		<pubDate>Wed, 12 Dec 2007 18:18:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.tyronedavisjr.com/index.php/2007/12/12/tsql-looping-through-a-result-set-without-using-database-cursor/</guid>
		<description><![CDATA[A few years ago a project that I was working on required me to loop through a result set from a select statement.&#160; Most examples that I saw used database cursors. However, I came across an old article that shows an alternative method for doing this.&#160; Let&#8217;s say we have a table that looks like]]></description>
			<content:encoded><![CDATA[<p>A few years ago a project that I was working on required me to loop through a result set from a select statement.&nbsp; Most examples that I saw used database cursors. However, I came across an old <a href="http://www.databasejournal.com/features/mssql/article.php/3111031" target="_blank">article</a> that shows an alternative method for doing this.&nbsp; Let&#8217;s say we have a table that looks like this:</p>
<p>Table Name : Contacts</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="54"><u>ID</u></td>
<td valign="top" width="94"><u>FirstName</u></td>
<td valign="top" width="93"><u>LastName</u></td>
<td valign="top" width="157"><u>EmailAddress</u></td>
</tr>
<tr>
<td valign="top" width="52">1</td>
<td valign="top" width="94">Jack</td>
<td valign="top" width="93">Bauer</td>
<td valign="top" width="159"><a href="mailto:jbauer@fox24.com">jbauer@fox24.com</a></td>
</tr>
<tr>
<td valign="top" width="52">2</td>
<td valign="top" width="94">Dan</td>
<td valign="top" width="92">Marino</td>
<td valign="top" width="160"><a href="mailto:dmarino@dolphins.com">dmarino@dolphins.com</a></td>
</tr>
<tr>
<td valign="top" width="52">3</td>
<td valign="top" width="94">George</td>
<td valign="top" width="92">Bush</td>
<td valign="top" width="160"><a href="mailto:gbush@whitehouse.gov">gbush@whitehouse.gov</a></td>
</tr>
<tr>
<td valign="top" width="52">4</td>
<td valign="top" width="94">Michael</td>
<td valign="top" width="92">Jordan</td>
<td valign="top" width="160"><a href="mailto:mjordan@bulls.com">mjordan@bulls.com</a></td>
</tr>
<tr>
<td valign="top" width="52">5</td>
<td valign="top" width="103">Bill</td>
<td valign="top" width="108">Clinton</td>
<td valign="top" width="163"><a href="mailto:bj@whitehouse.gov">bj@whitehouse.gov</a></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>Please don&#8217;t pay attention to the names. I&#8217;m sure you did so already. Anyway, let&#8217;s pretend that we had a scenario that we want to loop over the rows in this table and send an email out to these individuals.&nbsp; We can do this without using a database cursor. Here is some code to ponder on:</p>
<pre class="alt">&nbsp;</pre>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">declare</span> @email <span class="kwrd">varchar</span>(20)</pre>
<pre><span class="lnum">   2:  </span><span class="kwrd">declare</span> @id <span class="kwrd">int</span></pre>
<pre class="alt"><span class="lnum">   3:  </span><span class="kwrd">declare</span> @rowNum <span class="kwrd">int</span></pre>
<pre><span class="lnum">   4:  </span><span class="kwrd">declare</span> @maxrows <span class="kwrd">int</span></pre>
<pre class="alt"><span class="lnum">   5:  </span>&nbsp;</pre>
<pre><span class="lnum">   6:  </span><span class="kwrd">select</span> <span class="kwrd">top</span> 1 @id = ID, @email = EmailAddress <span class="kwrd">from</span> Contacts</pre>
<pre class="alt"><span class="lnum">   7:  </span><span class="kwrd">select</span> @maxRows = <span class="kwrd">count</span>(*) <span class="kwrd">from</span> Contacts</pre>
<pre><span class="lnum">   8:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   9:  </span><span class="kwrd">set</span> @rowNum = 0</pre>
<pre><span class="lnum">  10:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  11:  </span><span class="rem">-- this will until the last row is reached</span></pre>
<pre><span class="lnum">  12:  </span><span class="kwrd">WHILE</span> @rowNum &lt; @maxRows</pre>
<pre class="alt"><span class="lnum">  13:  </span> <span class="kwrd">BEGIN</span></pre>
<pre><span class="lnum">  14:  </span>    <span class="kwrd">set</span> @rowNum = @rowNum + 1</pre>
<pre class="alt"><span class="lnum">  15:  </span>    <span class="rem">-- this is where you can now do something like sending an email</span></pre>
<pre><span class="lnum">  16:  </span>    <span class="rem">-- for now, we will just print the email to the output screen</span></pre>
<pre class="alt"><span class="lnum">  17:  </span>    <span class="kwrd">print</span> (<span class="str">'Sending email to: '</span> + @email)</pre>
<pre><span class="lnum">  18:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  19:  </span>    <span class="rem">-- now we grab the next row making sure the ID of the next row</span></pre>
<pre><span class="lnum">  20:  </span>    <span class="rem">-- is greater than previous row</span></pre>
<pre class="alt"><span class="lnum">  21:  </span>    <span class="kwrd">select</span> <span class="kwrd">top</span> 1 @id = ID, @email = EmailAddress <span class="kwrd">from</span> Contacts <span class="kwrd">where</span> ID &gt; @id</pre>
<pre><span class="lnum">  22:  </span> END</pre>
</div>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="alt">&nbsp;</pre>
<p>I have tried to include additional comments in this code snippet so that it doesn&#8217;t require much explanation. However, you can see the original <a href="http://www.databasejournal.com/features/mssql/article.php/3111031" target="_blank">article</a> if you need additional details.. Until next time&#8230;</p>
<p>&nbsp;</p>
<p>Happy Coding!</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.tyronedavisjr.com%2findex.php%2f2007%2f12%2f12%2ftsql-looping-through-a-result-set-without-using-database-cursor%2f"><img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.tyronedavisjr.com%2findex.php%2f2007%2f12%2f12%2ftsql-looping-through-a-result-set-without-using-database-cursor%2f" border="0"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tyronedavisjr.com/2007/12/12/tsql-looping-through-a-result-set-without-using-database-cursor/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

