<?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>youdidwhatwithtsql.com &#187; T-SQL</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/t-sql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Deleting sequential duplicates with TSQL</title>
		<link>http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869</link>
		<comments>http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869#comments</comments>
		<pubDate>Wed, 01 Sep 2010 17:02:42 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[duplicates]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869</guid>
		<description><![CDATA[I was recently given a du-duping task which was much more difficult than I anticipated and taxed my SQL brain to its limits. I thought of using a CTE to do this but all of the examples I could find for deleting records with a CTE wouldn’t have worked in my situation. Essentially the table [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869">Deleting sequential duplicates with TSQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I was recently given a du-duping task which was much more difficult than I anticipated and taxed my SQL brain to its limits. I thought of using a <a href="http://msdn.microsoft.com/en-us/library/ms190766.aspx" target="_blank">CTE</a> to do this but all of the examples I could find for <a href="http://blog.sqlauthority.com/2009/06/23/sql-server-2005-2008-delete-duplicate-rows/" target="_blank">deleting records with a CTE</a> wouldn’t have worked in my situation.</p>
<p>Essentially the table had a business key consisting of 3 parts. Each record would also come with two values attached. The current system pumped data into this fairly regularly and we only wanted to keep values that were not sequentially duplicated. By “sequentially duplicated” I mean an insert into a feed table has the same values attached as the previously inserted record for the same business key. For example, assuming a single key, the below table illustrates the input sequence and the desired de-dupe result.</p>
<table border="0" cellspacing="2" cellpadding="2" width="400">
<tbody>
<tr>
<td valign="top" width="200"><strong>sequence</strong></td>
<td valign="top" width="200"><strong>de-duped</strong></td>
</tr>
<tr>
<td valign="top" width="200">1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</td>
<td valign="top" width="200">1</td>
</tr>
<tr>
<td valign="top" width="200">1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1</td>
<td valign="top" width="200">1,2,1</td>
</tr>
<tr>
<td valign="top" width="200">1,2,3,4,5,4,3,2,1</td>
<td valign="top" width="200">1,2,3,4,5,4,3,2,1</td>
</tr>
</tbody>
</table>
<p>Now for a SQL example;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p869code4'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8694"><td class="code" id="p869code4"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Create a test table</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">TestDupes</span>
<span style="color: #808080;">&#40;</span>
	Id <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>,
	KeyPart1 <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	KeyPart2 <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	KeyPart3 <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	value1 <span style="color: #0000FF;">FLOAT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	value2 <span style="color: #0000FF;">FLOAT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
<span style="color: #008080;">-- Insert some test data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">TestDupes</span>
<span style="color: #808080;">&#40;</span>
	KeyPart1,
	KeyPart2,
	KeyPart3,
	value1,
	value2
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>;
GO</pre></td></tr></table></div>

<p>The records considered to be duplicates are outlined in red in the image below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/sequential_dupes.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sequential dupes" border="0" alt="sequential dupes thumb Deleting sequential duplicates with TSQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/sequential_dupes_thumb.png" width="644" height="451" /></a> </p>
<p>A sequentially de-duped dataset would look like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/no_sequential_dupes.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="no sequential dupes" border="0" alt="no sequential dupes thumb Deleting sequential duplicates with TSQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/no_sequential_dupes_thumb.png" width="644" height="282" /></a></p>
<p>The solution I came up with, after much head-scratching, involved the use of the <a href="http://msdn.microsoft.com/en-us/library/ms173825.aspx" target="_blank">DENSE_RANK</a> &amp; <a href="http://msdn.microsoft.com/en-us/library/ms186734.aspx" target="_blank">ROW_NUMBER</a> functions combined with a <a href="http://msdn.microsoft.com/en-us/library/ms190766.aspx" target="_blank">Common Table Expression</a>. The DENSE_RANK function organises each business key (KeyPart1, KeyPart2, KeyPart3) into groups. The ROW_NUMBER function gives us a sequence number for each record within the group. This is best illustrated with a select statement with another group thrown in for good measure.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p869code5'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8695"><td class="code" id="p869code5"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">TRUNCATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">TestDupes</span>;
<span style="color: #008080;">-- Insert some test data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">TestDupes</span>
<span style="color: #808080;">&#40;</span>
	KeyPart1,
	KeyPart2,
	KeyPart3,
	value1,
	value2
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #008080;">-- Group 2 </span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>;
GO
&nbsp;
<span style="color: #0000FF;">WITH</span> Dupe_CTE <span style="color: #808080;">&#40;</span>
			Id, 
			KeyPart1, 
			KeyPart2, 
			KeyPart3, 
			value1, 
			value2, 
			group_num, 
			group_row_num
		<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #0000FF;">SELECT</span> Id,
	       KeyPart1,
	       KeyPart2,
	       KeyPart3,
	       value1,
	       value2,
               DENSE_RANK<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">OVER</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> KeyPart1, KeyPart2, KeyPart3<span style="color: #808080;">&#41;</span>,
               ROW_NUMBER<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">OVER</span><span style="color: #808080;">&#40;</span>PARTITION <span style="color: #0000FF;">BY</span> KeyPart1,
                                          KeyPart2,
                                          KeyPart3
                                 <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> Id<span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">TestDupes</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> Dupe_CTE;</pre></td></tr></table></div>

<p>Here you can see the data is organised into groups and each row, within the group, is numbered according to it&#8217;s position.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/feed_dupes_two_sets.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="duplicates organise into groups with DENSE_RANK" border="0" alt="feed dupes two sets thumb Deleting sequential duplicates with TSQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/feed_dupes_two_sets_thumb.png" width="582" height="484" /></a></p>
<p>Using this information about each group we can execute a delete. The delete self-joins to the CTE comparing the values for value1 and value2.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p869code6'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8696"><td class="code" id="p869code6"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">WITH</span> Dupe_CTE <span style="color: #808080;">&#40;</span>
			Id, 
			KeyPart1, 
			KeyPart2, 
			KeyPart3, 
			value1, 
			value2, 
			group_num, 
			group_row_num
		<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #0000FF;">SELECT</span> Id,
		   KeyPart1,
		   KeyPart2,
		   KeyPart3,
		   value1,
		   value2,
           DENSE_RANK<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">OVER</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> KeyPart1, KeyPart2, KeyPart3<span style="color: #808080;">&#41;</span>,
           ROW_NUMBER<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">OVER</span><span style="color: #808080;">&#40;</span>PARTITION <span style="color: #0000FF;">BY</span> KeyPart1,
                                          KeyPart2,
                                          KeyPart3
                             <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> Id<span style="color: #808080;">&#41;</span>
           <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">TestDupes</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">DELETE</span> fg2
<span style="color: #0000FF;">FROM</span> Dupe_CTE fg1
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> Dupe_CTE fg2
                <span style="color: #0000FF;">ON</span> fg1.<span style="color: #202020;">Group_Num</span> <span style="color: #808080;">=</span> fg2.<span style="color: #202020;">Group_Num</span>
                <span style="color: #808080;">AND</span> fg1.<span style="color: #202020;">value1</span> <span style="color: #808080;">=</span> fg2.<span style="color: #202020;">value1</span>
                <span style="color: #808080;">AND</span> fg1.<span style="color: #202020;">value2</span> <span style="color: #808080;">=</span> fg2.<span style="color: #202020;">value2</span>
<span style="color: #0000FF;">WHERE</span> fg1.<span style="color: #202020;">group_row_num</span> <span style="color: #808080;">=</span> fg2.<span style="color: #202020;">group_row_num</span> <span style="color: #808080;">-</span> <span style="color: #000;">1</span>;</pre></td></tr></table></div>

<p>This will remove any duplicates, keeping the oldest record in each case, giving us a clean dataset.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/cleaned_up_group_duplicates.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="cleaned up group duplicates" border="0" alt="cleaned up group duplicates thumb Deleting sequential duplicates with TSQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/cleaned_up_group_duplicates_thumb.png" width="644" height="439" /></a></p>
<p>Can anyone think of a <strike>CURSOR</strike> better way of doing this?</p>
<p><map name='google_ad_map_869_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/869?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_869_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=869&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fdeleting-sequential-duplicates-with-tsql%2F869' title="Deleting sequential duplicates with TSQL" alt=" Deleting sequential duplicates with TSQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869">Deleting sequential duplicates with TSQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Unsigned Integer Arithmetic in SQL</title>
		<link>http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794</link>
		<comments>http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794#comments</comments>
		<pubDate>Sat, 12 Jun 2010 16:45:27 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[integers]]></category>
		<category><![CDATA[unsigned]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794</guid>
		<description><![CDATA[Not the sexiest blog title in the world but I thought I’d knock up a little post on the behaviour of MySQL and SQL Server with integer subtraction. How would you expect a database system to behave with positive and negative data types? Microsoft SQL Server doesn’t really have unsigned data types. All integer types [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794">Unsigned Integer Arithmetic in SQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Not the sexiest blog title in the world but I thought I’d knock up a little post on the behaviour of MySQL and SQL Server with integer subtraction. How would you expect a database system to behave with positive and negative data types? Microsoft SQL Server doesn’t really have unsigned data types. All integer types can be positive and negative with the exception of <a href="http://msdn.microsoft.com/en-us/library/ms187745.aspx" target="_blank">TINYINT</a>. MySQL implements the concept of <a href="http://en.wikipedia.org/wiki/Signedness" target="_blank">signedness</a> so we can specify that TINYINT ranges from –128 to 127 or 0 to 255.</p>
<p>What would you expect SQL Server to do with this?</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p794code10'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p79410"><td class="code" id="p794code10"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @num1 <span style="color: #0000FF;">TINYINT</span> <span style="color: #808080;">=</span> <span style="color: #000;">50</span>, @num2 <span style="color: #0000FF;">TINYINT</span> <span style="color: #808080;">=</span> <span style="color: #000;">75</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> @num1 <span style="color: #808080;">-</span> @num2;</pre></td></tr></table></div>

<p>Well it errors. Good RDBMS! </p>
<pre>Msg 8115, Level 16, State 2, Line 3
Arithmetic overflow error converting expression to data type tinyint.</pre>
<p>What does MySQL do? Lets see.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p794code11'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p79411"><td class="code" id="p794code11"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">USE</span> test<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> nums
<span style="color: #FF00FF;">&#40;</span>
	num1 <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">UNSIGNED</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	num2 <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">UNSIGNED</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> nums
<span style="color: #FF00FF;">&#40;</span>
	num1<span style="color: #000033;">,</span>
	num2
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">VALUES</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008080;">50</span><span style="color: #000033;">,</span>
	<span style="color: #008080;">75</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> num1 <span style="color: #CC0099;">-</span> num2
<span style="color: #990099; font-weight: bold;">FROM</span> nums<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/06/mysql_unsigned_division.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="mysql unsigned division" border="0" alt="mysql unsigned division thumb Unsigned Integer Arithmetic in SQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/06/mysql_unsigned_division_thumb.png" width="644" height="216" /></a> </p>
<p>Oh dear not a pretty behaviour! Bad RDBMS! A colleague said to me “you need to set <a href="http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html" target="_blank">sql_mode</a> to traditional”. Mmmmmm, I thought I was running in traditional mode already. As it turns out we need to set the NO_UNSIGNED_SUBTRACTION option.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p794code12'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p79412"><td class="code" id="p794code12"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SET</span> @@<span style="color: #990099; font-weight: bold;">SESSION</span>.sql_mode <span style="color: #CC0099;">=</span> <span style="color: #008000;">'TRADITIONAL,NO<span style="color: #008080; font-weight: bold;">_</span>UNSIGNED<span style="color: #008080; font-weight: bold;">_</span>SUBTRACTION'</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> num1 <span style="color: #CC0099;">-</span> num2
<span style="color: #990099; font-weight: bold;">FROM</span> nums<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/06/mysql_correct_unsigned_division.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="mysql correct unsigned division" border="0" alt="mysql correct unsigned division thumb Unsigned Integer Arithmetic in SQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/06/mysql_correct_unsigned_division_thumb.png" width="644" height="219" /></a> </p>
<p>Got to love MySQL for keeping you on your toes!</p>
<p><map name='google_ad_map_794_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/794?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_794_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=794&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Funsigned-integer-arithmetic-in-sql%2F794' title="Unsigned Integer Arithmetic in SQL" alt=" Unsigned Integer Arithmetic in SQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794">Unsigned Integer Arithmetic in SQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Breaking my Non-Equi Join cherry</title>
		<link>http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652</link>
		<comments>http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652#comments</comments>
		<pubDate>Wed, 17 Feb 2010 21:56:16 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[non-equi joins]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652</guid>
		<description><![CDATA[There&#8217;s few SQL techniques you seem to keep in the cupboard gathering dust. I don&#8217;t think I&#8217;ve ever needed to use RIGHT JOIN outside of the classroom. I can recall using FULL OUTER JOIN, just once, to show an employer how not-in-sync their &#34;integrated system&#34; was. Today I broke my professional Non-Equi JOIN cherry! I [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652">Breaking my Non-Equi Join cherry</a></p>
]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s few SQL techniques you seem to keep in the cupboard gathering dust. I don&#8217;t think I&#8217;ve ever needed to use <a href="http://en.wikipedia.org/wiki/Join_(SQL)#Right_outer_joins" target="_blank">RIGHT JOIN</a> outside of the classroom. I can recall using <a href="http://en.wikipedia.org/wiki/Join_(SQL)#Full_outer_join" target="_blank">FULL OUTER JOIN</a>, just once, to show an employer how not-in-sync their &quot;integrated system&quot; was. Today I broke my professional <a href="http://www.orafaq.com/wiki/Nonequi_join" target="_blank">Non-Equi JOIN</a> cherry!</p>
<p>I basically had one table of appointments and another table providing appointment banding by a date range.&#160; The banding wasn&#8217;t consistent, spanning weeks and months,&#160; so there was no possibility of using an equi-join or doing anything with datetime arithmetic. Perhaps that non-equi join thing I remember reading in the textbook will do?</p>
<p>Here&#8217;s a quick run-through of a similar situation. Create some tables and insert some test data. (This first example is in <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> 2005)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p652code17'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65217"><td class="code" id="p652code17"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Appointments</span>
<span style="color: #808080;">&#40;</span>
	AppointId <span style="color: #0000FF;">INTEGER</span> <span style="color: #0000FF;">IDENTITY</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	AppointmentDateTime <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	FirstName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	LastName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	Reason <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> AppointmentBands
<span style="color: #808080;">&#40;</span>
	AppointBandId <span style="color: #0000FF;">INTEGER</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	StartDateTime <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	EndDateTime <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
<span style="color: #008080;">-- Insert Test Data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Appointments</span>
<span style="color: #808080;">&#40;</span>
	AppointmentDateTime,
	FirstName,
	LastName,
	Reason
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2010-02-18T17:00:00'</span>,
	   <span style="color: #FF0000;">'Rhys'</span>,
	   <span style="color: #FF0000;">'Campbell'</span>,
	   <span style="color: #FF0000;">'Eye Test'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2010-02-23T12:00:00'</span>,
	   <span style="color: #FF0000;">'John'</span>,
	   <span style="color: #FF0000;">'Smith'</span>,
	   <span style="color: #FF0000;">'Ear Test'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2010-02-28T14:00:00'</span>,
	   <span style="color: #FF0000;">'Frank'</span>,
	   <span style="color: #FF0000;">'Zappa'</span>,
	   <span style="color: #FF0000;">'Eye Test'</span>;
GO
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">AppointmentBands</span>
<span style="color: #808080;">&#40;</span>
	StartDateTime,
	EndDateTime
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2010-02-18T00:00:00'</span>,
	   <span style="color: #FF0000;">'2010-02-22T23:59:59'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2010-02-23T00:00:00'</span>,
	   <span style="color: #FF0000;">'2010-02-27T23:59:59'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2010-02-28T00:00:00'</span>,
	   <span style="color: #FF0000;">'2010-03-01T23:59:59'</span>;</pre></td></tr></table></div>

<p>I needed to identify which appointment band each record belonged to. It&#8217;s a trivial example here, but the real life situation involved hundreds of thousands of appointments and a few thousand appointment bands. The solution involved using a <a href="http://msdn.microsoft.com/en-us/library/ms187922.aspx" target="_blank">BETWEEN</a> in the join clause.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p652code18'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65218"><td class="code" id="p652code18"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> app.<span style="color: #808080;">*</span>, band.<span style="color: #202020;">AppointBandId</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Appointments</span> app
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> dbo.<span style="color: #202020;">AppointmentBands</span> band
	<span style="color: #0000FF;">ON</span> app.<span style="color: #202020;">AppointmentDateTime</span> <span style="color: #808080;">BETWEEN</span> band.<span style="color: #202020;">StartDateTime</span> <span style="color: #808080;">AND</span> band.<span style="color: #202020;">EndDateTime</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/appointments_non_equi_join.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="appointments non equi join" border="0" alt="appointments non equi join thumb Breaking my Non Equi Join cherry" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/appointments_non_equi_join_thumb.png" width="644" height="188" /></a> </p>
<p>Such a simple, elegant, single query solution. Perhaps we need to dust these things off from time-to-time? </p>
<p>Here&#8217;s the same example, a&#8217;la <a href="http://www.mysql.com" target="_blank">MySQL</a></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p652code19'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65219"><td class="code" id="p652code19"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> Appointments
<span style="color: #FF00FF;">&#40;</span>
	AppointId <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span><span style="color: #000033;">,</span>
	AppointmentDateTime <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	FirstName <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	LastName <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	Reason <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> AppointmentBands
<span style="color: #FF00FF;">&#40;</span>
	AppointBandId <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span><span style="color: #000033;">,</span>
	StartDateTime <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	EndDateTime <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Insert Test Data</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> Appointments
<span style="color: #FF00FF;">&#40;</span>
	AppointmentDateTime<span style="color: #000033;">,</span>
	FirstName<span style="color: #000033;">,</span>
	LastName<span style="color: #000033;">,</span>
	Reason
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'2010-02-18T17:00:00'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Rhys'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Campbell'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Eye Test'</span>
<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'2010-02-23T12:00:00'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'John'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Smith'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Ear Test'</span>
<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'2010-02-28T14:00:00'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Frank'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Zappa'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Eye Test'</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> AppointmentBands
<span style="color: #FF00FF;">&#40;</span>
	StartDateTime<span style="color: #000033;">,</span>
	EndDateTime
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'2010-02-18T00:00:00'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'2010-02-22T23:59:59'</span>
<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'2010-02-23T00:00:00'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'2010-02-27T23:59:59'</span>
<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'2010-02-28T00:00:00'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'2010-03-01T23:59:59'</span><span style="color: #000033;">;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p652code20'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65220"><td class="code" id="p652code20"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> app.<span style="color: #CC0099;">*</span><span style="color: #000033;">,</span> band.AppointBandId
<span style="color: #990099; font-weight: bold;">FROM</span> Appointments app
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> AppointmentBands band
	<span style="color: #990099; font-weight: bold;">ON</span> app.AppointmentDateTime <span style="color: #CC0099; font-weight: bold;">BETWEEN</span> band.StartDateTime <span style="color: #CC0099; font-weight: bold;">AND</span> band.EndDateTime<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/MySQL_non_equi_join.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MySQL non equi join" border="0" alt="MySQL non equi join thumb Breaking my Non Equi Join cherry" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/MySQL_non_equi_join_thumb.png" width="644" height="140" /></a></p>
<p><map name='google_ad_map_652_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/652?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_652_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=652&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fbreaking-my-non-equi-join-cherry%2F652' title="Breaking my Non Equi Join cherry" alt=" Breaking my Non Equi Join cherry" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652">Breaking my Non-Equi Join cherry</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Views or functions cannot reference themselves directly or indirectly</title>
		<link>http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519</link>
		<comments>http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519#comments</comments>
		<pubDate>Thu, 14 Jan 2010 21:18:38 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[self-reference]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519</guid>
		<description><![CDATA[Today I received the following SQL Server error which I had never encountered before. Msg 4429, Level 16, State 1, Line 1 View or function &#8216;Table_1&#8242; contains a self-reference. Views or functions cannot reference themselves directly or indirectly. Msg 4413, Level 16, State 1, Line 1 Could not use view or function &#8216;Test.Table_1&#8242; because of [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519">Views or functions cannot reference themselves directly or indirectly</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Today I received the following <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> error which I had never encountered before.</p>
<blockquote><p>Msg 4429, Level 16, State 1, Line 1      <br />View or function &#8216;Table_1&#8242; contains a self-reference. Views or functions cannot reference themselves directly or indirectly.       <br />Msg 4413, Level 16, State 1, Line 1       <br />Could not use view or function &#8216;Test.Table_1&#8242; because of binding errors.</p>
</blockquote>
<p>Here&#8217;s a quick walk-through of the issue.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code26'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51926"><td class="code" id="p519code26"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Table_1<span style="color: #808080;">&#93;</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">&#91;</span>id<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">int</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>test<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">nchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Insert some test data</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code27'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51927"><td class="code" id="p519code27"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Table_1</span>
<span style="color: #808080;">&#40;</span>
	test
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'One'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Two'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Three'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Four'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Five'</span>;</pre></td></tr></table></div>

<p>Add a schema called &#8216;Test&#8217; to the database by running the TSQL below. We will then create a view in this schema.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code28'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51928"><td class="code" id="p519code28"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">SCHEMA</span> Test;</pre></td></tr></table></div>

<p>Now create this view.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code29'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51929"><td class="code" id="p519code29"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> Test.<span style="color: #202020;">Table_1</span>
<span style="color: #0000FF;">AS</span>
	<span style="color: #0000FF;">SELECT</span> Id, Test
	<span style="color: #0000FF;">FROM</span> Table_1;</pre></td></tr></table></div>

<p>Now try to select from this view.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/sql_view_self_reference.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sql server view self reference error" border="0" alt="sql view self reference thumb Views or functions cannot reference themselves directly or indirectly" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/sql_view_self_reference_thumb.png" width="644" height="72" /></a>&#160;</p>
<p>I&#8217;d been creating lots of views for a third party application we&#8217;ve started to deploy at work. I&#8217;d simply created a new schema and named the views after the corresponding table in the database. This one was my fault for being a copy and paste monkey! I&#8217;d forgotten to specify the schema in one view. The fix?</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code30'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51930"><td class="code" id="p519code30"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">VIEW</span> Test.<span style="color: #202020;">Table_1</span>
<span style="color: #0000FF;">AS</span>
	<span style="color: #0000FF;">SELECT</span> Id, Test
	<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Table_1</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/view_results.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="view results" border="0" alt="view results thumb Views or functions cannot reference themselves directly or indirectly" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/view_results_thumb.png" width="156" height="144" /></a> </p>
<p>Even though my user <a href="http://msdn.microsoft.com/en-us/library/ms190387.aspx" target="_blank">default schema</a> was set to dbo <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> got its pants in a twist over this. So there you have it; it&#8217;s definitely good practice to specify schemas in your TSQL! </p>
<p><map name='google_ad_map_519_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/519?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_519_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=519&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fviews-or-functions-cannot-reference-themselves-directly-or-indirectly%2F519' title="Views or functions cannot reference themselves directly or indirectly" alt=" Views or functions cannot reference themselves directly or indirectly" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519">Views or functions cannot reference themselves directly or indirectly</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get database size With T-SQL and MySQL</title>
		<link>http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414</link>
		<comments>http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414#comments</comments>
		<pubDate>Sat, 31 Oct 2009 13:40:18 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Database documentation]]></category>
		<category><![CDATA[database size]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414</guid>
		<description><![CDATA[I&#8217;ve recently been busy documentation various systems at work and came up with these queries to get a list of databases and their sizes for each SQL Server. These queries will show the server name, database names, and their sizes in KB, MB and GB. For SQL Server 2005 &#38; 2008 ?View Code TSQL-- SQL [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414">Get database size With T-SQL and MySQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been busy documentation various systems at work and came up with these queries to get a list of databases and their sizes for each <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a>. These queries will show the server name, database names, and their sizes in KB, MB and GB.</p>
<p>For SQL Server 2005 &amp; 2008</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p414code34'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p41434"><td class="code" id="p414code34"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- SQL Server 2005 /2008</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #databases
<span style="color: #808080;">&#40;</span>
 DATABASE_NAME <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span>,
 DATABASE_SIZE <span style="color: #0000FF;">FLOAT</span>,
 REMARKS <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">INSERT</span> #Databases <span style="color: #0000FF;">EXEC</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'EXEC sp_databases'</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">@@SERVERNAME</span> <span style="color: #0000FF;">AS</span> SERVER_NAME,
    DATABASE_NAME,
    DATABASE_SIZE <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'(KB)'</span>,
    <span style="color: #FF00FF;">ROUND</span><span style="color: #808080;">&#40;</span>DATABASE_SIZE <span style="color: #808080;">/</span> <span style="color: #000;">1024</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'(MB)'</span>,
    <span style="color: #FF00FF;">ROUND</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>DATABASE_SIZE <span style="color: #808080;">/</span> <span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">1024</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'(GB)'</span>,
    REMARKS
<span style="color: #0000FF;">FROM</span> #databases;
&nbsp;
<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #databases;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/database_sizes_sql_server.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="database sizes sql server" border="0" alt="database sizes sql server thumb Get database size With T SQL and MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/database_sizes_sql_server_thumb.png" width="244" height="99" /></a> </p>
<p>For SQL Server 2000</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p414code35'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p41435"><td class="code" id="p414code35"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- SQL 2000</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">@@SERVERNAME</span> <span style="color: #0000FF;">AS</span> SERVER_NAME,
    db.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> DATABASE_NAME,
    <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">size</span> <span style="color: #808080;">*</span> <span style="color: #000;">8</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'(KB)'</span>,
    <span style="color: #FF00FF;">ROUND</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">size</span> <span style="color: #808080;">*</span> <span style="color: #000;">8</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">1024</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'(MB)'</span>,
    <span style="color: #FF00FF;">ROUND</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">size</span> <span style="color: #808080;">*</span> <span style="color: #000;">8</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'(GB)'</span>
<span style="color: #0000FF;">FROM</span> sysaltfiles files
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sysdatabases db
 <span style="color: #0000FF;">ON</span> db.<span style="color: #202020;">dbid</span> <span style="color: #808080;">=</span> files.<span style="color: #202020;">dbid</span>
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> db.<span style="color: #202020;">name</span></pre></td></tr></table></div>

<p>For MySQL. Note the <a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_hostname" target="_blank">@@hostname</a> variable. This was introduced in MySQL 5.0.3.8 so you&#8217;ll need to remove this if you&#8217;re using an earlier version.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p414code36'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p41436"><td class="code" id="p414code36"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- @@hostname variable introduced in MySQL 5.0.38</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> @@hostname<span style="color: #000033;">,</span>
       TABLE_SCHEMA <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #008000;">`DATABASE`</span><span style="color: #000033;">,</span>
       <span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_length <span style="color: #CC0099;">+</span> index_length <span style="color: #CC0099;">+</span>data_free<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #008000;">'(KB)'</span><span style="color: #000033;">,</span>
       <span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_length <span style="color: #CC0099;">+</span> index_length <span style="color: #CC0099;">+</span>data_free<span style="color: #FF00FF;">&#41;</span><span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #008000;">'(MB)'</span><span style="color: #000033;">,</span>
       <span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_length <span style="color: #CC0099;">+</span> index_length <span style="color: #CC0099;">+</span>data_free<span style="color: #FF00FF;">&#41;</span><span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span><span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #008000;">'(GB)'</span>
<span style="color: #990099; font-weight: bold;">FROM</span> information_schema.<span style="color: #990099; font-weight: bold;">TABLES</span>
<span style="color: #990099; font-weight: bold;">GROUP BY</span> TABLE_SCHEMA<span style="color: #000033;">;</span></pre></td></tr></table></div>

</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/database_sizes_mysql.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="database sizes mysql" border="0" alt="database sizes mysql thumb Get database size With T SQL and MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/database_sizes_mysql_thumb.png" width="244" height="114" /></a></p>
<p><map name='google_ad_map_414_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/414?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_414_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=414&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fget-database-size-with-t-sql-and-mysql%2F414' title="Get database size With T SQL and MySQL" alt=" Get database size With T SQL and MySQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414">Get database size With T-SQL and MySQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Computed Columns in SQL Server</title>
		<link>http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377</link>
		<comments>http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377#comments</comments>
		<pubDate>Sat, 26 Sep 2009 19:33:32 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Computed Columns]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377</guid>
		<description><![CDATA[So exactly what is a computed column? MSDN has this to say A computed column is computed from an expression that can use other columns in the same table. The expression can be a noncomputed column name, constant, function, and any combination of these connected by one or more operators. The expression cannot be a [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377">Computed Columns in SQL Server</a></p>
]]></description>
			<content:encoded><![CDATA[<p>So exactly what is a computed column? <a href="http://msdn.microsoft.com/en-us/library/ms191250.aspx" target="_blank">MSDN</a> has this to say</p>
<blockquote><p>A computed column is computed from an expression that can use other columns in the same table. The expression can be a noncomputed column name, constant, function, and any combination of these connected by one or more operators. The expression cannot be a subquery.</p>
</blockquote>
<p>Essentially there are two types of computed columns; a virtual column where the data is not physically stored in the table, values are calculated each time it is referenced in a query. These cannot be indexed. By making use of the PERSISTED TSQL keyword we can force the database engine to physically store this data in the table. These can be indexed provided the computation definition is deterministic. For example, we wouldn’t be permitted to index columns containing a call to the <a href="http://msdn.microsoft.com/en-us/library/ms188383.aspx" target="_blank">GETDATE</a> function.</p>
<p>Some people consider virtual computed columns to be a <a href="http://forums.mysql.com/read.php?10,218549,218555#msg-218555" target="_blank">waste of time</a>. Sure, it’s easy enough to build something like monthPay * 12 AS YearlySalary into your queries but in my opinion they offer the following benefits.</p>
<ul>
<li>Enforce a particular calculation method in your organisation. </li>
<li>Should the calculation need to change, modify the computed column definition and not loads of queries, procedures and reports. (Anyone having to cope with the UK’s <a href="http://www.hmrc.gov.uk/pbr2008/measure1.htm" target="_blank">change in VAT</a> last year will appreciate this). </li>
</ul>
<p>I’ve put persisted computed columns to good use recently. Some data came in from a client in an unexpected format. The record key value was sometimes contained in a column that was basically a <a href="http://en.wikipedia.org/wiki/Hodge-podge" target="_blank">hodge-podge</a> of notes. I created a computed column that extracted this value which then allowed it to be indexed. </p>
<p>Here’s an brief example of what I did to achieve this.</p>
<p>The code here uses SQL Server 2008. Firstly I created a <a href="http://msdn.microsoft.com/en-us/library/ms186755.aspx" target="_blank">user-defined function</a> that would extract the required key value from a note field.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p377code41'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37741"><td class="code" id="p377code41"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- This function will provide the computed column definition</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">FUNCTION</span> udf_extractPersonCode
<span style="color: #808080;">&#40;</span>
	@<span style="color: #0000FF;">input</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">RETURNS</span> <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">7</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">WITH</span> SCHEMAB<span style="color: #808080;">IN</span>D<span style="color: #808080;">IN</span>G
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
	<span style="color: #0000FF;">DECLARE</span> @extractedPersonCode <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">7</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #808080;">NULL</span>;
&nbsp;
	<span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">PATINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'%[A-Z][0-9][0-9][0-9][0-9][0-9][0-9]%'</span>, @<span style="color: #0000FF;">input</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">BEGIN</span>
		<span style="color: #0000FF;">SET</span> @extractedPersonCode <span style="color: #808080;">=</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">input</span>, <span style="color: #FF00FF;">PATINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'%[A-Z][0-9][0-9][0-9][0-9][0-9][0-9]%'</span>, @<span style="color: #0000FF;">input</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">7</span><span style="color: #808080;">&#41;</span>;
	<span style="color: #0000FF;">END</span>
&nbsp;
	<span style="color: #0000FF;">RETURN</span> @extractedPersonCode;
&nbsp;
<span style="color: #0000FF;">END</span>
GO</pre></td></tr></table></div>

<p>The WITH SCHEMABINDING is needed in the function definition or the following error will occur when attempting to create the function.</p>
<pre>Msg 4936, Level 16, State 1, Line 1
Computed column 'ExtractedPersonCode' in table 'ComputedColTable' cannot be persisted because the column is non-deterministic.</pre>
<p>Create this test table in the same database as the function. Note the definition of the computed column <strong>ExtractedPersonCode</strong>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p377code42'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37742"><td class="code" id="p377code42"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">ComputedColTable</span>
<span style="color: #808080;">&#40;</span>
	Id <span style="color: #0000FF;">INTEGER</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	FirstName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	LastName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	PersonCode <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">7</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	Notes <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	ExtractedPersonCode <span style="color: #0000FF;">AS</span> dbo.<span style="color: #202020;">udf_extractPersonCode</span><span style="color: #808080;">&#40;</span>Notes<span style="color: #808080;">&#41;</span> PERSISTED
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Now insert some test data.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p377code43'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37743"><td class="code" id="p377code43"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Insert some sample data into our table</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">ComputedColTable</span>
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	PersonCode,
	Notes
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Rhys'</span>,
	<span style="color: #FF0000;">'Campbell'</span>,
	<span style="color: #FF0000;">'C123456'</span>, <span style="color: #008080;">-- Correctly entered PersonCode</span>
	<span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'John'</span>,
	<span style="color: #FF0000;">'Smith'</span>,
	<span style="color: #808080;">NULL</span>, <span style="color: #008080;">-- No Person Code here</span>
	<span style="color: #FF0000;">'Person code = C654321 New Customer'</span> <span style="color: #008080;">-- PersonCode put in notes</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Joe'</span>,
	<span style="color: #FF0000;">'Bloggs'</span>,
	<span style="color: #808080;">NULL</span>, <span style="color: #008080;">-- No Person Code here</span>
	<span style="color: #FF0000;">'Visited London store C654320 customer code.'</span> <span style="color: #008080;">-- PersonCode put in notes</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Now lets view the data.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p377code44'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37744"><td class="code" id="p377code44"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Select data</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">ComputedColTable</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/computed_column_sql_server.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="computed_column_sql_server" border="0" alt="computed column sql server thumb Computed Columns in SQL Server" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/computed_column_sql_server_thumb.png" width="644" height="111" /></a> </p>
<p>Now we have cleaner data without any manual intervention needed.</p>
<p><map name='google_ad_map_377_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/377?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_377_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=377&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcomputed-columns-in-sql-server%2F377' title="Computed Columns in SQL Server" alt=" Computed Columns in SQL Server" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377">Computed Columns in SQL Server</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL Server Fulltext Search Primer</title>
		<link>http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248</link>
		<comments>http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248#comments</comments>
		<pubDate>Thu, 09 Jul 2009 16:10:50 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Fulltext]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248</guid>
		<description><![CDATA[Previously I wrote an article about fulltext searching with MySQL and thought I’d redo the same article but for SQL Server users. Depending of which side of the database wars you’re on SQL Server has either a more advanced or complicated way of doing things. Here’s a very brief introduction to the fulltext search features [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248">SQL Server Fulltext Search Primer</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Previously I wrote an article about <a href="http://www.youdidwhatwithtsql.com/mysql-fulltext-search-primer/224" target="_blank">fulltext searching with MySQL</a> and thought I’d redo the same article but for <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> users. Depending of which side of the <a href="http://www.sqlmag.com/Article/ArticleID/21431/sql_server_21431.html" target="_blank">database wars</a> you’re on <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> has either a more advanced or complicated way of doing things. Here’s a very brief introduction to the fulltext search features in <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx">SQL Server</a>.</p>
<p>First the fulltext search service needs to be enabled for the database.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p248code51'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24851"><td class="code" id="p248code51"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Enable fulltext searching on the database</span>
<span style="color: #0000FF;">USE</span> database1;
GO
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">sp_fulltext_database</span> <span style="color: #FF0000;">'enable'</span>;
GO
<span style="color: #008080;">-- Create a fulltext catalog</span>
<span style="color: #0000FF;">CREATE</span> FULLTEXT <span style="color: #0000FF;">CATALOG</span> ft_catalog_database1
GO</pre></td></tr></table></div>

<p>If your <a href="http://msdn.microsoft.com/en-us/library/ms187317.aspx">fulltext index</a> is going to be large, or very frequently searched, you may want to <a href="http://msdn.microsoft.com/en-us/library/ms187317.aspx" target="_blank">research options</a> to put the catalog on a different disk.</p>
<p>Create a table to hold some test data.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p248code52'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24852"><td class="code" id="p248code52"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Create a table to contain the poems</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> poems
<span style="color: #808080;">&#40;</span>
	id <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span>,
	author <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	title <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	poem <span style="color: #0000FF;">TEXT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Insert some dummy data.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p248code53'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24853"><td class="code" id="p248code53"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Insert some data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> poems
<span style="color: #808080;">&#40;</span>
	author,
	title,
	poem
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span>
&nbsp;
	<span style="color: #FF0000;">'Johann Wolfgang von Goethe'</span>,
	<span style="color: #FF0000;">'Night Thoughts'</span>,
	<span style="color: #FF0000;">'Stars, you are unfortunate, I pity you,
Beautiful as you are, shining in your glory,
Who guide seafaring men through stress and peril
And have no recompense from gods or mortals,
Love you do not, nor do you know what love is.
Hours that are aeons urgently conducting
Your figures in a dance through the vast heaven,
What journey have you ended in this moment,
Since lingering in the arms of my beloved
I lost all memory of you and midnight.'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span>
	<span style="color: #FF0000;">'Nikki Giovanni'</span>,
	<span style="color: #FF0000;">'I Love You'</span>,
	<span style="color: #FF0000;">'I love you
because the Earth turns round the sun
because the North wind blows north
sometimes
because the Pope is Catholic
and most Rabbis Jewish
because winters flow into springs
and the air clears after a storm'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span>
	<span style="color: #FF0000;">'Lord Byron'</span>,
	<span style="color: #FF0000;">'She Walks In Beauty'</span>,
	<span style="color: #FF0000;">'She walks in beauty, like the night
Of cloudless climes and starry skies;
And all that'</span><span style="color: #FF0000;">'s best of dark and bright
Meet in her aspect and her eyes:
Thus mellow'</span><span style="color: #FF0000;">'d to that tender light
Which heaven to gaudy day denies.
&nbsp;
One shade more, one ray less,
Had half impair'</span><span style="color: #FF0000;">'d the nameless grace
Which waves in every raven tress,
Or softly lightens o'</span><span style="color: #FF0000;">'er her face;
Where thoughts serenely sweet express
How pure, how dear their dwelling place.
&nbsp;
And on that cheek, and o'</span><span style="color: #FF0000;">'er that brow
So soft, so calm, yet eloquent,
The smiles that win, the tints that glow,
But tell of days in goodness spent,
A mind at peace with all below,
A heart whose love is innocent!'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span>
	<span style="color: #FF0000;">'Christopher Marlowe'</span>,
	<span style="color: #FF0000;">'Come Live With Me'</span>,
	<span style="color: #FF0000;">'Come live with me, and be my love;
And we will all the pleasures prove
That valleys, groves, hills, and fields,
Woods or steepy mountain yields.'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span>
	<span style="color: #FF0000;">'Thomas Campbell'</span>,
	<span style="color: #FF0000;">'Freedom and Love'</span>,
	<span style="color: #FF0000;">'How delicious is the winning
Of a kiss at love'</span><span style="color: #FF0000;">'s beginning,
When two mutual hearts are sighing
For the knot there'</span><span style="color: #FF0000;">'s no untying!
&nbsp;
Yet remember, '</span><span style="color: #FF0000;">'midst your wooing
Love has bliss, but Love has ruing;
Other smiles may make you fickle,
Tears for other charms may trickle.
&nbsp;
Love he comes and Love he tarries
Just as fate or fancy carries;
Longest stays, when sorest chidden;
Laughs and flies, when press'</span><span style="color: #FF0000;">'d and bidden.
&nbsp;
Bind the sea to slumber stilly,
Bind its odour to the lily,
Bind the aspen ne'</span><span style="color: #FF0000;">'er to quiver,
Then bind Love to last for ever.
&nbsp;
Love'</span><span style="color: #FF0000;">'s a fire that needs renewal
Of fresh beauty for its fuel;
Love'</span><span style="color: #FF0000;">'s wing moults when caged and captured,
Only free, he soars enraptured.
&nbsp;
Can you keep the bees from ranging,
Or the ringdove'</span><span style="color: #FF0000;">'s neck from changing?
No! nor fetter'</span><span style="color: #FF0000;">'d Love from dying
In the knot there'</span><span style="color: #FF0000;">'s no untying.'</span>;</pre></td></tr></table></div>

<p>Now we need to create the <a href="http://msdn.microsoft.com/en-us/library/ms187317.aspx">fulltext</a> index;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p248code54'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24854"><td class="code" id="p248code54"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Create a fulltext index on title and poem</span>
<span style="color: #0000FF;">CREATE</span> FULLTEXT <span style="color: #0000FF;">INDEX</span> <span style="color: #0000FF;">ON</span> database1.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">poems</span>
<span style="color: #808080;">&#40;</span>
	title
	<span style="color: #0000FF;">Language</span> 0X0,
	poem
	<span style="color: #0000FF;">Language</span> 0X0
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">INDEX</span> PK__poems__00551192 <span style="color: #0000FF;">ON</span> ft_catalog_database1
<span style="color: #0000FF;">WITH</span> CHANGE_TRACK<span style="color: #808080;">IN</span>G AUTO;</pre></td></tr></table></div>

<p>The fulltext index requires a unique non-nullable index to be specified on its creation. Check your primary key name in SSMS and change the above <a href="http://msdn.microsoft.com/en-us/library/ms189826(SQL.90).aspx">TSQL</a> as appropriate (after KEY INDEX). </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image10.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Check Primary Key name in SSMS" border="0" alt="image thumb10 SQL Server Fulltext Search Primer" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb10.png" width="244" height="225" /></a> </p>
<p>You will receive the following warning after the index is created;</p>
<pre>
Warning: Table or indexed view 'Documents' has full-text indexed columns that are of type image, text, or ntext. Full-text change tracking cannot track WRITETEXT or UPDATETEXT operations performed on these columns.
</pre>
<p>Standard UPDATE and INSERT changes are reflected in the fulltext catalog but changes with <a href="http://msdn.microsoft.com/en-us/library/ms186838.aspx">WRITETEXT</a> or <a href="http://msdn.microsoft.com/en-us/library/ms189466.aspx">UPDATETEXT</a> are not. If you’re not using these then you can ignore the warning. Otherwise this would involve some form of manual <a href="http://msdn.microsoft.com/en-us/library/aa214782(SQL.80).aspx">update to the fulltext catalog</a> after using <a href="http://msdn.microsoft.com/en-us/library/ms186838.aspx">WRITETEXT</a> / <a href="http://msdn.microsoft.com/en-us/library/ms189466.aspx">UPDATETEXT</a>. Now the fulltext index is ready to use.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p248code55'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24855"><td class="code" id="p248code55"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> 
<span style="color: #0000FF;">FROM</span> poems
<span style="color: #0000FF;">WHERE</span> <span style="color: #0000FF;">CONTAINS</span><span style="color: #808080;">&#40;</span>title, <span style="color: #FF0000;">'&quot;I love you&quot;'</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image11.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Fulltext search results in SSMS" border="0" alt="image thumb11 SQL Server Fulltext Search Primer" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb11.png" width="644" height="106" /></a></p>
<p>You can also order by relevance;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p248code56'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24856"><td class="code" id="p248code56"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #0000FF;">CONTAINSTABLE</span><span style="color: #808080;">&#40;</span>dbo.<span style="color: #202020;">poems</span>, <span style="color: #808080;">*</span>, <span style="color: #FF0000;">'&quot;I love you&quot;'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> t1
<span style="color: #808080;">JOIN</span> dbo.<span style="color: #202020;">poems</span> <span style="color: #0000FF;">AS</span> t2
	<span style="color: #0000FF;">ON</span> t1.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">KEY</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> t2.<span style="color: #202020;">id</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> t1.<span style="color: #808080;">&#91;</span>RANK<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">DESC</span>;</pre></td></tr></table></div>

<p>Note the asterisk in the <a href="http://technet.microsoft.com/en-us/library/ms189760.aspx">CONTAINSTABLE</a> clause. This means search all columns in the fulltext index. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image12.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Fulltext search results with rank in SSMS" border="0" alt="image thumb12 SQL Server Fulltext Search Primer" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb12.png" width="644" height="138" /></a></p>
<p>TSQL has two predicates and two functions we can use with fulltext searching. The table* below details these. Be sure to investigate these to make the most of your fulltext catalogs.</p>
<table border="1" cellspacing="0" cellpadding="5">
<tbody>
<tr>
<td><b>Keyword</b></td>
<td><b>Meaning</b></td>
<td><b>Returns</b></td>
</tr>
<tr>
<td>CONTAINS</td>
<td>Supports complex syntax to search for a word or phrase using Boolean operators, prefix terms</td>
<td>Simple Boolean predicate</td>
</tr>
<tr>
<td>CONTAINSTABLE</td>
<td>Supports CONTAINS syntax and returns document IDs and rank scores for matches</td>
<td>Table containing document IDs and rank scores</td>
</tr>
<tr>
<td>FREETEXT</td>
<td>Automatically performs thesaurus expansions and replacements in a simplified syntax</td>
<td>Simple Boolean predicate</td>
</tr>
<tr>
<td>FREETEXTTABLE</td>
<td>Supports FREETEXT syntax and returns document IDs and rank scores for matches</td>
<td>Table containing document IDs and rank scores</td>
</tr>
</tbody>
</table>
<p>* <em>source: <a title="http://en.wikipedia.org/wiki/SQL_Server_Full_Text_Search" href="http://en.wikipedia.org/wiki/SQL_Server_Full_Text_Search">http://en.wikipedia.org/wiki/SQL_Server_Full_Text_Search</a></em></p>
<p>I’ve just scratched the surface, of what you can do with fulltext searches in <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx">SQL Server</a>. Be sure to check out the concepts around <a href="http://msdn.microsoft.com/en-us/library/ms142551(SQL.90).aspx">Noise words</a>, usage of the <a href="http://msdn.microsoft.com/en-us/library/ms142491(SQL.90).aspx">Thesaurus</a> and <a href="http://msdn.microsoft.com/en-us/library/ms142509(SQL.90).aspx">word-breakers</a> to make the most of your fulltext searches.</p>
<p><map name='google_ad_map_248_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/248?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_248_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=248&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fsql-server-fulltext-search-primer%2F248' title="SQL Server Fulltext Search Primer" alt=" SQL Server Fulltext Search Primer" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248">SQL Server Fulltext Search Primer</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Documenting Databases</title>
		<link>http://www.youdidwhatwithtsql.com/documenting-databases/204</link>
		<comments>http://www.youdidwhatwithtsql.com/documenting-databases/204#comments</comments>
		<pubDate>Tue, 23 Jun 2009 19:05:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Database documentation]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/documenting-databases/204</guid>
		<description><![CDATA[Asking for database documentation in many tech shops will result in blank stares. Other places do see the value of but it forever remains on the to-do list. There are a few commercial products available hoping to help with this; SchemaToDoc &#8211; http://www.schematodoc.com/ SqlSpec &#8211; http://www.elsasoft.org/ SQL Doc &#8211; http://www.red-gate.com/products/SQL_Doc/index.htm I’m not convinced of their [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/documenting-databases/204">Documenting Databases</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Asking for database documentation in many tech shops will result in blank stares. Other places do see the value of but it forever remains on the to-do list. There are a few commercial products available hoping to help with this;</p>
<p>SchemaToDoc &#8211; <a title="http://www.schematodoc.com/" href="http://www.schematodoc.com/">http://www.schematodoc.com/</a>     <br />SqlSpec &#8211; <a title="http://www.elsasoft.org/" href="http://www.elsasoft.org/">http://www.elsasoft.org/</a>     <br />SQL Doc &#8211; <a title="http://www.red-gate.com/products/SQL_Doc/index.htm" href="http://www.red-gate.com/products/SQL_Doc/index.htm">http://www.red-gate.com/products/SQL_Doc/index.htm</a></p>
<p>I’m not convinced of their value especially when important object metadata, or business information,&#160; is missing from your database. </p>
<p>All databases objects should be tagged if appropriate with need-to-know information; Tables should be tagged with some basic information, if that data is licensed and needs annual renewal why not document this inside the database itself? </p>
<p>Columns should have a brief description; it may be obvious to you what the column holds but is it to everyone else? One system I worked with had a <a href="http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html" target="_blank">TINYINT</a> column called phone_permission with values of 0 or 1. My first guess was that ‘1’ meant you could call the telephone number on the record. Luckily I didn&#8217;t run the telesales department so no problem was caused from this misunderstanding.</p>
<p><strong>Adding comments to database tables</strong></p>
<p><font color="#666666">In <a href="http://www.mysql.com" target="_blank">MySQL</a> we can add a comment to a table like so;</font></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code64'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20464"><td class="code" id="p204code64"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">ALTER</span> <span style="color: #990099; font-weight: bold;">TABLE</span> TestTable <span style="color: #990099; font-weight: bold;">COMMENT</span> <span style="color: #008000;">'Data supplied by ACME Corp.'</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Table comments can then be viewed by using the <a href="http://dev.mysql.com/doc/refman/5.0/en/show-create-table.html" target="_blank">SHOW CREATE TABLE</a> syntax.</p>
<pre>mysql&gt; SHOW CREATE TABLE TestTable \G
*************************** 1. row ***************************
       Table: TestTable
Create Table: CREATE TABLE `TestTable` (
  `idnm` int(11) default NULL,
  `fullName` varchar(255) default NULL,
  `old_id` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='The data in this table is supplied by ACME Corp.'
1 row in set (0.00 sec)

mysql&gt;</pre>
<p>Or by querying <a href="http://dev.mysql.com/doc/refman/5.0/en/information-schema.html" target="_blank">INFORMATION_SCHEMA</a>;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code65'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20465"><td class="code" id="p204code65"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- View table comments for the current database</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> TABLE_NAME<span style="color: #000033;">,</span> TABLE_COMMENT
<span style="color: #990099; font-weight: bold;">FROM</span> information_schema.<span style="color: #990099; font-weight: bold;">tables</span>
<span style="color: #990099; font-weight: bold;">WHERE</span> TABLE_SCHEMA <span style="color: #CC0099;">=</span> <span style="color: #000099;">DATABASE</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>For <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> it’s a little more complicated. We have to add an <a href="http://msdn.microsoft.com/en-us/library/ms190243.aspx" target="_blank">extended property</a> to attach a comment onto the table.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code66'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20466"><td class="code" id="p204code66"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> sys.<span style="color: #AF0000;">sp_addextendedproperty</span> @name<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'Description'</span>, 
				@<span style="color: #0000FF;">value</span><span style="color: #808080;">=</span>N<span style="color: #FF0000;">'hello table Customer'</span>,
				@level0type<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'SCHEMA'</span>, 
				@level0name<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'dbo'</span>, 
				@level1type<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'TABLE'</span>, 
				@level1name<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'Customer'</span>;</pre></td></tr></table></div>

<p>Viewing this comment isn’t as easy as <a href="http://www.mysql.com" target="_blank">MySQL</a> either. Use the below <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> to view all table level comments in the current database (dbo schema);</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code67'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20467"><td class="code" id="p204code67"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Table comments</span>
<span style="color: #0000FF;">SELECT</span> objtype, objname, name, <span style="color: #0000FF;">value</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #AF0000;">fn_listextendedproperty</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #FF0000;">'schema'</span>, 
	<span style="color: #FF0000;">'dbo'</span>, 
	<span style="color: #FF0000;">'table'</span>, 
	<span style="color: #0000FF;">default</span>, 
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
GO</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image18.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Viewing Table comments in SSMS" border="0" alt="image thumb18 Documenting Databases" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb18.png" width="644" height="134" /></a> </p>
<p>Obviously <a href="http://www.microsoft.com/en/us/default.aspx" target="_blank">Microsoft</a> didn’t intend people to hand-code <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> to add comments to their databases, but rather use tool support like <a href="http://msdn.microsoft.com/en-us/library/ms174173.aspx" target="_blank">SSMS</a>. This would probably be rather tedious if you needed to comment a large number of tables. Here’s a suggested method for making this task a little easier. First get the schema and name for all tables in the database you wish to document.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code68'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20468"><td class="code" id="p204code68"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> TABLE_SCHEMA, TABLE_NAME
<span style="color: #0000FF;">FROM</span> <span style="color: #808080;">IN</span>F<span style="color: #808080;">OR</span>MATION_SCHEMA.<span style="color: #202020;">TABLES</span>
<span style="color: #0000FF;">WHERE</span> TABLE_TYPE <span style="color: #808080;">=</span> <span style="color: #FF0000;">'BASE TABLE'</span>;</pre></td></tr></table></div>

<p>Copy and paste this into Excel, or other <a href="http://www.openoffice.org/product/calc.html" target="_blank">spreadsheet program</a>, so you can comment each table easily.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image19.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Comment your tables in Excel" border="0" alt="image thumb19 Documenting Databases" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb19.png" width="644" height="147" /></a> </p>
<p>When you are finished import this data into <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> and run this <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> statement against it. N.B. This statement assumes you imported the table comment data into a table called <strong>temp_table_comments</strong>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code69'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20469"><td class="code" id="p204code69"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Generate T_SQL to add extended properties to all tables</span>
<span style="color: #008080;">-- in temp_table_comments</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'EXEC sys.sp_addextendedproperty @name=N'</span><span style="color: #FF0000;">'TABLE_COMMENT'</span><span style="color: #FF0000;">', 
					@value=N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> COMMENT <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">',
					@level0type=N'</span><span style="color: #FF0000;">'SCHEMA'</span><span style="color: #FF0000;">', 
					@level0name=N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> TABLE_SCHEMA <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">',
					@level1type=N'</span><span style="color: #FF0000;">'TABLE'</span><span style="color: #FF0000;">', 
					@level1name=N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> TABLE_NAME <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">';'</span>
<span style="color: #0000FF;">FROM</span> temp_table_comments;</pre></td></tr></table></div>

<p>Here’s the <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> it generates if run against the <a href="http://www.microsoft.com/downloads/details.aspx?familyid=e719ecf7-9f46-4312-af89-6ad8702e4e6e" target="_blank">AdventureWorks</a> database table names. Once you have generated the <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> then you’re ready to run it against your database and apply your comments to the tables.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image20.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Running the script against the AdventureWorks database" border="0" alt="image thumb20 Documenting Databases" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb20.png" width="644" height="130" /></a></p>
<p>View the table comments added for the Sales schema.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code70'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20470"><td class="code" id="p204code70"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Table comments for AdventureWorks</span>
<span style="color: #008080;">-- Sales schema</span>
<span style="color: #0000FF;">SELECT</span> objtype, objname, name, <span style="color: #0000FF;">value</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #AF0000;">fn_listextendedproperty</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #FF0000;">'schema'</span>, 
	<span style="color: #FF0000;">'Sales'</span>, 
	<span style="color: #FF0000;">'table'</span>, 
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TABLE_COMMENT'</span>
GO</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image21.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Table comments in the Sales schema" border="0" alt="image thumb21 Documenting Databases" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb21.png" width="644" height="202" /></a></p>
<p>It would be fairly easy to take a similar approach for commenting your table columns too. Once your database is nicely documented it’ll be easy to knock up a couple of reports in <a href="http://msdn.microsoft.com/en-us/library/ms159106.aspx" target="_blank">SSRS</a> and surprise the next person who asks for “<em>the documentation”.</em></p>
<p><map name='google_ad_map_204_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/204?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_204_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=204&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fdocumenting-databases%2F204' title="Documenting Databases" alt=" Documenting Databases" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/documenting-databases/204">Documenting Databases</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/documenting-databases/204" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/documenting-databases/204/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cannot resolve the collation conflict</title>
		<link>http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176</link>
		<comments>http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176#comments</comments>
		<pubDate>Fri, 12 Jun 2009 17:57:53 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[collations]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176</guid>
		<description><![CDATA[I do a fair bit of work with Linked Servers and cross-database queries&#160; and sometimes come across the following error when joining between databases with different collations; Msg 468, Level 16, State 9, Line 1 Cannot resolve the collation conflict between 'Latin1_General_CI_AS' and 'SQL_Latin1_General_Pref_CP850_CI_AS'; in the equal to operation. To replicate this error run the [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176">Cannot resolve the collation conflict</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I do a fair bit of work with <a href="http://msdn.microsoft.com/en-us/library/ms188279.aspx" target="_blank">Linked Servers</a> and <a href="http://www.sqlservercentral.com/articles/Advanced/designingcrossdatabasequeries/1753/" target="_blank">cross-database queries</a>&#160; and sometimes come across the following error when joining between databases with different <a href="http://msdn.microsoft.com/en-us/library/aa214408(SQL.80).aspx" target="_blank">collations</a>;</p>
<pre>Msg 468, Level 16, State 9, Line 1

Cannot resolve the collation conflict between 'Latin1_General_CI_AS' and 'SQL_Latin1_General_Pref_CP850_CI_AS'; in the equal to operation.</pre>
<p>To replicate this error run the below <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">TSQL</a> to create two databases with tables and data.&#160;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p176code75'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17675"><td class="code" id="p176code75"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Create first database</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">DATABASE</span> database1 <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AS;
GO
<span style="color: #008080;">-- Create second database</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">DATABASE</span> database2 <span style="color: #0000FF;">COLLATE</span> SQL_Latin1_General_Pref_CP850_CI_AS;
GO
&nbsp;
<span style="color: #0000FF;">USE</span> database1;
GO
<span style="color: #008080;">-- Create Customer table in database1</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> Customer
<span style="color: #808080;">&#40;</span>
	CustID <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	FirstName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	LastName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	DOB <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	StartDate <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">DEFAULT</span> <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>; 
GO
<span style="color: #0000FF;">USE</span> database2;
GO
<span style="color: #008080;">-- Create Customer table in database2</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> Customer
<span style="color: #808080;">&#40;</span>
	CustID <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	FirstName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	LastName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	DOB <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	StartDate <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">DEFAULT</span> <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>; 
GO
&nbsp;
<span style="color: #0000FF;">USE</span> database1;
GO
<span style="color: #008080;">-- Insert test data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Customer</span>
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	DOB
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Joe'</span>, <span style="color: #FF0000;">'Bloggs'</span>, <span style="color: #FF0000;">'1975-01-01 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span><span style="color: #FF0000;">'Dave'</span>, <span style="color: #FF0000;">'Smith'</span>, <span style="color: #FF0000;">'1977-10-11 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span><span style="color: #FF0000;">'Fred'</span>, <span style="color: #FF0000;">'Bloggs'</span>, <span style="color: #FF0000;">'1965-11-28 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span><span style="color: #FF0000;">'Sue'</span>, <span style="color: #FF0000;">'Smith'</span>, <span style="color: #FF0000;">'1974-06-17 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Steve'</span>, <span style="color: #FF0000;">'Smith'</span>, <span style="color: #FF0000;">'1981-07-07 00:00:00'</span>;
GO
<span style="color: #0000FF;">USE</span> database2;
GO
<span style="color: #008080;">-- Insert test data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Customer</span>
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	DOB
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Joe'</span>, <span style="color: #FF0000;">'Bloggs'</span>, <span style="color: #FF0000;">'1975-01-01 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span><span style="color: #FF0000;">'Dave'</span>, <span style="color: #FF0000;">'Smith'</span>, <span style="color: #FF0000;">'1977-10-11 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span><span style="color: #FF0000;">'Fred'</span>, <span style="color: #FF0000;">'Bloggs'</span>, <span style="color: #FF0000;">'1965-11-28 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span><span style="color: #FF0000;">'Sue'</span>, <span style="color: #FF0000;">'Smith'</span>, <span style="color: #FF0000;">'1974-06-17 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Steve'</span>, <span style="color: #FF0000;">'Smith'</span>, <span style="color: #FF0000;">'1981-07-07 00:00:00'</span>;
GO</pre></td></tr></table></div>

<p>Run the following query to observe the collation conflict.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p176code76'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17676"><td class="code" id="p176code76"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Cross-database query causing the collation conflict</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Customer</span> c1
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> database1.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">Customer</span> <span style="color: #0000FF;">AS</span> c2
	<span style="color: #0000FF;">ON</span> c1.<span style="color: #202020;">FirstName</span> <span style="color: #808080;">=</span> c2.<span style="color: #202020;">FirstName</span>
<span style="color: #808080;">AND</span> c1.<span style="color: #202020;">LastName</span> <span style="color: #808080;">=</span> c2.<span style="color: #202020;">LastName</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image11.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="SQL Server collation conflict" border="0" alt="image thumb11 Cannot resolve the collation conflict" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb11.png" width="644" height="72" /></a> </p>
<p>You could fix this by changing the collation in one of the databases, i.e.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p176code77'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17677"><td class="code" id="p176code77"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Set database2 to the same collation as database1</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">DATABASE</span> database2 <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AS;
<span style="color: #008080;">-- Change VARCHAR columns on our existing tables</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> Customer <span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">COLUMN</span> FirstName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AS;
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> Customer <span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">COLUMN</span> LastName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AS;</pre></td></tr></table></div>

<p>Sometimes you may not want, or be able, to change a database in this way. In these situations you can add <a href="http://msdn.microsoft.com/en-us/library/aa258237(SQL.80).aspx" target="_blank">COLLATE</a> DATABASE_DEFAULT to the JOINS or expressions in your query.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p176code78'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17678"><td class="code" id="p176code78"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Using COLLATE DATABASE_DEFAULT</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Customer</span> c1
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> database1.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">Customer</span> <span style="color: #0000FF;">AS</span> c2
	<span style="color: #0000FF;">ON</span> c1.<span style="color: #202020;">FirstName</span> <span style="color: #808080;">=</span> c2.<span style="color: #202020;">FirstName</span> <span style="color: #0000FF;">COLLATE</span> DATABASE_DEFAULT
<span style="color: #808080;">AND</span> c1.<span style="color: #202020;">LastName</span> <span style="color: #808080;">=</span> c2.<span style="color: #202020;">LastName</span> <span style="color: #0000FF;">COLLATE</span> DATABASE_DEFAULT;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image12.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Query using COLLATE DATABASE_DEFAULT" border="0" alt="image thumb12 Cannot resolve the collation conflict" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb12.png" width="644" height="102" /></a></p>
<p><map name='google_ad_map_176_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/176?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_176_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=176&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcannot-resolve-the-collation-conflict%2F176' title="Cannot resolve the collation conflict" alt=" Cannot resolve the collation conflict" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176">Cannot resolve the collation conflict</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Executing MySQL Stored Procedures from SQL Server</title>
		<link>http://www.youdidwhatwithtsql.com/executing-mysql-stored-procedures-from-sql-server/170</link>
		<comments>http://www.youdidwhatwithtsql.com/executing-mysql-stored-procedures-from-sql-server/170#comments</comments>
		<pubDate>Mon, 08 Jun 2009 19:01:25 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Database Integration]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/executing-mysql-stored-procedures-from-sql-server/170</guid>
		<description><![CDATA[If you ever need to call a MySQL procedure from SQL Server it’s fairly simple thanks to ODBC and Linked Servers. This will allow you to reuse any logic already invested in MySQL Stored Procedures saving you from rewriting them. Here&#8217;s a simple example on how you can do it; Create the following procedure in [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/executing-mysql-stored-procedures-from-sql-server/170">Executing MySQL Stored Procedures from SQL Server</a></p>
]]></description>
			<content:encoded><![CDATA[<p>If you ever need to call a <a href="http://www.mysql.com" target="_blank">MySQL</a> procedure from <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> it’s fairly simple thanks to <a href="http://en.wikipedia.org/wiki/Open_Database_Connectivity" target="_blank">ODBC</a> and <a href="http://msdn.microsoft.com/en-us/library/ms188279.aspx" target="_blank">Linked Servers</a>. This will allow you to reuse any logic already invested in <a href="http://dev.mysql.com/doc/refman/5.0/en/stored-routines.html" target="_blank">MySQL Stored Procedures</a> saving you from rewriting them. Here&#8217;s a simple example on how you can do it;</p>
<ul>
<li>Create the following procedure in your MySQL ‘test’ database. </li>
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p170code82'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17082"><td class="code" id="p170code82"><pre class="mysql" style="font-family:monospace;">DELIMITER $$
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> <span style="color: #008000;">`test`</span>.<span style="color: #008000;">`usp<span style="color: #008080; font-weight: bold;">_</span>test`</span>$$
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #008000;">`test`</span>.<span style="color: #008000;">`usp<span style="color: #008080; font-weight: bold;">_</span>test`</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span>    
	<span style="color: #990099; font-weight: bold;">LANGUAGE SQL</span>
	<span style="color: #990099; font-weight: bold;">DETERMINISTIC</span>
	<span style="color: #990099; font-weight: bold;">SQL SECURITY</span> <span style="color: #990099; font-weight: bold;">INVOKER</span>
	<span style="color: #990099; font-weight: bold;">COMMENT</span> <span style="color: #008000;">'Test Procedure returns the numbers 1-5'</span>
<span style="color: #990099; font-weight: bold;">BEGIN</span>
	<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008080;">1</span>
	<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
	<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008080;">2</span>
	<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
	<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008080;">3</span>
	<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
	<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008080;">4</span>
	<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
	<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008080;">5</span><span style="color: #000033;">;</span>
<span style="color: #009900;">END</span>$$
DELIMITER <span style="color: #000033;">;</span></pre></td></tr></table></div>

<ul>
<li>Configure your <a href="http://www.mysql.com" target="_blank">MySQL</a> Server as a <a href="http://msdn.microsoft.com/en-us/library/ms188279.aspx" target="_blank">Linked Server</a> in <a href="http://msdn.microsoft.com/en-us/library/ms174173.aspx" target="_blank">SSMS</a>. Plenty of guides on the net about this so I won’t repeat it here. Here’s  a <a href="http://www.ideaexcursion.com/2009/02/25/howto-setup-sql-server-linked-server-to-mysql/" target="_blank">good one</a>. </li>
</ul>
<p>If you get the following message&#160;&#160; </p>
<pre>Msg 7411, Level 16, State 1, Line 1
Server 'MYSQL' is not configured for RPC.</pre>
<p>You need to change the <a href="http://msdn.microsoft.com/en-us/library/ms188279.aspx" target="_blank">Linked Server</a> property &quot;RPC Out&quot; to true.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image9.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Linked Server Properties - Enable RPC Out" border="0" alt="image thumb9 Executing MySQL Stored Procedures from SQL Server" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb9.png" width="539" height="484" /></a></p>
<ul>
<li>Once your <a href="http://msdn.microsoft.com/en-us/library/ms188279.aspx" target="_blank">Linked Server</a> has been configured correctly you’re ready to go. Execute the following <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> in <a href="http://msdn.microsoft.com/en-us/library/ms188279.aspx" target="_blank">SSMS</a>. </li>
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p170code83'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17083"><td class="code" id="p170code83"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'CALL test.usp_test()'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AT</span> MYSQL;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image10.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Executing MySQL Stored Procedures from SQL Server" border="0" alt="image thumb10 Executing MySQL Stored Procedures from SQL Server" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb10.png" width="169" height="160" /></a> </p>
<p>In fact the <a href="http://msdn.microsoft.com/en-us/library/aa175921(SQL.80).aspx" target="_blank">EXEC</a> can be used to run any <a href="http://www.mysql.com" target="_blank">MySQL</a> specific command. All the following will work;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p170code84'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17084"><td class="code" id="p170code84"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'SHOW TABLES'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AT</span> MySQL; <span style="color: #008080;">-- Show the tables in the current database</span>
<span style="color: #0000FF;">EXEC</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'SHOW SLAVE STATUS'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AT</span> MySQL; <span style="color: #008080;">-- Show Slave status info (if applicable)</span>
<span style="color: #0000FF;">EXEC</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'SHOW DATABASES'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AT</span> MySQL; <span style="color: #008080;">-- Show the accessible databases</span>
<span style="color: #0000FF;">EXEC</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'SHOW CREATE TABLE mysql.user'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AT</span> MySQL; <span style="color: #008080;">-- Show the SQL used to create mysql.users</span></pre></td></tr></table></div>

<p><a href="http://www.mysql.com" target="_blank">MySQL</a> <a href="http://msdn.microsoft.com/en-us/library/ms188279.aspx" target="_blank">Linked Servers</a> work pretty solidly with <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> and allow complex integrations between systems to happen reasonably easily. So if your needs are simple there’s no need to resort to an additional layer like <a href="http://msdn.microsoft.com/en-us/library/ms141026.aspx" target="_blank">SSIS</a> if it’s going to complicate your environment.</p>
<p><map name='google_ad_map_170_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/170?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_170_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=170&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fexecuting-mysql-stored-procedures-from-sql-server%2F170' title="Executing MySQL Stored Procedures from SQL Server" alt=" Executing MySQL Stored Procedures from SQL Server" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/executing-mysql-stored-procedures-from-sql-server/170">Executing MySQL Stored Procedures from SQL Server</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/executing-mysql-stored-procedures-from-sql-server/170" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/executing-mysql-stored-procedures-from-sql-server/170/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

