<?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>Alkampfer&#039;s Place&#187; Sql Server</title>
	<atom:link href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codewrecks.com/blog</link>
	<description>Wrecks of code floating in the sea of Internet By Ricci Gian Maria</description>
	<lastBuildDate>Wed, 08 Feb 2012 17:49:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Check progress of DBCC CHECKDB</title>
		<link>http://www.codewrecks.com/blog/index.php/2012/02/07/check-progress-of-dbcc-checkdb/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/02/07/check-progress-of-dbcc-checkdb/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 10:12:52 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/02/07/check-progress-of-dbcc-checkdb/</guid>
		<description><![CDATA[If you issue a DBCC CHECKDB on a big database to verify for consistency errors, it will take a long time to complete, but the Management Studio windows usually does not give you any hint about how long does it take, or a percentage progress. Luckily enough sql server has a Dynamic Management View that [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F02%2F07%2Fcheck-progress-of-dbcc-checkdb%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F02%2F07%2Fcheck-progress-of-dbcc-checkdb%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Sql+Server&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>If you issue a <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms176064.aspx">DBCC CHECKDB</a> on a big database to verify for consistency errors, it will take a long time to complete, but the Management Studio windows usually does not give you any hint about how long does it take, or a percentage progress. Luckily enough <a title="sql server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">sql server</a> has a Dynamic Management View that can solve your problem.</p>
<p>This is the SQL code to visualize progress of the operation</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">SELECT</span>  session_id ,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>         request_id ,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>         percent_complete ,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>         estimated_completion_time ,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>         DATEADD(ms,estimated_completion_time,GETDATE()) <span style="color: rgb(0, 0, 255);">AS</span> EstimatedEndTime, </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>         start_time ,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>         status ,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>         command </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span> <span style="color: rgb(0, 0, 255);">FROM</span> sys.dm_exec_requests</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span> <span style="color: rgb(0, 0, 255);">WHERE</span> database_id = 16</pre>
<p><!--CRLF--></div>
</div>
<p>In my example I filtered the results only for the database and used the Id of the database that you can obtain with the <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms186274.aspx">DB_ID</a> function.</p>
<p>An example of what you got with this query is represented in the following picture.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image6.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image_thumb6.png" width="881" height="127" /></a></p>
<p>As you can see you can easily visualize percentage of completion, estimated end time and the command that is running.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/02/07/check-progress-of-dbcc-checkdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When it is time to tweak SQL Server queries</title>
		<link>http://www.codewrecks.com/blog/index.php/2012/01/31/when-it-is-time-to-tweak-sql-server-queries/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/01/31/when-it-is-time-to-tweak-sql-server-queries/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 18:35:45 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/01/31/when-it-is-time-to-tweak-sql-server-queries/</guid>
		<description><![CDATA[I’ve a stored procedure with a query that runs on a quite big database, it was slow (more than one minute to run) and was optimized using a temp table. The result is that execution time dropped to ~2 secs, and since this was acceptable the optimization stopped. After a couple of months, the query [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F01%2F31%2Fwhen-it-is-time-to-tweak-sql-server-queries%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F01%2F31%2Fwhen-it-is-time-to-tweak-sql-server-queries%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Sql+Server&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I’ve a stored procedure with a query that runs on a quite big database, it was slow (more than one minute to run) and was optimized using a temp table. The result is that execution time dropped to ~2 secs, and since this was acceptable the optimization stopped.</p>
<p>After a couple of months, the query become really slow again, it got executed in ~30 secs and I started to investigate why.</p>
<p>A quick tour with SSMS and STATISTICS I/O did not reveal some <a target="_blank" title="news" href="http://www.codewrecks.com/blog/index.php/category/news/">news</a>, but the execution plan have a strange sort operation that takes 90% of the time of the overall query and this is really strange, but I did not find any reason why it should slow the query so much. To have a better picture of what is happening I decided to fire the Activity Monitor to check if the query stops for any lock in table, but I found that the task that is executing the query goes into heavy parallelism (you see a lot of row in the activity monitor with the same id), but each subtask is waiting a lot with a&#160; <em><strong>CXPACKET wait type</strong></em> and everything seems stuck. <a href="http://www.mssqltips.com/sqlservertip/2027/a-closer-look-at-cxpacket-wait-type-in-sql-server/">CXPACKET wait time happens when the execution of the query is parallelized</a> so I decided to disable parallelism of the query with <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms181007.aspx"><strong>OPTION (MAXDOP 1</strong></a><strong>) </strong>to verify if the situation change<strong>. </strong>The result is that the query now executed in ~2 secs, like it did two months before, so I decided to leave it with parallelism disabled and it start to run just fine.</p>
<p>The conclusion is,: when database is big, and query are complex, it is not so simple to understand why a query is slow, sometimes you need to tweak how <a title="SQL Server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">SQL Server</a> issue the query with Query Hint and you should use all the <a title="tools" href="http://www.codewrecks.com/blog/index.php/tag/tools/">tools</a> you have to understand what is really happening <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> .</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/01/31/when-it-is-time-to-tweak-sql-server-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hardcore fix error in Sql Server database</title>
		<link>http://www.codewrecks.com/blog/index.php/2012/01/31/hardcore-fix-error-in-sql-server-database/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/01/31/hardcore-fix-error-in-sql-server-database/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 09:20:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/01/31/hardcore-fix-error-in-sql-server-database/</guid>
		<description><![CDATA[In a production Sql Server database we had some issue with the hardware, the result is that one very big database started to gave us errors on DBCC CHECKDB, the error is the following one. Msg 8929, Level 16, State 1, Line 1 Object ID xxxxxx, index ID 1, partition ID xxxxxx, alloc unit ID [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F01%2F31%2Fhardcore-fix-error-in-sql-server-database%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F01%2F31%2Fhardcore-fix-error-in-sql-server-database%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Sql+Server&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In a production <a title="Sql Server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">Sql Server</a> database we had some issue with the <a title="hardware" href="http://www.codewrecks.com/blog/index.php/tag/hardware/">hardware</a>, the result is that one very big database started to gave us errors on DBCC CHECKDB, the error is the following one.</p>
<blockquote><p>Msg 8929, Level 16, State 1, Line 1      <br />Object ID xxxxxx, index ID 1, partition ID xxxxxx, alloc unit ID xxxxx (type In-row data): Errors found in off-row data with ID xxxxxxx owned by data record identified by RID = (3:34252:4)</p>
</blockquote>
<p>This happens in one of the most central and important table of the database, that contains millions of record, and only 20 rows gave us error, so I’d like to verify witch rows had the error, because in that table there are lots of records in logical status “deleted” and the corresponding physical row can be removed from the database without much pain and reinserted with the very same data to avoid dataloss.</p>
<p>So my question is… how can I identify the row given the RID (Row Identifier?) First of all from Object ID you can easily find the name of the table with <strong>select Object_Name(&lt;Object_ID&gt;) </strong>but what about the RID. After a little search I find the sys.fn_physLocFormatter function and I created this query</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">SELECT</span> sys.fn_PhysLocFormatter (%%physloc%%) <span style="color: rgb(0, 0, 255);">AS</span> RID,  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>         *</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span> <span style="color: rgb(0, 0, 255);">into</span> debug.RIDTABLE </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span> <span style="color: rgb(0, 0, 255);">FROM</span> TableName</pre>
<p><!--CRLF--></div>
</div>
<p>I selected into a table in Debug schema the RID of the Row as well as all the other columns of the table with error, now I take the output of the DBCC CHECKDB, selected all the RID of rows with errors and I could execute the query</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">select</span> * <span style="color: rgb(0, 0, 255);">from</span> debug.RIDTABLE</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> <span style="color: rgb(0, 0, 255);">where</span> RID <span style="color: rgb(0, 0, 255);">IN</span> (<span style="color: rgb(0, 96, 128);">'(3:34252:4)'</span>, <span style="color: rgb(0, 96, 128);">'(x:xxxxx:x)'</span>..... </pre>
<p><!--CRLF--></div>
</div>
<p>To extract from the RIDTAble all the row with errors, then I can simply delete them from the original table, and insert them again from the RIDTABLE. Checking the data inside RIDTABLE it seems that the data is correct, except for a NVARCHAR(MAX) field that contains long string of text. </p>
<p>After the process DBCC CHECKDB did not give any other error, so I was quite happy to solve this problem without the need to put the database in single-user-mode, but the most important aspect is that I was able to identify the rows with error, to understand the impact of errors in the software. It turned out that only a couple of lines contained really important data, the others are in Deleted status, so we did not lost anything so important.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/01/31/hardcore-fix-error-in-sql-server-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why Management Studio forces you to know SQL</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/12/28/why-management-studio-forces-you-to-know-sql/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/12/28/why-management-studio-forces-you-to-know-sql/#comments</comments>
		<pubDate>Wed, 28 Dec 2011 10:22:47 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/12/28/why-management-studio-forces-you-to-know-sql/</guid>
		<description><![CDATA[Suppose you have a big table with about 3 GB of data in and you need to add a nullable new column on it, you can open SSMS, open the table in designer and create a new column of type Int called sclo_durationInMinutes, press save and the table gets update quite immediately. Then you realize [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F12%2F28%2Fwhy-management-studio-forces-you-to-know-sql%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F12%2F28%2Fwhy-management-studio-forces-you-to-know-sql%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Sql+Server&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Suppose you have a big table with about 3 GB of data in and you need to add a nullable new column on it, you can open SSMS, open the table in designer and create a new column of type Int called sclo_durationInMinutes, press save and the table gets update quite immediately.</p>
<p>Then you realize that the user want the duration in Minutes as a floating point number, so you open the designer, change the type of the sclo_durationInMinutes from Int to Float and press save…. after 30 seconds SSMS tells you that you got a timeout. If you look at the change script you can verify, with HORROR, that changing the type of a column is done with the creation of a temporary table, copy all the data (3 GB) in the temp table and finally deleting the old table and renaming the temp table with the name of the original table… REALLY?</p>
<p>Then I simply opened a new query and issue an ALTER TABLE manually</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">alter</span> <span style="color: rgb(0, 0, 255);">table</span> dbo.scanlog</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 0, 255);">alter</span> <span style="color: rgb(0, 0, 255);">column</span> sclo_durationInMinutes <span style="color: rgb(0, 0, 255);">Float</span> <span style="color: rgb(0, 0, 255);">null</span> </pre>
<p><!--CRLF--></div>
</div>
<p>I know that SSMS should work for a wide range of change, but requiring a full copy of a table just to change the type of a column it is quite overkill.</p>
<p>In the end, if you use SSMS it is better for you to have a deep knowledge of SQL, because the designer too much often opts for Copy-drop-rename strategy to alter structure of a table.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/12/28/why-management-studio-forces-you-to-know-sql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Some interesting links for SQL Server 2012 data tools</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/11/28/some-interesting-links-for-sql-server-2012-data-tools/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/11/28/some-interesting-links-for-sql-server-2012-data-tools/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 08:43:09 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQLite]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/11/28/some-interesting-links-for-sql-server-2012-data-tools/</guid>
		<description><![CDATA[I love Database Project, introduced with Visual Studio 2008, and with SQL server 2012 they will be replaced by the Data Tools (codename Juneau), so I have a couple of links to share about this argument. In this link you can find a table that compares all the features. As you can see, actually, one [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F11%2F28%2Fsome-interesting-links-for-sql-server-2012-data-tools%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F11%2F28%2Fsome-interesting-links-for-sql-server-2012-data-tools%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=SQLite&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I love Database Project, introduced with <a target="_blank" title="Visual Studio" href="http://blogs.msdn.com/b/visualstudio/">Visual Studio</a> 2008, and with <a title="SQL server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">SQL server</a> 2012 they will be replaced by the Data <a title="Tools" href="http://www.codewrecks.com/blog/index.php/tag/tools/">Tools</a> (codename Juneau), so I have a couple of links to share about this argument.</p>
<p>In <a target="_blank" href="http://blogs.msdn.com/b/ssdt/archive/2011/11/21/sql-server-data-tools-ctp4-vs-vs2010-database-projects.aspx">this link</a> you can find a table that compares all the features. As you can see, actually, one of the most missed feature in my opinion is the Data Generation and Database Unit <a title="Testing" href="http://www.codewrecks.com/blog/index.php/category/programming/testing/">Testing</a> support. The important stuff is that, as stated in the blog, <em>SSDT can be used side-by-side with <a title="VSDB" href="http://www.codewrecks.com/blog/index.php/tag/vsdb/">VSDB</a> if your application relies on features not included in SSDT CTP4</em>, so at least if you have Database Projects where you use Unit <a title="Test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">Test</a> you can still use them with no problem.</p>
<p>If you are going to convert existing Database Project into SSDT you should read <a target="_blank" href="http://blogs.msdn.com/b/ssdt/archive/2011/11/21/top-vsdb-gt-ssdt-project-conversion-issues.aspx">this post</a> that list the most common issues you can face during the conversion.</p>
<p>If you are moving from CTP3 to CTP4, you should read <a target="_blank" href="http://blogs.msdn.com/b/ssdt/archive/2011/11/21/uninstalling-ssdt-ctp3-code-named-juneau.aspx">this post</a>, that basically explain how to uninstall CTP3. Clearly I strongly suggest people to install CTP in <a title="Virtual Machine" href="http://www.codewrecks.com/blog/index.php/tag/virtual-machine/">Virtual Machine</a> so you should not need this, but just in case you installed CTP3 in production machine <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , it will be handy.</p>
<p>Finally <a target="_blank" href="http://blogs.msdn.com/b/ssdt/archive/2011/11/21/what-s-new-in-sql-server-data-tools-ctp4.aspx">here is a list of the new feature</a>s of the CTP4.</p>
<p>Enjoy Denali.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/11/28/some-interesting-links-for-sql-server-2012-data-tools/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Trim all non-alpha character from SQL string</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/10/28/trim-all-non-alpha-character-from-sql-string/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/10/28/trim-all-non-alpha-character-from-sql-string/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 09:06:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/10/28/trim-all-non-alpha-character-from-sql-string/</guid>
		<description><![CDATA[I’have a table with some dirty data, if you select it seems that everything is ok, but since it was imported from an external source, it happens that some string field actually ends with strange non alpha char. The symptom is that I have some strange behavior on some data, then I verify what is [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F10%2F28%2Ftrim-all-non-alpha-character-from-sql-string%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F10%2F28%2Ftrim-all-non-alpha-character-from-sql-string%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Sql+Server&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I’have a table with some dirty data, if you select it seems that everything is ok, but since it was imported from an external source, it happens that some string field actually ends with strange non alpha char.</p>
<p>The symptom is that I have some strange behavior on some data, then I verify what is the content of that row, so I issue a Select * from xxx where Name = ‘Azioni’ and got no result, so I select everything that contains Azioni and I found the record. This is the clear symptom that the field contains some strange stuff in it, so I simply to a Copy and paste in the editor to see <em>exactly </em>what is stored in the field, and I found</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/10/image3.png"><img style="margin: 0px; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/10/image_thumb3.png" width="178" height="70" /></a></p>
<p>Do you see the strange char at the end? So I need a trim function that removes every non alphabetic character from the beginning and the end of the string. Looking in the internet I found <a target="_blank" href="http://stackoverflow.com/questions/1007697/how-to-strip-all-non-alphabetic-characters-from-string-in-sql-server">this interesting article</a> that lists a function that remove all non alpha char from the string, so I modified to make it remove non-alpha char only from the beginning and the end. </p>
<p>Here it is if you need it:</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">create</span> <span style="color: rgb(0, 0, 255);">Function</span> [dbo].[TrimNonAlphaCharacters](@Temp nVarChar(<span style="color: rgb(0, 0, 255);">max</span>))</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 0, 255);">Returns</span> nVarChar(<span style="color: rgb(0, 0, 255);">max</span>)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">AS</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 0, 255);">Begin</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&#160;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">While</span> PatIndex(<span style="color: rgb(0, 96, 128);">'[^a-z]%'</span>, @Temp) &gt; 0</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        <span style="color: rgb(0, 0, 255);">Set</span> @Temp = Stuff(@Temp, PatIndex(<span style="color: rgb(0, 96, 128);">'[^a-z]%'</span>, @Temp), 1, <span style="color: rgb(0, 96, 128);">''</span>)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&#160;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">While</span> PatIndex(<span style="color: rgb(0, 96, 128);">'%[^a-z]'</span>, @Temp) &gt; 0</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        <span style="color: rgb(0, 0, 255);">Set</span> @Temp = Stuff(@Temp, PatIndex(<span style="color: rgb(0, 96, 128);">'%[^a-z]'</span>, @Temp), 1, <span style="color: rgb(0, 96, 128);">''</span>)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">Return</span> @TEmp</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">End</span></pre>
<p><!--CRLF--></div>
</div>
<p>I’ve not tested it extensively, just ran it against my data and verified that it solves my problem, so I’m pretty confident that it works, but feel free to post any correction or any better implementation if you have one <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/10/28/trim-all-non-alpha-character-from-sql-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Always pay attention to uniqueness to SQL Index</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/10/06/always-pay-attention-to-uniqueness-to-sql-index/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/10/06/always-pay-attention-to-uniqueness-to-sql-index/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 09:58:06 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/10/06/always-pay-attention-to-uniqueness-to-sql-index/</guid>
		<description><![CDATA[I have a database with several Gigabyte of data and query performance is usually a issue, so we need to take great care of indexes and DB optimization. Since the vast majority of data access is done with NHibernate, we have also some read-only view that we use to easy the access from the views. [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F10%2F06%2Falways-pay-attention-to-uniqueness-to-sql-index%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F10%2F06%2Falways-pay-attention-to-uniqueness-to-sql-index%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Sql+Server&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I have a database with several Gigabyte of data and query <a title="performance" href="http://www.codewrecks.com/blog/index.php/tag/performance/">performance</a> is usually a issue, so we need to take great care of indexes and DB optimization. Since the vast majority of data access is done with <a title="NHibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">NHibernate</a>, we have also some read-only view that we use to easy the access from the views.</p>
<p>One of this view, have four left outer join from a main table to other four tables and we have a SELECT COUNT query that is quite slow, so we decide to understand how to optimize it. I started looking at the execution plan and I found this.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/10/image.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/10/image_thumb.png" width="557" height="222" /></a></p>
<p><strong>Figure 1: </strong><em>Original execution plan.</em></p>
<p>The execution plan clearly shows that we have a join between two tables, but I’m issuing a SELECT COUNT with condition on fields of the first table only, the question is: why <a title="sql server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">sql server</a> issue a join with another table, if the other table does not contains fields that are involved on the query?</p>
<p>The answer is really simple, a JOIN between two tables influences the number of rows returned, suppose that for each record of the main table we have 2 records on the table in join, this situation affects the SELECT COUNT operation, so <a title="SQL SERVER" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">SQL SERVER</a> has no choice, he need to do the JOIN. Our Data Model enforces in Business Logic that for each record in the main table, at most we can have a single related record on the other table, so I decided to make the index that supports the foreign key unique (it was not unique because we usually enforce this type of constraint in business logic).</p>
<p>Once the index is <em>Unique</em>, <a title="Sql Server" href="http://www.codewrecks.com/blog/index.php/tag/sql-server/">Sql Server</a> knows that the join could not affect the result of SELECT COUNT and from execution plan I verified that the join is gone and performances are greatly improved.</p>
<p>The motto is: if you have Business Logic that enforce uniqueness, creating a unique index at the Database Level if you are using that field in join can lead to better execution plans.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/10/06/always-pay-attention-to-uniqueness-to-sql-index/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

