<?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>Scratty.com</title>
	<atom:link href="http://www.scratty.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scratty.com</link>
	<description>Development answers from real life experiences.</description>
	<lastBuildDate>Fri, 02 Mar 2012 23:02:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Windows 2008 Error:  A fatal error occurred when attempting to access the SSL server credential private key</title>
		<link>http://www.scratty.com/windows-2008-error-a-fatal-error-occurred-when-attempting-to-access-the-ssl-server-credential-private-key/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=windows-2008-error-a-fatal-error-occurred-when-attempting-to-access-the-ssl-server-credential-private-key</link>
		<comments>http://www.scratty.com/windows-2008-error-a-fatal-error-occurred-when-attempting-to-access-the-ssl-server-credential-private-key/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 21:38:03 +0000</pubDate>
		<dc:creator>Scratty</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Windows 2008]]></category>

		<guid isPermaLink="false">http://www.scratty.com/?p=193</guid>
		<description><![CDATA[While working on one of my Windows 2008 R2 servers, I noticed the following error in the event log over and over: A fatal error occurred when attempting to access the SSL server credential private key. The error code returned from the cryptographic module is 0x8009030d. The internal error state is 10001. I&#8217;m running SQL [...]]]></description>
			<content:encoded><![CDATA[<p>While working on one of my Windows 2008 R2 servers, I noticed the following error in the event log over and over:</p>
<blockquote><p><span style="color: #ff0000;">A fatal error occurred when attempting to access the SSL server credential private key. The error code returned from the cryptographic module is 0x8009030d. The internal error state is 10001.</span></p></blockquote>
<p>I&#8217;m running SQL Server 2008 R2 on this machine and didn&#8217;t think it had anything to do with the error, but I was wrong.    Turns out that the service account running SQL Server needs security access to:    <em>C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys</em>.</p>
<p>The Fix:</p>
<p>1.  Open windows explorer and navigate to:  <em>C:\ProgramData\Microsoft\Crypto\RSA\</em></p>
<p>2.  Right click on the folder <em>MachineKeys</em></p>
<p><em></em>3.  Set the security to add full control to whatever service is running SQL Server.</p>
<p>&nbsp;</p>
<p>I&#8217;ve checked a few other machines with this error and this fix resolved the problem.</p>
<p>Thanks to:   <a title="a-fatal-error-occurred-when-attempting-to-access-the-ssl-server-credential-private-key" href="http://www.zerohoursleep.com/2010/11/a-fatal-error-occurred-when-attempting-to-access-the-ssl-server-credential-private-key/" target="_blank">http://www.zerohoursleep.com/2010/11/a-fatal-error-occurred-when-attempting-to-access-the-ssl-server-credential-private-key/</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="al2fb_like_button"><div id="fb-root"></div><script type="text/javascript">
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=116192261841709";
  fjs.parentNode.insertBefore(js, fjs);
}(document, "script", "facebook-jssdk"));
</script>
<fb:like href="http://www.scratty.com/windows-2008-error-a-fatal-error-occurred-when-attempting-to-access-the-ssl-server-credential-private-key/" layout="button_count" show_faces="true" width="450" action="like" font="arial" colorscheme="light" ref="AL2FB"></fb:like></div>]]></content:encoded>
			<wfw:commentRss>http://www.scratty.com/windows-2008-error-a-fatal-error-occurred-when-attempting-to-access-the-ssl-server-credential-private-key/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress image upload issue on Windows 2008</title>
		<link>http://www.scratty.com/wordpress-image-upload-issue-windows-2008/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=wordpress-image-upload-issue-windows-2008</link>
		<comments>http://www.scratty.com/wordpress-image-upload-issue-windows-2008/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 19:18:25 +0000</pubDate>
		<dc:creator>Scratty</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Windows 2008]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[500 Internal Server Error]]></category>
		<category><![CDATA[Image]]></category>

		<guid isPermaLink="false">http://www.scratty.com/?p=170</guid>
		<description><![CDATA[For those of you running WordPress on Windows 2008, here&#8217;s a fix that I have been searching a while for. Have you ever run into the problem of uploading an image in WordPress and then try to access the image and you get the following error? Server Error 500 – Internal server error. There is [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you running WordPress on Windows 2008, here&#8217;s a fix that I have been searching a while for.</p>
<p>Have you ever run into the problem of uploading an image in WordPress and then try to access the image and you get the following error?</p>
<p class="size-full wp-image-171" title="500 Internal Server Error"><strong>Server Error </strong><br />
<strong>500 – Internal server error.</strong><br />
There is a problem with the resource you are looking for, and it cannot be displayed.</p>
<div id="attachment_171" class="wp-caption alignleft" style="width: 310px"><img class="size-full wp-image-171" title="500 Internal Server Error" src="http://www.scratty.com/wp-content/uploads/2011/08/server-error-500-300x66.png" alt="500 Internal Server Error" width="300" height="66" /><p class="wp-caption-text">500 Internal Server Error</p></div>
<h2></h2>
<h2></h2>
<h2></h2>
<h2></h2>
<h2><strong>Here&#8217;s the FIX:</strong></h2>
<p>Fixing permissions so that images/media/files uploaded via WordPress will properly render.  This is the fix for the infamous &#8220;500&#8243; error that is thrown when your server has wrong file permissions and the visual result is all the images you uploaded are &#8220;broken images&#8221; and won’t display.  Also called <strong>HTTP Error 500.50 – URL Rewrite Module Error</strong> when you have Detailed errors on.</p>
<ol>
<li>This problem is caused because PHP first uploads the document to a temporary directory (by default C:WindowsTemp), and then moves it from that directory to the actual /wp-content/uploads/ sub-directory.  What happens is that because IIS does not have any permissions to your &#8220;C:WindowsTemp&#8221; directory, when the file is uploaded there, then moved by PHP, the file inherits NO permissions.  So when IIS tries to serve out that file from your /wp-content/uploads/sub-directory it throws a 500 error and that is actually a permissions error.</li>
<li>The solution:  on the WindowsTemp folder, grant &#8220;modify&#8221; permissions to both IUSR and {servername}IIS_IUSRS user / group accounts.</li>
<li>Now when you upload files via PHP and PHP then moves them to the correct directory, the files will have the correct permissions and can be accessed.</li>
<li>Important note #1:  If you already have uploaded files and are getting the dreaded broken images issue, go to the /wp-content/uploads/directory and replace/update the permissions to add access for both user accounts noted above.  That will solve that issue.</li>
<li>Important note #2: as with any change off the defaults for Windows, you do this AT YOUR OWN RISK.  It’s up to you if you want to grant the WindowsTemp directory permissions for those two accounts.  If you don’t PHP won’t be able to upload without the 500 issue (unless you move the PHP temp folder elsewhere and grant those accounts access to the other folder).  Do these changes at your own risk.  Please know what you are doing.</li>
<li>You can also edit the php.ini file and change the location of the &#8220;upload_tmp_dir&#8221; variable and point it to another location.  It’s up to you how you want to do this, as long as you have the right permissions mentioned above, your WP images and future uploads will work fine.</li>
</ol>
<p>Thanks to:  http://www.amixa.com/blog/2011/04/29/iis-wordpress-images-500-error/</p>
<p>&nbsp;</p>
<div class="al2fb_like_button"><div id="fb-root"></div><script type="text/javascript">
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=116192261841709";
  fjs.parentNode.insertBefore(js, fjs);
}(document, "script", "facebook-jssdk"));
</script>
<fb:like href="http://www.scratty.com/wordpress-image-upload-issue-windows-2008/" layout="button_count" show_faces="true" width="450" action="like" font="arial" colorscheme="light" ref="AL2FB"></fb:like></div>]]></content:encoded>
			<wfw:commentRss>http://www.scratty.com/wordpress-image-upload-issue-windows-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress on Windows with Wincache</title>
		<link>http://www.scratty.com/wordpress-windows-wincache/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=wordpress-windows-wincache</link>
		<comments>http://www.scratty.com/wordpress-windows-wincache/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 03:25:51 +0000</pubDate>
		<dc:creator>Scratty</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Windows 2008]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.scratty.com/?p=163</guid>
		<description><![CDATA[For those of you running WordPress on Windows with the 1.1 version of WinCache for php.   Here&#8217;s a great article regarding an updated wincache dll.    The 1.1 version has been driving me crazy when updating my plugins.  I would select Update Automatically on a plugin, but it would fail on deleting the old directory.  Then [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you running WordPress on Windows with the 1.1 version of WinCache for php.   Here&#8217;s a great article regarding an updated wincache dll.    The 1.1 version has been driving me crazy when updating my plugins.  I would select Update Automatically on a plugin, but it would fail on deleting the old directory.  Then I would have to recycle the app pool and manually download the plugin to install it.</p>
<p>Here&#8217;s the link:  <a title="wincache-and-wordpress-plugin-upgrade-problem" href="http://ruslany.net/2011/04/wincache-and-wordpress-plugin-upgrade-problem/" target="_blank">http://ruslany.net/2011/04/wincache-and-wordpress-plugin-upgrade-problem/</a></p>
<div class="al2fb_like_button"><div id="fb-root"></div><script type="text/javascript">
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=116192261841709";
  fjs.parentNode.insertBefore(js, fjs);
}(document, "script", "facebook-jssdk"));
</script>
<fb:like href="http://www.scratty.com/wordpress-windows-wincache/" layout="button_count" show_faces="true" width="450" action="like" font="arial" colorscheme="light" ref="AL2FB"></fb:like></div>]]></content:encoded>
			<wfw:commentRss>http://www.scratty.com/wordpress-windows-wincache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ATI Catalyst Control Center (CCC) won&#8217;t start</title>
		<link>http://www.scratty.com/ati-catalyst-control-center-ccc-start/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=ati-catalyst-control-center-ccc-start</link>
		<comments>http://www.scratty.com/ati-catalyst-control-center-ccc-start/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 14:35:35 +0000</pubDate>
		<dc:creator>Scratty</dc:creator>
				<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[ATI]]></category>
		<category><![CDATA[Catalyst Control Center]]></category>

		<guid isPermaLink="false">http://www.scratty.com/?p=160</guid>
		<description><![CDATA[If you have an ATI Video card and use the Catalyst Control Center program and you have the problem that CCC won&#8217;t start, here&#8217;s the solution. My solution was to clear out the  Windows GAC (Global Assembly Cache).  In Windows 7, you can browse  directly to &#8220;C:Windowsassembly&#8221;.  Sort by &#8220;Public Key Token&#8221;, then  delete everything [...]]]></description>
			<content:encoded><![CDATA[<p>If you have an ATI Video card and use the Catalyst Control Center program and you have the problem that CCC won&#8217;t start, here&#8217;s the solution.</p>
<p>My solution was to clear out the  Windows GAC (Global Assembly Cache).  In Windows 7, you can browse  directly to &#8220;C:Windowsassembly&#8221;.  Sort by &#8220;Public Key Token&#8221;, then  delete everything with the Token &#8220;90ba9c70f846762e&#8221; (ATI&#8217;s key).  You&#8217;ll  obviously need Administrator privileges.  No reboot needed either.   Reinstalled CCC and it ran perfectly!</p>
<p>&nbsp;</p>
<div class="al2fb_like_button"><div id="fb-root"></div><script type="text/javascript">
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=116192261841709";
  fjs.parentNode.insertBefore(js, fjs);
}(document, "script", "facebook-jssdk"));
</script>
<fb:like href="http://www.scratty.com/ati-catalyst-control-center-ccc-start/" layout="button_count" show_faces="true" width="450" action="like" font="arial" colorscheme="light" ref="AL2FB"></fb:like></div>]]></content:encoded>
			<wfw:commentRss>http://www.scratty.com/ati-catalyst-control-center-ccc-start/feed/</wfw:commentRss>
		<slash:comments>84</slash:comments>
		</item>
		<item>
		<title>Slow Windows 7 Remote Desktop Problem Solved</title>
		<link>http://www.scratty.com/slow-windows-7-remote-desktop-problem-solved/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=slow-windows-7-remote-desktop-problem-solved</link>
		<comments>http://www.scratty.com/slow-windows-7-remote-desktop-problem-solved/#comments</comments>
		<pubDate>Sat, 20 Nov 2010 03:59:30 +0000</pubDate>
		<dc:creator>Scratty</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Remote Desktop]]></category>

		<guid isPermaLink="false">http://www.scratty.com/?p=155</guid>
		<description><![CDATA[I do a lot of work from home by connecting to my work network via VPN and then using remote desktop to my work computer.   Lately though the Remote Desktop has been painfully slow. After some searching I found this link. Here&#8217;s an excerpt: In any event, I discovered that Vista&#8217;s Receive Window Auto-Tuning [...]]]></description>
			<content:encoded><![CDATA[<p>I do a lot of work from home by connecting to my work network via VPN and then using remote desktop to my work computer.   Lately though the Remote Desktop has been painfully slow.</p>
<p>After some searching I found this <a href="http://blog.tmcnet.com/blog/tom-keating/microsoft/remote-desktop-slow-problem-solved.asp" target="_blank">link</a>.</p>
<p>Here&#8217;s an excerpt:</p>
<p>In any event, I discovered that Vista&#8217;s Receive Window Auto-Tuning could have issues on some networks.  I use Remote Desktop all the time to manage 30+ servers.  After adjusting Receive Window Auto-Tuning, the &#8220;slowness&#8221; problem with mouse-clicks, keystrokes, and screen redraws went away. Problem solved! Woo-hoo!</p>
<p>Here is what you need to do if you have the same issue:</p>
<p>- Run a command prompt (cmd.exe) as an Administrator</p>
<p>[crayon-4fb93f4400c44/]</p>
<p>In some cases you may need to use this command in addition to the above, but I didn&#8217;t have to:<br />
[crayon-4fb93f4400cbe/]</p>
<p>If you want to to re-set it back to normal:<br />
[crayon-4fb93f4400d35/]</p>
<p>The reason is that this command will still &#8220;auto tune&#8221; your TCP connections, but not as drastically as &#8216;normal&#8217; mode. It will allow the receive window to grow beyond the default value, but again it will do so <em>very conservatively</em>. In this mode, Windows 7 will by default use RWIN (receive window) of 16,384 bytes with a scale factor of 2. I was browsing computers in my Network Neighborhood and trying to get to \computernamec$ which was taking forever to load. I changed it to highlyrestricted and it was much faster. &#8216;highlyrestricted&#8217; mode is my recommendation for the fastest network performance whether you are using Remote Desktop, Internet browsing, or doing SMB file copies across your network.</p>
<p>This did the trick for me and worked wonderfully!</p>
<div class="al2fb_like_button"><div id="fb-root"></div><script type="text/javascript">
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=116192261841709";
  fjs.parentNode.insertBefore(js, fjs);
}(document, "script", "facebook-jssdk"));
</script>
<fb:like href="http://www.scratty.com/slow-windows-7-remote-desktop-problem-solved/" layout="button_count" show_faces="true" width="450" action="like" font="arial" colorscheme="light" ref="AL2FB"></fb:like></div>]]></content:encoded>
			<wfw:commentRss>http://www.scratty.com/slow-windows-7-remote-desktop-problem-solved/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>30 SQL Myths from Paul Randall</title>
		<link>http://www.scratty.com/30-sql-myths-paul-randall/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=30-sql-myths-paul-randall</link>
		<comments>http://www.scratty.com/30-sql-myths-paul-randall/#comments</comments>
		<pubDate>Wed, 03 Nov 2010 00:05:40 +0000</pubDate>
		<dc:creator>Scratty</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Sql server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://www.scratty.com/?p=110</guid>
		<description><![CDATA[Paul Randall has published 30 SQL myths with full explanations of why they aren&#8217;t true. Download the pdf from http://www.sqlskills.com/blogs/paul/CommonSQLServerMyths.pdf. The full blog can be found here: http://www.sqlskills.com/BLOGS/PAUL/post/Myths-and-misconceptions-60-page-PDF-of-blog-posts.aspx]]></description>
			<content:encoded><![CDATA[<p>Paul Randall has published 30 SQL myths with full explanations of why they aren&#8217;t true. Download the pdf from <a href="http://www.sqlskills.com/blogs/paul/CommonSQLServerMyths.pdf "> http://www.sqlskills.com/blogs/paul/CommonSQLServerMyths.pdf</a>.</p>
<p>The full blog can be found here: <a href="http://www.sqlskills.com/BLOGS/PAUL/post/Myths-and-misconceptions-60-page-PDF-of-blog-posts.aspx">http://www.sqlskills.com/BLOGS/PAUL/post/Myths-and-misconceptions-60-page-PDF-of-blog-posts.aspx</a></p>
<div class="al2fb_like_button"><div id="fb-root"></div><script type="text/javascript">
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=116192261841709";
  fjs.parentNode.insertBefore(js, fjs);
}(document, "script", "facebook-jssdk"));
</script>
<fb:like href="http://www.scratty.com/30-sql-myths-paul-randall/" layout="button_count" show_faces="true" width="450" action="like" font="arial" colorscheme="light" ref="AL2FB"></fb:like></div>]]></content:encoded>
			<wfw:commentRss>http://www.scratty.com/30-sql-myths-paul-randall/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sequence Tables</title>
		<link>http://www.scratty.com/sequence-tables/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sequence-tables</link>
		<comments>http://www.scratty.com/sequence-tables/#comments</comments>
		<pubDate>Mon, 18 Oct 2010 21:30:18 +0000</pubDate>
		<dc:creator>Scratty</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Sql server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://www.scratty.com/todays-links-15/</guid>
		<description><![CDATA[http://sqlblog.com/blogs/paul_white/archive/2010/10/19/sequence-tables.aspx Sequence Tables It is frequently useful to generate sequences of values within SQL Server, perhaps for use as surrogate keys.  Using the IDENTITY property on a column is the easiest way to automatically generate such sequences: [crayon-4fb93f4402adc/] Sometimes though, the database designer finds that she needs a more flexible scheme than is provided by [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sqlblog.com/blogs/paul_white/archive/2010/10/19/sequence-tables.aspx">http://sqlblog.com/blogs/paul_white/archive/2010/10/19/sequence-tables.aspx</a></p>
<h2>Sequence Tables</h2>
<p>It is frequently useful to generate sequences of values within SQL Server, perhaps for use as <a href="http://en.wikipedia.org/wiki/Surrogate_key">surrogate keys</a>.  Using the IDENTITY property on a column is the easiest way to automatically generate such sequences:</p>
<p>[crayon-4fb93f4402adc/]</p>
<p>Sometimes though, the database designer finds that she needs a more flexible scheme than is provided by the IDENTITY property.  One alternative is to use a Sequence Table.</p>
<h4>What is a Sequence Table?</h4>
<p>A Sequence Table is a regular table that stores the next available value for one or more sequences – somewhat like a manual identity value in many respects.  The definition of a Sequence Table might look like this:</p>
<p>[crayon-4fb93f4402b55/]</p>
<p>The table contains one row for each sequence range under management. One Sequence Table can maintain any number of independent sequences. Typically, though, the table only contains a few tens of rows.</p>
<p>The first column represents the sequence name, and the second represents the next unallocated value in the range. A sequence is initialized with a statement like:</p>
<p>[crayon-4fb93f4402bcc/]</p>
<p>The values obtained from the sequence table can be used for any purpose, but the most common is to use them as key values for rows in other tables.  Values are allocated from the sequence through a stored procedure, which encapsulates the logic required to extend the sequence, and provides an easy way to manage permissions. Generally, users have no permissions on the Sequence Table itself, to protect the integrity of the sequence.</p>
<h4>The Allocation Stored Procedure</h4>
<p>The most common use of a Sequence Table is in pre-allocating a range of keys, to make a batch process more efficient. A process that needs to add a large number of new rows to a table can be made to perform better by batching the requests, and submitting each batch in a single transaction. To make this work, we need to know the key values in advance.</p>
<p>Say we want to reserve a contiguous range of 250 keys from the Test Sequence defined above. We might call a stored procedure like this:</p>
<p>[crayon-4fb93f4402c43/]<br />
The procedure returns the first sequence value reserved, so if we receive the value 1 back, we can safely use key values from 1 to 250 inclusive, without calling the procedure again. No other process can obtain key values from our reserved range.</p>
<p>After this procedure call, the Sequence Table looks like this:</p>
<p><a href="http://sqlblog.com/blogs/paul_white/image009_3B883B1A.png"><img style="padding-left: 0px; padding-right: 0px; padding-top: 0px; border-width: 0px;" src="http://sqlblog.com/blogs/paul_white/image009_thumb_6111B87B.png" alt="image009" width="161" height="40" border="0" /></a></p>
<p>An implementation of the procedure itself might look like this:</p>
<p>[crayon-4fb93f4402cb9/]</p>
<p>The important part of the procedure is the UPDATE statement. The output parameter @FirstAllocated is set to the pre-update value of the <em>next_value</em> column. The <em>next_value</em> column then has @RangeSize added to its value, thereby reserving a contiguous range of values for the procedure caller.</p>
<p>As a reminder, recall that the caller checks the value returned in @FirstAllocated, knowing that the next @RangeSize keys after that have been reserved exclusively for her use.</p>
<h4>Where to Use a Sequence Table</h4>
<p>Generally, you will want to use an identity column, if it fits the application requirements. Only consider a Sequence Table where some aspect of the identity property makes it unsuitable.</p>
<p>A Sequence Table might be the right choice where you need:</p>
<ol>
<li>To generate a custom sequence of a type not directly supported by identity columns; or</li>
<li>To maintain a sequence of key values over more than one table; or</li>
<li>A guarantee that a multi-row insert will receive a <em>contiguous</em> block of sequence values; or</li>
<li>To pre-allocate a range of sequence values for performance reasons</li>
</ol>
<p>Cases 1 and 2 are perhaps obvious. Between them, they cover custom sequences that cannot conveniently be implemented with an identity column.</p>
<p>Case 3 is quite interesting. If concurrent queries insert multiple rows to the same table with an identity column, SQL Server does not guarantee that the identity values allocated will be contiguous for each caller. In fact, the asynchronous nature of identity value allocations means that the values assigned will typically <em>not</em> be contiguous. This can be important to some applications, and there is more about this later on, in the section on concurrency.</p>
<p>Case 4 covers a class of question often seen on the forums. The identity value assigned to a row is only available <em>after</em> the insert has completed. Frequently, it would be more efficient to know the value before the insert is performed.</p>
<p>Consider a process which must add a number of related objects to the database. One option is to insert one object at a time, retrieve the identity value, and then use that value in subsequent inserts. If the application were able to pre-allocate the values, the insert operations could all be performed in a single batch, and a single transaction. This could prove to have very significant performance benefits.</p>
<p>This blog entry is concerned primarily with case four (pre-allocation of ranges). The Sequence Table method can be used to solve problems in all four cases.</p>
<h4>Concurrency and Deadlocking</h4>
<p>A common objection to using the Sequence Table design is that it might encounter concurrency problems. Although the process of allocating a new range is atomic and pretty fast, like any data modification it acquires an exclusive row-level lock on the Sequence Table, which will be held until the containing transaction completes.</p>
<p>Imagine a process (A) starting a transaction and requesting a new range of key values from the database via the Sequence Table allocation stored procedure.  The allocation completes very quickly (typically in a <em>fraction</em> of a millisecond), but process (A) goes on to perform another operation before committing its transaction. As luck would have it, this second operation encounters a delay of some sort. Process (A) continues to hold an exclusive lock on a Sequence Table row until it completes its transaction.</p>
<p>Meanwhile, a second process (B) also requests a range of key values, for the same sequence name as process (A). This request needs to update the same row that process (A) has locked exclusively. Process B is therefore blocked until process A completes its transaction.  This limitation means that solutions based on a Sequence Table historically often suffered from an unacceptable level of deadlocks.</p>
<p>In the example above, if process B already held a lock which process A must wait to acquire, we have a deadlock – neither process can make progress since it is waiting for a lock which is incompatible with one already held by the opposing process.</p>
<h4>How SQL Server Does It</h4>
<p>At this point, you might be wondering how SQL Server avoids this sort of problem when working with identity columns. How does SQL Server avoid these kinds of concurrency and deadlocking problems when users add multiple rows concurrently to the same table?</p>
<p>The answer is that SQL Server allocates identity values using an internal function that does not participate in user transactions. (This is the reason that the current identity value for a table is not reset when a user transaction rolls back – the change in identity value was not part of the aborted transaction).</p>
<p>Another way to look at this is to say that the allocation of identity values is performed using an ‘autonomous’ transaction – one that exists independently of any user transaction.</p>
<p>SQL Server <em>does</em> briefly lock an internal resource while updating the current identity value, but the lock is transient and held just long enough to prevent data corruption. The autonomous transaction is therefore very fast, and does not cause concurrency problems.</p>
<p>This autonomous behaviour is also the reason that two users concurrently executing a multi-row insert to the same table typically do not receive a contiguous range of identity values. Because identity allocations do not block, a single INSERT statement can be given multiple small ranges of identity values, which do not form a single contiguous sequence.</p>
<p>It appears that the way to avoid concurrency problems is to use an autonomous transaction. Unfortunately, SQL Server does not currently offer this facility – but it is possible to simulate it. The next section examines this in detail, since it is <em>essential</em> for a robust implementation of a Sequence Table.</p>
<h4>Simulating Autonomous Transactions</h4>
<p>The goal here is to find a way to allocate a range from the Sequence Table without holding long-term locks. This implies that we need to acquire our range in the context of a separate transaction. The most obvious way to achieve this is to use a separate database connection.</p>
<p>In SQL Server 2000, we could write an extended stored procedure to make a new connection to the database and update the Sequence Table on that connection. The exclusive lock on the Sequence Table row is held for a very short time on the second connection, and is unaffected by the open transaction on the first connection.</p>
<p>The downside to this is that extended stored procedures are difficult to write, and may affect the stability of the SQL Server instance. Extended stored procedures were deprecated with the release of SQL Server 2005.</p>
<p>From SQL Server 2005, a CLR stored procedure can be used as a direct replacement for an extended stored procedure. The CLR hosted environment makes this option very safe to use within SQL Server, and accessible to anyone with basic .NET coding skills.</p>
<p>In SQL Server 2008, there is a new way to achieve autonomous transactions in pure T-SQL. The method is fully documented and supported, and slightly out-performs the equivalent CLR implementation.</p>
<h5>SQL Server 2005</h5>
<p>A full implementation of a CLR stored procedure is included in the script at the end of this blog entry. Here, we will just take a quick look at the main features of this solution.</p>
<p>The CLR stored procedure needs to make a new connection to the SQL Server instance, call the T-SQL allocation stored procedure on that connection, and then return the results.</p>
<p>The first problem is that the CLR procedure needs to know the name of the SQL Server instance it was called from, in order to create a new connection to it.</p>
<p>We can find the information we need using the <em>context connection</em> – a built-in feature, which allows the CLR procedure to talk back to the database via the connection it was called on.</p>
<p>It is important to note that we cannot use the context connection to call the T-SQL allocation stored procedure, since the context connection is associated with the still-open original transaction. If we did that, we would not solve any of the concurrency or deadlocking problems.</p>
<p>The code to get the name of the SQL Server instance we need to connect to looks like this:</p>
<p>[crayon-4fb93f4402d43/]<br />
Now that we have the name of the SQL Server instance, we can make a new connection to it:</p>
<p>[crayon-4fb93f4402dba/]</p>
<p>It is important to specify (<em>enlist = false</em>) in the connection string, to ensure that the new connection has no link to the original transaction. The rest of the code in the CLR stored procedure is just concerned with calling the original allocation routine and returning the results.</p>
<p>At first look, it seems as if this method might be inefficient. Each allocation from the Sequence Table requires a call back to the server to find its name, and the creation of a new connection, in addition to the cost of calling the Sequence Table’s allocation procedure.</p>
<p>In practice though, the process is quite fast. The context connection already exists, and the new connection created is usually allocated from a pool. The whole process typically takes around one millisecond to complete.</p>
<p>This method works well, but does require that CLR integration be enabled.</p>
<h5>SQL Server 2008</h5>
<p>In this release, SQL Server introduces a new option for linked servers, which allows a purely T-SQL approach to autonomous transactions.  To use this new feature, we need to create a <em>loopback </em>linked-server – a linked server that points back to the same server it is created on.</p>
<p>We can then use the <a href="http://msdn.microsoft.com/en-us/library/ms178532.aspx">sp_serveroption</a> system stored procedure, to set the new option “<em>remote proc transaction promotion”</em> to FALSE (or OFF). This setting prevents a local transaction from being promoted to a distributed transaction when making a call to the loopback linked-server.</p>
<p>The effect is that of an autonomous transaction – exactly what we need. Further details on this method can be found <a href="http://blogs.msdn.com/sqlprogrammability/archive/2008/08/22/how-to-create-an-autonomous-transaction-in-sql-server-2008.aspx">here</a> in a blog entry by the SQL Programmability &amp; API Development Team.</p>
<p>Again, a full implementation of this method can be found at the end of this entry. We will now look at the essential features of this approach.  First, we need to create a loopback linked-server, and set the options. The following code snippet takes care of that:</p>
<p>[crayon-4fb93f4402e34/]</p>
<p>Using this method, we can take full advantage of the flexibility the Sequence Table offers, while avoiding concurrency issues and deadlocks. Calling the allocation procedure in this way is typically a little faster than using the CLR implementation.</p>
<h4>Testing and Results</h4>
<p>A full test rig is included at the end of this entry. This script contains all the code necessary to test the behaviour of each of the three methods presented in this article: the original stored procedure, the CLR stored procedure, and the method using a loopback linked server.</p>
<p>You will need a copy of SQL Server 2008 for best results. The test rig is written so that the SQL Server 2008 method will still work when run on SQL Server 2005, but you will not get the benefits of an autonomous transaction.</p>
<p>When run on SQL Server 2005, the loopback linked server method will hold long-term locks, and will be very much slower. The CLR stored procedure works well for both SQL Server 2005 and 2008 users.</p>
<p>The tests create a single test sequence and allocate a number of key values using each method, displaying the locks held by each open transaction.</p>
<p>The transaction wrapping each test is rolled back at the end, to show the effect on the Sequence Table. It is important to note that the non-blocking (CLR and loopback server) methods behave like an identity column in that the next value in the sequence is not affected by a roll back.</p>
<p>The output is reproduced and discussed below.</p>
<h5>Original Allocation Stored Procedure</h5>
<p>This implementation takes no special steps to avoid holding long-term locks. The locks held after allocating values from the test sequence are:</p>
<p><a href="http://sqlblog.com/blogs/paul_white/image022_437BC97D.jpg"><img style="padding-left: 0px; padding-right: 0px; padding-top: 0px; border-width: 0px;" src="http://sqlblog.com/blogs/paul_white/image022_thumb_610DA47C.jpg" alt="image022" width="480" height="80" border="0" /></a></p>
<p>An exclusive row-level lock is held on the Sequence Table, together with the normal higher-level intent-exclusive locks. This row-level lock will prevent other concurrent sessions allocating from the same sequence until the transaction commits or is rolled back.</p>
<p>This method is the fastest, but will exhibit poor concurrency, and may result in a high level of deadlocks, depending on the design of the calling application. This method is therefore not recommended.</p>
<h5>2005 CLR Stored Procedure</h5>
<p>This implementation uses a separate external connection to the database to avoid holding long-term locks. The locks held by this procedure are:</p>
<p><a href="http://sqlblog.com/blogs/paul_white/image024_37B65C7E.jpg"><img style="padding-left: 0px; padding-right: 0px; padding-top: 0px; border-width: 0px;" src="http://sqlblog.com/blogs/paul_white/image024_thumb_3122DCEE.jpg" alt="image024" width="476" height="58" border="0" /></a></p>
<p>The only locks held are schema-stability locks associated with the CLR assembly. These locks prevent the assembly from being dropped or modified while it is in use, and do not affect Sequence Table concurrency or deadlocking. The important fact is that there are no locks held on the Sequence Table itself – therefore no concurrency or deadlocking problems can occur.</p>
<p>This method is well suited to pre-allocating ranges of keys for an application.</p>
<h5>2008 Loopback Linked Server</h5>
<p>This method holds no locks at all, after the allocation is made, confirming the autonomous transaction behaviour we were seeking. This method is slightly faster than the CLR solution.</p>
<h4>Performance</h4>
<p>On a test run of ten thousand (arbitrarily large) range allocations, the CLR method averages around 2ms per call, and the loopback server method averages around 1ms per call.</p>
<p>It is important to note though, that both methods are slower than using a column with the IDENTITY property. If you can use a simple identity column, you probably should.</p>
<h4>Other Considerations and Alternatives</h4>
<h5>Page Splits</h5>
<p>Pre-allocating key values can be an excellent performance optimization. If, however, you were relying on an identity column to control index page splits, you should be careful to consider the implications of switching to a Sequence Table method.</p>
<p>In general, the potential changes in page-splitting behaviour are not a great concern. As always though, it is important to test any proposed solution thoroughly, and the Sequence Table method is no exception to that rule.</p>
<h5>Alternatives</h5>
<p>Other potential solutions to the wider problem include using sequential GUIDs, the OUTPUT clause, or maintaining key sequences outside SQL Server.</p>
<p>GUIDs are relatively wide for use as keys, and some people find them less convenient to work with, compared to integers. This method also requires a good way of producing them in sequential order. Of course, there are scenarios where it makes sense to use GUIDs, sequential or otherwise.</p>
<p>Using the OUTPUT clause to return allocated identity values is can be problematic in batch scenarios.  Using OUTPUT does not guarantee that the range of values assigned will be contiguous. SQL Server also does not typically return rows from the OUTPUT clause in any defined order. This leads on to problems mapping the returned identity values to the rows submitted for insertion. Nevertheless, there are occasions where this method can be made to work well.</p>
<p>You might also consider maintaining key values outside of SQL Server, possibly in a mid-tier component. One disadvantage is that the key values are then not available to any process running inside SQL Server. It might also be argued that key allocation, as a data entity, logically belongs in the database layer.</p>
<h4>Conclusion</h4>
<p>Properly implemented, a Sequence Table is a high-performing and robust solution for appropriate applications running on SQL Server 2005 or later. Anyone with a need to pre-allocate key values or to maintain a custom sequence should give a Sequence Table serious consideration.</p>
<div class="al2fb_like_button"><div id="fb-root"></div><script type="text/javascript">
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=116192261841709";
  fjs.parentNode.insertBefore(js, fjs);
}(document, "script", "facebook-jssdk"));
</script>
<fb:like href="http://www.scratty.com/sequence-tables/" layout="button_count" show_faces="true" width="450" action="like" font="arial" colorscheme="light" ref="AL2FB"></fb:like></div>]]></content:encoded>
			<wfw:commentRss>http://www.scratty.com/sequence-tables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Index Defrag Script</title>
		<link>http://www.scratty.com/sql-index-defrag-script/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sql-index-defrag-script</link>
		<comments>http://www.scratty.com/sql-index-defrag-script/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 03:11:16 +0000</pubDate>
		<dc:creator>Scratty</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Sql server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://www.scratty.com/?p=58</guid>
		<description><![CDATA[Found this great defrag script for SQL Server from SQLFool.com.      Check it out here:  http://sqlfool.com/2010/04/index-defrag-script-v4-0/. Here&#8217;s the 4.0 version of the code: [crayon-4fb93f440b0fb/]]]></description>
			<content:encoded><![CDATA[<p>Found this great defrag script for SQL Server from SQLFool.com.      Check it out here:  <a title="SQL Server index defrag script" href="http://sqlfool.com/2010/04/index-defrag-script-v4-0/" target="_blank">http://sqlfool.com/2010/04/index-defrag-script-v4-0/</a>.</p>
<p>Here&#8217;s the 4.0 version of the code:</p>
<p>[crayon-4fb93f440b0fb/] </p>
<div class="al2fb_like_button"><div id="fb-root"></div><script type="text/javascript">
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=116192261841709";
  fjs.parentNode.insertBefore(js, fjs);
}(document, "script", "facebook-jssdk"));
</script>
<fb:like href="http://www.scratty.com/sql-index-defrag-script/" layout="button_count" show_faces="true" width="450" action="like" font="arial" colorscheme="light" ref="AL2FB"></fb:like></div>]]></content:encoded>
			<wfw:commentRss>http://www.scratty.com/sql-index-defrag-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New web sites I&#8217;m working on.</title>
		<link>http://www.scratty.com/new-web-sites-im-working-on/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=new-web-sites-im-working-on</link>
		<comments>http://www.scratty.com/new-web-sites-im-working-on/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 15:01:11 +0000</pubDate>
		<dc:creator>Scratty</dc:creator>
				<category><![CDATA[Car Buying]]></category>

		<guid isPermaLink="false">http://www.scratty.com/?p=53</guid>
		<description><![CDATA[http://www.dealer411.net Find dealers in your area. http://www.ecardeal.com http://blog.ecardeal.com http://www.ezcarprices.com http://www.automatic-financing.com Great sites if your looking to buy a car.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.dealer411.net">http://www.dealer411.net</a> Find dealers in your area.</p>
<p><a href="http://www.ecardeal.com" target="_blank">http://www.ecardeal.com</a></p>
<p><a href="http://blog.ecardeal.com">http://blog.ecardeal.com</a></p>
<p><a href="http://www.ezcarprices.com">http://www.ezcarprices.com</a></p>
<p><a href="http://www.automatic-financing.com">http://www.automatic-financing.com</a></p>
<p>Great sites if your looking to buy a car.</p>
<div class="al2fb_like_button"><div id="fb-root"></div><script type="text/javascript">
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=116192261841709";
  fjs.parentNode.insertBefore(js, fjs);
}(document, "script", "facebook-jssdk"));
</script>
<fb:like href="http://www.scratty.com/new-web-sites-im-working-on/" layout="button_count" show_faces="true" width="450" action="like" font="arial" colorscheme="light" ref="AL2FB"></fb:like></div>]]></content:encoded>
			<wfw:commentRss>http://www.scratty.com/new-web-sites-im-working-on/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SQL Server 2008 Proximity Search With The Geography Data Type</title>
		<link>http://www.scratty.com/sql-server-2008-proximity-search-with-the-geography-data-type/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sql-server-2008-proximity-search-with-the-geography-data-type</link>
		<comments>http://www.scratty.com/sql-server-2008-proximity-search-with-the-geography-data-type/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 02:34:11 +0000</pubDate>
		<dc:creator>Scratty</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://www.scratty.com/?p=50</guid>
		<description><![CDATA[Here's a great article on how to use the new SQL Server 2008 geography data type for proximity searches aka zip code radius search.   I've used many of diffent ways to run these searches, including the ones provided by the zip code data providers, but this solution has got to be the fastest I've run across.   If you use SQL Server 2008 and need a quick and simple proximity search based on zip code, then you have to read this article:  ]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a great article on how to use the new SQL Server 2008 geography data type for proximity searches aka zip code radius search.   I&#8217;ve used many of diffent ways to run these searches, including the ones provided by the zip code data providers, but this solution has got to be the fastest I&#8217;ve run across.   If you use SQL Server 2008 and need a quick and simple proximity search based on zip code, then you have to read this article:  <a title="SQL Server 2008 Zip Code Radius Search" href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sql-server-2008-proximity-search-with-th" target="_blank">http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sql-server-2008-proximity-search-with-th</a></p>
<p>If you have SQL 2005, then read this  article:  <a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sql-server-zipcode-latitude-longitude-pr">http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sql-server-zipcode-latitude-longitude-pr</a></p>
<p>I love finding solutions that make life easier.  <img src='http://www.scratty.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<div class="al2fb_like_button"><div id="fb-root"></div><script type="text/javascript">
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=116192261841709";
  fjs.parentNode.insertBefore(js, fjs);
}(document, "script", "facebook-jssdk"));
</script>
<fb:like href="http://www.scratty.com/sql-server-2008-proximity-search-with-the-geography-data-type/" layout="button_count" show_faces="true" width="450" action="like" font="arial" colorscheme="light" ref="AL2FB"></fb:like></div>]]></content:encoded>
			<wfw:commentRss>http://www.scratty.com/sql-server-2008-proximity-search-with-the-geography-data-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

