<?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</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Wed, 01 Sep 2010 17:02:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>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" 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" 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="duplicates organise into groups with DENSE_RANK" 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" 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>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parse MySQL Slow Logs with mysqlsla</title>
		<link>http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856</link>
		<comments>http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856#comments</comments>
		<pubDate>Tue, 10 Aug 2010 19:36:21 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856</guid>
		<description><![CDATA[Here&#8217;s a bash script that you can use to parse multiple MySQL Slow Query Log files, in one sweep, into something much more understandable. The script uses the handy utility mysqlsla so make sure this is in your path.&#160; mysqlsla parses, filters, analyzes and sorts MySQL slow, general, binary and microslow patched logs in order [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856">Parse MySQL Slow Logs with mysqlsla</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a bash script that you can use to parse multiple <a title="MySQL Slow Query Logs" href="http://dev.mysql.com/doc/refman/5.1/en/slow-query-log.html" target="_blank">MySQL Slow Query Log</a> files, in one sweep, into something much more understandable. The script uses the handy utility <a href="http://hackmysql.com/mysqlsla" target="_blank">mysqlsla</a> so make sure this is in your path.&nbsp;<br />
<blockquote>mysqlsla parses, filters, analyzes and sorts MySQL <a href="http://dev.mysql.com/doc/refman/5.0/en/slow-query-log.html">slow</a>, <a href="http://dev.mysql.com/doc/refman/5.0/en/query-log.html">general</a>, <a href="http://dev.mysql.com/doc/refman/5.0/en/binary-log.html">binary</a> and <a href="http://www.mysqlperformanceblog.com/2008/04/20/updated-msl-microslow-patch-installation-walk-through/">microslow patched</a> logs in order to create a customizable report of the queries and their meta-property values. Since these reports are customizable, they can be used for human consumption or be fed into other scripts to further analyze the queries. For example, to profile with <a href="http://maatkit.sourceforge.net/doc/mk-query-profiler.html">mk-query-profiler</a> (a script from Baron Schwartz&#8217;s <a href="http://www.maatkit.org/">Maatkit</a>) every unique SELECT statement using database foo from a slow log: <a title="mysqlsla" href="http://hackmysql.com/mysqlsla" target="_blank">source</a></p></blockquote>
<p>Place all your slow logs into a directory. Change the <strong>sl_dir</strong> variable to point at this directory.<strong> </strong>When you execute the script it will create a directory, within your slow logs directory, called reports. This will contain the reports produced by <a title="mysqlsla" href="http://hackmysql.com/mysqlsla" target="_blank">mysqlsla</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('p856code8'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8568"><td class="code" id="p856code8"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Script to process multiple mysql slow logs</span>
<span style="color: #666666; font-style: italic;"># using mysqlsla http://hackmysql.com/mysqlsla</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Directory containing slow logs</span>
<span style="color: #007800;">sl_dir</span>=<span style="color: #ff0000;">&quot;/home/rhys/Desktop/slow_logs&quot;</span>;
&nbsp;
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$sl_dir</span>&quot;</span>;
<span style="color: #666666; font-style: italic;">#slow_logs=$(ls &quot;$sl_dir&quot;);</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Folder for reports</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$sl_dir</span>&quot;</span><span style="color: #000000; font-weight: bold;">/</span>reports <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$sl_dir</span>&quot;</span><span style="color: #000000; font-weight: bold;">/</span>reports;
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># process each slow log file</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$sl_dir</span>&quot;</span><span style="color: #000000; font-weight: bold;">/*</span>
<span style="color: #000000; font-weight: bold;">do</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Processing file: <span style="color: #007800;">$file</span>&quot;</span>;
                <span style="color: #007800;">filename</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$file</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                mysqlsla <span style="color: #660033;">-lt</span> slow <span style="color: #ff0000;">&quot;<span style="color: #007800;">$file</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #ff0000;">&quot;reports/<span style="color: #007800;">$filename</span>.rpt&quot;</span>;
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Finished processing file: <span style="color: #007800;">$file</span>&quot;</span>;
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p>The reports produced are much easier to work with than the raw mysql logs so this should be a good time saver when optimising those queries!</p>
<p><map name='google_ad_map_856_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/856?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_856_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=856&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fparse-mysql-slow-logs-with-mysqlsla%2F856' title="Parse MySQL Slow Logs with mysqlsla" alt=" Parse MySQL Slow Logs with mysqlsla" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856">Parse MySQL Slow Logs with mysqlsla</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kill all processes by name with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/kill-all-processes-by-name-with-powershell/853</link>
		<comments>http://www.youdidwhatwithtsql.com/kill-all-processes-by-name-with-powershell/853#comments</comments>
		<pubDate>Tue, 10 Aug 2010 19:10:58 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[kill all processes]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/kill-all-processes-by-name-with-powershell/853</guid>
		<description><![CDATA[A reader commented on a previous post pointing out a deficiency in one of the scripts used to kill processes on remote computers. If more than one instance of the specified process was running on the target computer the script would buckle. This is pretty easy to rectify. The below script will kill all process [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/kill-all-processes-by-name-with-powershell/853">Kill all processes by name with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>A reader commented on a <a href="http://www.youdidwhatwithtsql.com/more-powershell-nuggets/239" target="_blank">previous post</a> pointing out a deficiency in one of the scripts used to kill processes on remote computers. If more than one instance of the specified process was running on the target computer the script would buckle. This is pretty easy to rectify. The below script will kill all process instances on the target machine.</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('p853code11'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p85311"><td class="code" id="p853code11"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Kill all processes on a remote machine with a specific name</span>
<span style="color: #800080;">$computer</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost&quot;</span>;
<span style="color: #800080;">$processToKill</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;notepad.exe&quot;</span>;
<span style="color: #800080;">$process</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-Class</span> Win32_Process <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;Name = '$processToKill'&quot;</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$computer</span>;
<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$process</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>              <span style="color: #008000;"># If null then the process may not be running</span>
                <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Red <span style="color: #800000;">&quot;Couldn't get process $processToKill on $computer&quot;</span>;
                <span style="color: #008080; font-weight: bold;">sleep</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">10</span><span style="color: #000000;">&#41;</span>;
                exit;
<span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">else</span>
<span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Attempting to Kill $processToKill on $computer&quot;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># This original part of the script dies if $process is an array of more than one calc.exe process</span>
<span style="color: #008000;"># Kill the process and get exit status 0 = OK</span>
<span style="color: #008000;"># $status = $process.InvokeMethod(&quot;Terminate&quot;, $null);</span>
<span style="color: #008000;"># switch($status)</span>
<span style="color: #008000;"># {</span>
<span style="color: #008000;">#              0 { Write-Host -ForegroundColor Green &quot;Killed $processToKill on $computer&quot;};</span>
<span style="color: #008000;">#              default { Write-Host -ForegroundColor Red &quot;Error, couldn't kill $processToKill on $computer&quot;};</span>
<span style="color: #008000;">#};</span>
&nbsp;
<span style="color: #800080;">$count</span> <span style="color: pink;">=</span> <span style="color: #804000;">1</span>;
&nbsp;
<span style="color: #008000;"># This will work regardless if $process is an array or not</span>
<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$ps</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$process</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Kill count = $count&quot;</span>;
				<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Handle = &quot;</span> <span style="color: #800080;">$ps</span>.Handle;
                <span style="color: #800080;">$status</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ps</span>.InvokeMethod<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Terminate&quot;</span><span style="color: pink;">,</span> <span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #0000FF;">switch</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$status</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                                <span style="color: #804000;">0</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;Killed $processToKill on $computer&quot;</span><span style="color: #000000;">&#125;</span>;
                                default <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Red <span style="color: #800000;">&quot;Error, couldn't kill $processToKill on $computer&quot;</span><span style="color: #000000;">&#125;</span>;
                <span style="color: #000000;">&#125;</span>;
                <span style="color: #800080;">$count</span><span style="color: pink;">++</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>This will procedure output similar to below.</p>
<pre>Attempting to Kill notepad.exe on localhost
Kill count = 1
Handle =  3788
Killed notepad.exe on localhost
Kill count = 2
Handle =  4916
Killed notepad.exe on localhost
Kill count = 3
Handle =  5884
Killed notepad.exe on localhost
Kill count = 4
Handle =  3488
Killed notepad.exe on localhost
Kill count = 5
Handle =  6232
Killed notepad.exe on localhost</pre>
<p>A similar thing can be achieved , on the localhost, with a one-liner.</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('p853code12'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p85312"><td class="code" id="p853code12"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Kills all processes called 'calc' on the localhost</span>
<span style="color: #008080; font-weight: bold;">ps</span> calc <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">kill</span>;</pre></td></tr></table></div>

<p>With a little bit of <a href="http://blogs.msdn.com/b/powershell/archive/2008/05/10/remoting-with-powershell-quickstart.aspx" target="_blank">Remoting</a>, available in Powershell V2, it would be simple enough to achieve the same functionality in the one-liner to execute this on remote computers.</p>
<p><map name='google_ad_map_853_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/853?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_853_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=853&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fkill-all-processes-by-name-with-powershell%2F853' title="Kill all processes by name with Powershell" alt=" Kill all processes by name with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/kill-all-processes-by-name-with-powershell/853">Kill all processes by name with Powershell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/kill-all-processes-by-name-with-powershell/853/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tweet-SQL Version 3 released</title>
		<link>http://www.youdidwhatwithtsql.com/tweet-sql-version-3-released/850</link>
		<comments>http://www.youdidwhatwithtsql.com/tweet-sql-version-3-released/850#comments</comments>
		<pubDate>Sat, 07 Aug 2010 19:23:51 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Tweet-SQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/tweet-sql-version-3-released/850</guid>
		<description><![CDATA[Tweet-SQL version 3 has been released! Here’s a quick summary of the new features. Full 64 bit OS Support. (Previously the installer would fail to created the required registry keys on 64 bit Operating Systems). Full support for oAuth authentication. Support for the new Twitter List methods. See sqlserverpedia list with Tweet-SQL for this in [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/tweet-sql-version-3-released/850">Tweet-SQL Version 3 released</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.tweet-sql.com" target="_blank">Tweet-SQL</a> version 3 has been released! Here’s a quick summary of the new features.</p>
<ul>
<li>Full 64 bit OS Support. (Previously the installer would fail to created the required registry keys on 64 bit Operating Systems).</li>
<li>Full support for <a href="http://www.youdidwhatwithtsql.com/configuring-oauth-for-tweet-sql/814" target="_blank">oAuth authentication</a>.</li>
<li>Support for the new Twitter List methods. See <a href="http://www.youdidwhatwithtsql.com/creating-a-sqlserverpedia-list-with-tweet-sql/835" target="_blank">sqlserverpedia list with Tweet-SQL</a> for this in action.</li>
<li>Support for geo-tagging of status updates.</li>
<li>Support for the Twitter reweet methods.</li>
<li>Support for the saved search API methods.</li>
<li>Support for the suggested users API methods.</li>
<li>Support for <a href="http://tinyurl.com" target="_blank">TinyURL</a>, <a href="http://tweetshrink.com/" target="_blank">TweetShrink</a> and <a href="http://twittercounter.com/" target="_blank">TwitterCounter</a>.</li>
</ul>
<p>I’ll be following up with a few cool blog posts showing what you can do with these new features. In the meantime checkout the <a href="http://www.tweet-sql.com/files/Tweet-SQL%20User%20Documentation.pdf" target="_blank">Tweet-SQL User Documentation</a> for more details and download <a href="http://www.tweet-sql.com/download.php" target="_blank">Tweet-SQL Version 3</a> now!</p>
<p><map name='google_ad_map_850_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/850?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_850_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=850&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ftweet-sql-version-3-released%2F850' title="Tweet SQL Version 3 released" alt=" Tweet SQL Version 3 released" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/tweet-sql-version-3-released/850">Tweet-SQL Version 3 released</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/tweet-sql-version-3-released/850/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comparing accented strings with TSQL</title>
		<link>http://www.youdidwhatwithtsql.com/comparing-accented-strings-with-tsql/849</link>
		<comments>http://www.youdidwhatwithtsql.com/comparing-accented-strings-with-tsql/849#comments</comments>
		<pubDate>Mon, 02 Aug 2010 20:38:47 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[collations]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/comparing-accented-strings-with-tsql/849</guid>
		<description><![CDATA[Today a colleague was running into some difficulties matching football team names containing accented characters. For example Olympique Alès and Olympique Ales were not matching when he wanted them to. The issue here is all to do with collations. We can use the Latin1_General_CI_AI collation in our queries to force the comparison to ignore accents [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/comparing-accented-strings-with-tsql/849">Comparing accented strings with TSQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Today a colleague was running into some difficulties matching football team names containing accented characters. For example <em>Olympique Alès </em>and <em>Olympique Ales</em> were not matching when he wanted them to. The issue here is all to do with <a href="http://msdn.microsoft.com/en-us/library/ms144260.aspx" target="_blank">collations</a>. We can use the <strong>Latin1_General_CI_AI</strong> collation in our queries to force the comparison to ignore accents (AI stands for <em>Accent Insensitive</em>).</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('p849code20'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84920"><td class="code" id="p849code20"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @string1 <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span>, @string2 <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">SET</span> @string1 <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Olympique Alès'</span>; <span style="color: #008080;">-- With accented e</span>
<span style="color: #0000FF;">SET</span> @string2 <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Olympique Ales'</span>; <span style="color: #008080;">-- Without accented e</span>
&nbsp;
<span style="color: #0000FF;">IF</span> @string1 <span style="color: #808080;">=</span> @string2
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'Strings do not match!'</span>; <span style="color: #008080;">-- This will not print</span>
<span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #0000FF;">IF</span> @string1 <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AI <span style="color: #808080;">=</span> @string2 <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AI
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'Strings match if we use COLLATE Latin1_General_CI_AI!'</span>; <span style="color: #008080;">-- This will print</span>
<span style="color: #0000FF;">END</span></pre></td></tr></table></div>

<p>Here’s how it works with GROUP BY.</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('p849code21'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84921"><td class="code" id="p849code21"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Places</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>,
	Place <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>
<span style="color: #808080;">&#41;</span>;
&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;">Places</span> 
<span style="color: #808080;">&#40;</span>
	Place
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Olympique Alès'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Olympique Ales'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Gazélec Ajaccio'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Gazelec Ajaccio'</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #0000FF;">SELECT</span> Place, <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Places</span>
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> Place;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/08/group_by_no_collate.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="group by no collate" border="0" alt="group by no collate" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/08/group_by_no_collate_thumb.png" width="240" height="126" /></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('p849code22'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84922"><td class="code" id="p849code22"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> Place <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AI, <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Places</span>
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> Place <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AI;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/08/group_by_with_collate.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="group by with collate" border="0" alt="group by with collate" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/08/group_by_with_collate_thumb.png" width="254" height="89" /></a> </p>
<p>You can also use this with JOINS.</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('p849code23'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84923"><td class="code" id="p849code23"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- No COLLATE</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Places</span> t1
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> dbo.<span style="color: #202020;">Places</span> t2
	<span style="color: #0000FF;">ON</span> t1.<span style="color: #202020;">Place</span> <span style="color: #808080;">=</span> t2.<span style="color: #202020;">Place</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/08/join_no_collate.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="join no collate" border="0" alt="join no collate" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/08/join_no_collate_thumb.png" width="275" height="137" /></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('p849code24'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84924"><td class="code" id="p849code24"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- With COLLATE</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Places</span> t1
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> dbo.<span style="color: #202020;">Places</span> t2
	<span style="color: #0000FF;">ON</span> t1.<span style="color: #202020;">Place</span> <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AI <span style="color: #808080;">=</span> t2.<span style="color: #202020;">Place</span> <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AI;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/08/join_with_collate.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="join with collate" border="0" alt="join with collate" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/08/join_with_collate_thumb.png" width="273" height="225" /></a> </p>
<p>So that’s how you can force strings to match if they contain accented characters. I was using this for a quick data mapping task but be wary of using the COLLATE clause, in any applications or processes, where performance may become an issue. </p>
<p>If you use the COLLATE clause then this will mean the database engine cannot use any index on the referenced column. You could replace the accented characters before inserting them into your database but here’s a solution I prefer using persisted <a href="http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377" target="_blank">computed columns</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('p849code25'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84925"><td class="code" id="p849code25"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Alter Places table. Note we specify the collation</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Places</span> <span style="color: #0000FF;">ADD</span> CleanPlaceName <span style="color: #0000FF;">AS</span> Place <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AI PERSISTED;
<span style="color: #008080;">-- Index this column!</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">INDEX</span> idx_CleanPlaceName <span style="color: #0000FF;">ON</span> dbo.<span style="color: #202020;">Places</span> <span style="color: #808080;">&#40;</span>CleanPlaceName<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">-- Note accents in CleanPlaceName are preserved</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Places</span>
&nbsp;
<span style="color: #008080;">-- No COLLATE needed!</span>
<span style="color: #0000FF;">SELECT</span> CleanPlaceName, <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Places</span>
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> CleanPlaceName;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/08/group_by_no_collate_needed.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="group by no collate needed" border="0" alt="group by no collate needed" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/08/group_by_no_collate_needed_thumb.png" width="250" height="102" /></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('p849code26'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84926"><td class="code" id="p849code26"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Places</span> t1
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> dbo.<span style="color: #202020;">Places</span> t2
	<span style="color: #0000FF;">ON</span> t1.<span style="color: #202020;">CleanPlaceName</span> <span style="color: #808080;">=</span> t2.<span style="color: #202020;">CleanPlaceName</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/08/join_no_collate_needed.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="join no collate needed" border="0" alt="join no collate needed" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/08/join_no_collate_needed_thumb.png" width="463" height="227" /></a> </p>
<p>This solution removes the need to include the COLLATE clause in your queries and keeps the possibility of using indices open!</p>
<p><map name='google_ad_map_849_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/849?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_849_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=849&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcomparing-accented-strings-with-tsql%2F849' title="Comparing accented strings with TSQL" alt=" Comparing accented strings with TSQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/comparing-accented-strings-with-tsql/849">Comparing accented strings with TSQL</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/comparing-accented-strings-with-tsql/849/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a sqlserverpedia list with Tweet-SQL</title>
		<link>http://www.youdidwhatwithtsql.com/creating-a-sqlserverpedia-list-with-tweet-sql/835</link>
		<comments>http://www.youdidwhatwithtsql.com/creating-a-sqlserverpedia-list-with-tweet-sql/835#comments</comments>
		<pubDate>Sat, 24 Jul 2010 16:24:20 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Tweet-SQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/creating-a-sqlserverpedia-list-with-tweet-sql/835</guid>
		<description><![CDATA[Many moons ago I posted an article illustrating how to befriend twitter users on the sqlserverpedia list with Tweet-SQL. Since Twitter have added various list methods to their API I thought it would be fun to rehash this post to create a list with Tweet-SQL. First copy the list of users from the sqlserverpedia page. [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/creating-a-sqlserverpedia-list-with-tweet-sql/835">Creating a sqlserverpedia list with Tweet-SQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Many moons ago I posted an article illustrating how to befriend twitter users on the <a href="http://sqlclrnews.blogspot.com/2009/01/befriend-twitter-users-on.html" target="_blank">sqlserverpedia list</a> with <a href="http://www.tweet-sql.com" target="_blank">Tweet-SQL</a>. Since <a href="http://twitter.com" target="_blank">Twitter</a> have added various list methods to their <a href="http://apiwiki.twitter.com/Twitter-API-Documentation" target="_blank">API</a> I thought it would be fun to rehash this post to create a list with <font color="#666666"><a href="http://www.tweet-sql.com" target="_blank">Tweet-SQL</a>. </font></p>
<p><font color="#666666">First copy the list of users from the <a href="http://sqlserverpedia.com/wiki/Twitter" target="_blank">sqlserverpedia page</a>.</font></p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/07/copy_sqlserverpedia_list.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="copy sqlserverpedia list" border="0" alt="copy sqlserverpedia list" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/07/copy_sqlserverpedia_list_thumb.png" width="644" height="449" /></a> </p>
<p>Paste this into Excel and you should get something looking like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/07/sqlserverpedia_excel_list.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sqlserverpedia_excel_list" border="0" alt="sqlserverpedia excel list thumb Creating a sqlserverpedia list with Tweet SQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/07/sqlserverpedia_excel_list_thumb.png" width="387" height="484" /></a> </p>
<p>Remove the section titles, empty rows and any text after the url so we are just left with a list of Twitter profile pages.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/07/excel_sqlserverpedia_users.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sqlserverpedia users in excel" border="0" alt="sqlserverpedia users in excel" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/07/excel_sqlserverpedia_users_thumb.png" width="376" height="484" /></a> </p>
<p>Next we need to extract the Twitter username from the url. This little bit of Excel wizardry should do it.</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('p835code32'); return false;">View Code</a> VBA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83532"><td class="code" id="p835code32"><pre class="vba" style="font-family:monospace;">=MID(A1,SEARCH(&quot;http://twitter.com/&quot;,A1)+19, LEN(A1) - 19)</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/07/excel__formula_sqlserverpedia.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="excel_ formula to extract sqlserverpedia list" border="0" alt="excel_ formula to extract sqlserverpedia list" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/07/excel__formula_sqlserverpedia_thumb.png" width="408" height="484" /></a></p>
<p>This formula may break as the page changes in the future so watch out for this. Review the list and remove any invalid values. At the time of writing there’s one of the list that doesn’t contain a twitter url. Save this as a csv and then import it into a database containing the <a href="http://www.tweet-sql.com/features.php" target="_blank">Tweet-SQL Procedures</a>. I’ve uploaded a copy of the file I produced today <a href="http://www.tweet-sql.com/files/sqlserverpedia_users_20100724.csv" target="_blank">here</a>. This file contains 261 Twitter users. I used the below table structure.</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('p835code33'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83533"><td class="code" id="p835code33"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">USE</span> <span style="color: #808080;">&#91;</span>TweetSQLV3<span style="color: #808080;">&#93;</span>
GO
&nbsp;
<span style="color: #008080;">/****** Object:  Table [dbo].[sqlserverpedia]    Script Date: 07/24/2010 16:16:24 ******/</span>
<span style="color: #0000FF;">SET</span> ANSI_<span style="color: #808080;">NULL</span>S <span style="color: #0000FF;">ON</span>
GO
&nbsp;
<span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">ON</span>
GO
&nbsp;
<span style="color: #0000FF;">SET</span> ANSI_PADD<span style="color: #808080;">IN</span>G <span style="color: #0000FF;">ON</span>
GO
&nbsp;
<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>sqlserverpedia<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">&#91;</span>url<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>tweep<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">ON</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">PRIMARY</span><span style="color: #808080;">&#93;</span>
&nbsp;
GO
&nbsp;
<span style="color: #0000FF;">SET</span> ANSI_PADD<span style="color: #808080;">IN</span>G <span style="color: #0000FF;">OFF</span>
GO</pre></td></tr></table></div>

<p>There’s a few dupes in the below list, because people are listed in multiple sections, so run the below TSQL script to de-duplicate it.</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('p835code34'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83534"><td class="code" id="p835code34"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">sqlserverpedia</span> <span style="color: #0000FF;">ADD</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>;
GO
&nbsp;
<span style="color: #0000FF;">DELETE</span> t1
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">sqlserverpedia</span> <span style="color: #0000FF;">AS</span> t1
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> dbo.<span style="color: #202020;">sqlserverpedia</span> <span style="color: #0000FF;">AS</span> t2
	<span style="color: #0000FF;">ON</span> t1.<span style="color: #202020;">tweep</span> <span style="color: #808080;">=</span> t2.<span style="color: #202020;">tweep</span>
<span style="color: #0000FF;">WHERE</span> t1.<span style="color: #202020;">id</span> <span style="color: #808080;">&amp;</span>lt; t2.<span style="color: #202020;">id</span>;
&nbsp;
<span style="color: #008080;">-- Additional column we'll use later</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">sqlserverpedia</span> <span style="color: #0000FF;">ADD</span> done <span style="color: #0000FF;">BIT</span> <span style="color: #0000FF;">DEFAULT</span> <span style="color: #000;">0</span>;</pre></td></tr></table></div>

<p>Next we’ll need to create the list. The below TSQL will do this (for SQL 2008). Make a note of the list id as we’ll need this later.</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('p835code35'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83535"><td class="code" id="p835code35"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @list_name <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;">=</span> <span style="color: #FF0000;">'sqlserverpedia list'</span>;
<span style="color: #008080;">-- Turn on relational resultsets in Tweet-SQL</span>
<span style="color: #0000FF;">EXEC</span> dbo.<span style="color: #202020;">tweet_cfg_resultset_send</span> <span style="color: #000;">1</span>;
<span style="color: #008080;">-- Create the list </span>
<span style="color: #0000FF;">EXEC</span> dbo.<span style="color: #202020;">tweet_list_post_lists</span> @list_name, <span style="color: #FF0000;">'public'</span>, null;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/07/tweetsql_twitter_list_created.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Tweet-SQL twitter list created" border="0" alt="Tweet-SQL twitter list created" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/07/tweetsql_twitter_list_created_thumb.png" width="644" height="161" /></a> </p>
<p>Next we’ll add the users onto this list. After each record is added to the Twitter list it is flagged as done so you can simply re-run the script if something fails halfway through.</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('p835code36'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83536"><td class="code" id="p835code36"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @tweep <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span>, @list_id <span style="color: #0000FF;">INTEGER</span>;
&nbsp;
<span style="color: #008080;">-- Set your list id here</span>
<span style="color: #0000FF;">SET</span> @list_id <span style="color: #808080;">=</span> <span style="color: #000;">17542298</span>;
&nbsp;
<span style="color: #008080;">-- Turn off resultsets in Tweet-SQL</span>
<span style="color: #0000FF;">EXEC</span> dbo.<span style="color: #202020;">tweet_cfg_resultset_send</span> <span style="color: #000;">0</span>;
&nbsp;
<span style="color: #0000FF;">DECLARE</span> tweeps <span style="color: #0000FF;">CURSOR</span> <span style="color: #0000FF;">LOCAL</span> FAST_F<span style="color: #808080;">OR</span>WARD <span style="color: #0000FF;">FOR</span> <span style="color: #0000FF;">SELECT</span> tweep
					 <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">sqlserverpedia</span>
					 <span style="color: #0000FF;">WHERE</span> done <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span>;
&nbsp;
<span style="color: #008080;">-- Open the cursor and get the first result</span>
<span style="color: #0000FF;">OPEN</span> tweeps;
<span style="color: #0000FF;">FETCH</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">FROM</span> tweeps <span style="color: #0000FF;">INTO</span> @tweep;
&nbsp;
<span style="color: #0000FF;">WHILE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">@@FETCH_STATUS</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
	<span style="color: #008080;">-- Add the tweep to the list</span>
	<span style="color: #0000FF;">EXEC</span> dbo.<span style="color: #202020;">tweet_list_post_list_members</span> @list_id, @tweep, null;
	<span style="color: #008080;">-- Flag the current record as done</span>
	<span style="color: #0000FF;">UPDATE</span> dbo.<span style="color: #202020;">sqlserverpedia</span>
	<span style="color: #0000FF;">SET</span> done <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
	<span style="color: #0000FF;">WHERE</span> tweep <span style="color: #808080;">=</span> @tweep;
	<span style="color: #008080;">-- Wait for a bit so we don't annoy twitter</span>
	<span style="color: #0000FF;">WAITFOR</span> DELAY <span style="color: #FF0000;">'00:00:05'</span>
	<span style="color: #008080;">-- Fetch the next row</span>
	<span style="color: #0000FF;">FETCH</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">FROM</span> tweeps <span style="color: #0000FF;">INTO</span> @tweep;
&nbsp;
<span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #008080;">-- Clean up</span>
<span style="color: #0000FF;">DEALLOCATE</span> tweeps;
<span style="color: #008080;">-- Turn Tweet-SQL resultsets back on</span>
<span style="color: #0000FF;">EXEC</span> dbo.<span style="color: #202020;">tweet_cfg_resultset_send</span> <span style="color: #000;">1</span>;</pre></td></tr></table></div>

<p>See my finished list <a href="http://twitter.com/rhyscampbell/sqlserverpedia-list" target="_blank">here</a> and follow <a href="http://twitter.com/rhyscampbell" target="_blank">me on twitter</a> for more TSQL tomfoolery!</p>
<p><map name='google_ad_map_835_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/835?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_835_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=835&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcreating-a-sqlserverpedia-list-with-tweet-sql%2F835' title="Creating a sqlserverpedia list with Tweet SQL" alt=" Creating a sqlserverpedia list with Tweet SQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/creating-a-sqlserverpedia-list-with-tweet-sql/835">Creating a sqlserverpedia list with Tweet-SQL</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/creating-a-sqlserverpedia-list-with-tweet-sql/835/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Can&#8217;t reopen table: &#8216;t1&#8242;</title>
		<link>http://www.youdidwhatwithtsql.com/cant-reopen-table-t1/823</link>
		<comments>http://www.youdidwhatwithtsql.com/cant-reopen-table-t1/823#comments</comments>
		<pubDate>Mon, 19 Jul 2010 21:24:08 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/cant-reopen-table-t1/823</guid>
		<description><![CDATA[I&#8217;m quite often jumping between MySQL and SQL Server so remembering the quirks and limitations of each system can be difficult. With MySQL, if you attempt to reference a temporary table more than once in the same query, you will encounter the following error; Error Code : 1137 Can't reopen table: 't1’ The following provides [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/cant-reopen-table-t1/823">Can&#8217;t reopen table: &#8216;t1&#8242;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m quite often jumping between <a href="http://www.mysql.com" target="_blank">MySQL</a> and <a href="http://www.microsoft.com/en/gb/sqlserver/default.aspx" target="_blank">SQL Server</a> so remembering the quirks and limitations of each system can be difficult. With MySQL, if you attempt to reference a temporary table more than once in the same query, you will encounter the following error;
<pre>Error Code : 1137
Can't reopen table: 't1’</pre>
<p>The following provides an example of this&#8230;</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('p823code40'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p82340"><td class="code" id="p823code40"><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;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> test
<span style="color: #FF00FF;">&#40;</span>
                Id <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test <span style="color: #990099; font-weight: bold;">AS</span> t1
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> test <span style="color: #990099; font-weight: bold;">AS</span> t2
     <span style="color: #990099; font-weight: bold;">ON</span> t1.Id <span style="color: #CC0099;">=</span> t2.Id<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>It&#8217;s not just self-joins that have this issue <strong>UNIONS</strong> do as well;</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('p823code41'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p82341"><td class="code" id="p823code41"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test <span style="color: #990099; font-weight: bold;">AS</span> t1
<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: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test <span style="color: #990099; font-weight: bold;">AS</span> t2<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>There&#8217;s a <a href="http://stackoverflow.com/questions/343402/getting-around-mysql-cant-reopen-table-error">thread over on Stackoverflow</a> discussing this problem. Here&#8217;s a solution I commonly use to get around the problem;</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('p823code42'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p82342"><td class="code" id="p823code42"><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: #808080; font-style: italic;"># Create temp table</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> test
<span style="color: #FF00FF;">&#40;</span>
                Id <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Insert some test data</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> test <span style="color: #FF00FF;">&#40;</span>Id<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;">1</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">3</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">4</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">5</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Clone the table. This will do structure &amp; indices but no data.</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> test2 <span style="color: #CC0099; font-weight: bold;">LIKE</span> test<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Insert the data into the new table</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> test2
<span style="color: #990099; font-weight: bold;">SELECT</span> Id
<span style="color: #990099; font-weight: bold;">FROM</span> test<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Now our queries will work if we use the tables clone</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test <span style="color: #990099; font-weight: bold;">AS</span> t1
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> test2 <span style="color: #990099; font-weight: bold;">AS</span> t2
      <span style="color: #990099; font-weight: bold;">ON</span> t1.Id <span style="color: #CC0099;">=</span> t2.Id<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test <span style="color: #990099; font-weight: bold;">AS</span> t1
<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: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test2 <span style="color: #990099; font-weight: bold;">AS</span> t2<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Clean up</span>
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> test<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> test2<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><map name='google_ad_map_823_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/823?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_823_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=823&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcant-reopen-table-t1%2F823' title="Cant reopen table: t1" alt=" Cant reopen table: t1" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/cant-reopen-table-t1/823">Can&#8217;t reopen table: &#8216;t1&#8242;</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/cant-reopen-table-t1/823/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rename MySQL Stored Procedures</title>
		<link>http://www.youdidwhatwithtsql.com/rename-mysql-stored-procedures/819</link>
		<comments>http://www.youdidwhatwithtsql.com/rename-mysql-stored-procedures/819#comments</comments>
		<pubDate>Wed, 14 Jul 2010 20:47:05 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/rename-mysql-stored-procedures/819</guid>
		<description><![CDATA[I’ve previously blogged about the limitations of MySQL Alter Procedure Syntax and I came across a thread on the MySQL forums with a possible solution. I thought it might be handy to wrap this up into a stored procedure akin to SQL Server’s sp_rename. This procedure will allow you to easily rename MySQL Stored Procedures [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/rename-mysql-stored-procedures/819">Rename MySQL Stored Procedures</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I’ve previously blogged about the limitations of <a href="http://www.youdidwhatwithtsql.com/mysql-alter-procedure-syntax/788" target="_blank">MySQL Alter Procedure Syntax</a> and I came across a thread on the <a href="http://forums.mysql.com/read.php?10,274538,274563#msg-274563" target="_blank">MySQL forums</a> with a possible solution. I thought it might be handy to wrap this up into a stored procedure akin to <a href="http://msdn.microsoft.com/en-us/library/ms188351.aspx" target="_blank">SQL Server’s sp_rename</a>. </p>
<p>This procedure will allow you to easily rename MySQL Stored Procedures in any database. Please be aware that this does update the MySQL system tables and has only had minimal testing. As with all tips you find on the Internet please use with caution!</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('p819code45'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81945"><td class="code" id="p819code45"><pre class="mysql" style="font-family:monospace;">DELIMITER $$
&nbsp;
<span style="color: #990099; font-weight: bold;">USE</span> <span style="color: #008000;">`mysql`</span>$$
&nbsp;
<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;">`mysp<span style="color: #008080; font-weight: bold;">_</span>rename<span style="color: #008080; font-weight: bold;">_</span>proc`</span>$$
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">DEFINER</span><span style="color: #CC0099;">=</span><span style="color: #008000;">`root`</span>@<span style="color: #008000;">`<span style="color: #008080; font-weight: bold;">%</span>`</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #008000;">`mysp<span style="color: #008080; font-weight: bold;">_</span>rename<span style="color: #008080; font-weight: bold;">_</span>proc`</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> p_proc_name <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">64</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">IN</span> p_new_name <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">64</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">IN</span> p_db <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">64</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">MODIFIES SQL DATA</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;">'Use to rename stored procedures.'</span>
<span style="color: #990099; font-weight: bold;">BEGIN</span>
    proc: <span style="color: #990099; font-weight: bold;">BEGIN</span>
	<span style="color: #808080; font-style: italic;"># A few tests to see if the input is sensible</span>
	<span style="color: #009900;">IF</span> <span style="color: #000099;">CHARACTER_LENGTH</span><span style="color: #FF00FF;">&#40;</span><span style="color: #009900;">IFNULL</span><span style="color: #FF00FF;">&#40;</span>p_proc_name<span style="color: #000033;">,</span> <span style="color: #008000;">''</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span> <span style="color: #CC0099; font-weight: bold;">OR</span> <span style="color: #000099;">CHARACTER_LENGTH</span><span style="color: #FF00FF;">&#40;</span><span style="color: #009900;">IFNULL</span><span style="color: #FF00FF;">&#40;</span>p_new_name<span style="color: #000033;">,</span> <span style="color: #008000;">''</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span>
		<span style="color: #CC0099; font-weight: bold;">OR</span> <span style="color: #000099;">CHARACTER_LENGTH</span><span style="color: #FF00FF;">&#40;</span><span style="color: #009900;">IFNULL</span><span style="color: #FF00FF;">&#40;</span>p_db<span style="color: #000033;">,</span> <span style="color: #008000;">''</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span> <span style="color: #009900;">THEN</span>
	<span style="color: #990099; font-weight: bold;">BEGIN</span>
		<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'Error: One of more of the input parameters is zero in length.'</span> <span style="color: #990099; font-weight: bold;">AS</span> Error<span style="color: #000033;">;</span>
		LEAVE proc<span style="color: #000033;">;</span> 
	<span style="color: #009900;">END</span><span style="color: #000033;">;</span>
	ELSEIF <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">COUNT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #CC0099;">*</span><span style="color: #FF00FF;">&#41;</span>
		 <span style="color: #990099; font-weight: bold;">FROM</span> mysql.proc
		 <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #008000;">`name`</span> <span style="color: #CC0099;">=</span> p_proc_name
		 <span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #008000;">`type`</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">'PROCEDURE'</span>
		 <span style="color: #CC0099; font-weight: bold;">AND</span> db <span style="color: #CC0099;">=</span> p_db<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span> <span style="color: #009900;">THEN</span>
	<span style="color: #990099; font-weight: bold;">BEGIN</span>
		<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'Error: The procedure specified in p<span style="color: #008080; font-weight: bold;">_</span>proc<span style="color: #008080; font-weight: bold;">_</span>name does not exist in this database.'</span> <span style="color: #990099; font-weight: bold;">AS</span> Error<span style="color: #000033;">;</span>
		LEAVE proc<span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span><span style="color: #000033;">;</span>
	ELSEIF <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">COUNT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #CC0099;">*</span><span style="color: #FF00FF;">&#41;</span>
		 <span style="color: #990099; font-weight: bold;">FROM</span> mysql.proc
		 <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #008000;">`name`</span> <span style="color: #CC0099;">=</span> p_new_name
		 <span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #008000;">`type`</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">'PROCEDURE'</span>
		 <span style="color: #CC0099; font-weight: bold;">AND</span> db <span style="color: #CC0099;">=</span> p_db<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span> <span style="color: #009900;">THEN</span>
	<span style="color: #990099; font-weight: bold;">BEGIN</span>
		<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'Error: Unable to rename the procedure specified in p<span style="color: #008080; font-weight: bold;">_</span>proc<span style="color: #008080; font-weight: bold;">_</span>name as it already exists in this database.'</span> <span style="color: #990099; font-weight: bold;">AS</span> Error<span style="color: #000033;">;</span>
		LEAVE proc<span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># Rename the proc</span>
	<span style="color: #990099; font-weight: bold;">UPDATE</span> <span style="color: #008000;">`mysql`</span>.<span style="color: #008000;">`proc`</span>
	<span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #008000;">`name`</span> <span style="color: #CC0099;">=</span> p_new_name<span style="color: #000033;">,</span>
	specific_name <span style="color: #CC0099;">=</span> p_new_name
	<span style="color: #990099; font-weight: bold;">WHERE</span> db <span style="color: #CC0099;">=</span> p_db 
	<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #008000;">`name`</span> <span style="color: #CC0099;">=</span> p_proc_name
	<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #008000;">`type`</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">'PROCEDURE'</span><span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># Update any associated privileges</span>
	<span style="color: #990099; font-weight: bold;">UPDATE</span> <span style="color: #008000;">`mysql`</span>.<span style="color: #008000;">`procs<span style="color: #008080; font-weight: bold;">_</span>priv`</span>
	<span style="color: #990099; font-weight: bold;">SET</span> Routine_name <span style="color: #CC0099;">=</span> p_new_name
	<span style="color: #990099; font-weight: bold;">WHERE</span> db <span style="color: #CC0099;">=</span> p_db 
	<span style="color: #CC0099; font-weight: bold;">AND</span> Routine_name <span style="color: #CC0099;">=</span> p_proc_name
	<span style="color: #CC0099; font-weight: bold;">AND</span> Routine_type <span style="color: #CC0099;">=</span> <span style="color: #008000;">'PROCEDURE'</span><span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># Check update rowcount to see if privileges need to be flushed</span>
	<span style="color: #009900;">IF</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">ROW_COUNT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">&gt;</span> <span style="color: #008080;">0</span> <span style="color: #009900;">THEN</span>
	<span style="color: #990099; font-weight: bold;">BEGIN</span>
		FLUSH <span style="color: #990099; font-weight: bold;">PRIVILEGES</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
&nbsp;
    <span style="color: #009900;">END</span> proc<span style="color: #000033;">;</span>
    <span style="color: #009900;">END</span>$$
&nbsp;
DELIMITER <span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Usage is as follows;</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('p819code46'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81946"><td class="code" id="p819code46"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CALL</span> mysp_rename_proc<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'usp<span style="color: #008080; font-weight: bold;">_</span>proc'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'usp<span style="color: #008080; font-weight: bold;">_</span>new<span style="color: #008080; font-weight: bold;">_</span>proc<span style="color: #008080; font-weight: bold;">_</span>name'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'database<span style="color: #008080; font-weight: bold;">_</span>proc<span style="color: #008080; font-weight: bold;">_</span>exists<span style="color: #008080; font-weight: bold;">_</span>in'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>As the thread poster mentions the Stored Procedure is callable by it’s old name as well as the new one until you reconnect. There doesn’t seem to be any suitable <a href="http://dev.mysql.com/doc/refman/5.1/en/flush.html" target="_blank">FLUSH</a> command to resolve this.</p>
<p>If you liked this you may like;</p>
<p><a href="http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_spaceused/636" target="_blank">MySQL Clone of SP_SpaceUsed</a></p>
<p><a href="http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_msforeachtable/624" target="_blank">MySQL Clone of SP_MsForEachTable</a></p>
<p><map name='google_ad_map_819_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/819?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_819_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=819&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Frename-mysql-stored-procedures%2F819' title="Rename MySQL Stored Procedures" alt=" Rename MySQL Stored Procedures" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/rename-mysql-stored-procedures/819">Rename MySQL Stored Procedures</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/rename-mysql-stored-procedures/819/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Purge MySQL Binary Logs</title>
		<link>http://www.youdidwhatwithtsql.com/purge-mysql-binary-logs/816</link>
		<comments>http://www.youdidwhatwithtsql.com/purge-mysql-binary-logs/816#comments</comments>
		<pubDate>Mon, 12 Jul 2010 10:58:03 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/?p=816</guid>
		<description><![CDATA[From time-to-time you may need to manually purge binary logs on your MySQL slaves to free up a bit of disk space. We can achieve this by using the PURGE BINARY LOGS command from the MySQL command line client. MySQL advises the following procedure when purging these logs To safely purge binary log files, follow [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/purge-mysql-binary-logs/816">Purge MySQL Binary Logs</a></p>
]]></description>
			<content:encoded><![CDATA[<p>From time-to-time you may need to manually purge binary logs on your <a title="MySQL replication" href="http://dev.mysql.com/doc/refman/5.1/en/replication.html" target="_blank">MySQL slaves</a> to free up a bit of disk space. We can achieve this by using the <a title="PURGE BINARY LOGS" href="http://dev.mysql.com/doc/refman/5.5/en/purge-binary-logs.html" target="_blank">PURGE BINARY LOGS command</a> from the MySQL command line client. MySQL advises the following procedure when purging these logs</p>
<blockquote><p>To safely purge binary log files, follow this procedure:</p>
<div>
<ol type="1">
<li>On each slave server, use <a title="12.4.5.35. SHOW SLAVE STATUS Syntax" href="http://dev.mysql.com/doc/refman/5.5/en/show-slave-status.html"><code>SHOW SLAVE             STATUS</code></a> to check which log file it is reading.</li>
<li>Obtain a listing of the binary log files on the master             server with <a title="12.4.5.2. SHOW BINARY LOGS Syntax" href="http://dev.mysql.com/doc/refman/5.5/en/show-binary-logs.html"><code>SHOW BINARY LOGS</code></a>.</li>
<li>Determine the earliest log file among all the slaves. This             is the target file. If all the slaves are up to date, this             is the last log file on the list.</li>
<li>Make a backup of all the log files you are about to delete.             (This step is optional, but always advisable.)</li>
<li>Purge all log files up to but not including the target file. <a title="MySQL purge binary logs" href="http://dev.mysql.com/doc/refman/5.5/en/purge-binary-logs.html" target="_blank">source</a></li>
</ol>
</div>
</blockquote>
<p>The below example will purge all binary logs older than the one called <strong>backup-master.001271</strong>.</p>
<pre>Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2640654
Server version: 5.1.34-log SUSE MySQL RPM

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql&gt; PURGE BINARY LOGS TO 'backup-master.001271'
</pre>
<p>Provided your slave is running this command is reasonably safe as MySQL will prevent you from purging any log files that are currently being read. If the slave thread is not running then you do have to make sure you are not purging a needed file. If you purge a needed file the slave will break when it is restarted. You can also use the <a title="expire_logs_days in my.cnf" href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_expire_logs_days" target="_blank">expire_logs_days</a> variable to automatically control when binary logs are purged.</p>
<p><map name='google_ad_map_816_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/816?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_816_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=816&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fpurge-mysql-binary-logs%2F816' title="Purge MySQL Binary Logs" alt=" Purge MySQL Binary Logs" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/purge-mysql-binary-logs/816">Purge MySQL Binary Logs</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/purge-mysql-binary-logs/816/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>For RANGE partitions each partition must be defined</title>
		<link>http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815</link>
		<comments>http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815#comments</comments>
		<pubDate>Fri, 09 Jul 2010 12:08:31 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[partition]]></category>
		<category><![CDATA[partitioning]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815</guid>
		<description><![CDATA[If you encounter the following error when trying to create a partitioned table in MySQL Error Code : 1492 For RANGE partitions each partition must be defined Assuming you have defined your partitions then you probably have a syntax error. Take the following incorrect example. ?View Code MYSQLCREATE TABLE People &#40; PersonId INTEGER NOT NULL [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815">For RANGE partitions each partition must be defined</a></p>
]]></description>
			<content:encoded><![CDATA[<p>If you encounter the following error when trying to create a partitioned table in MySQL</p>
<pre>Error Code : 1492
For RANGE partitions each partition must be defined</pre>
<p>Assuming you have defined your partitions then you probably have a syntax error. Take the following incorrect 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('p815code49'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81549"><td class="code" id="p815code49"><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> People
<span style="color: #FF00FF;">&#40;</span>
	PersonId <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</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>
	DateOfBirth <span style="color: #999900; font-weight: bold;">DATE</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	Telephone <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">30</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	Email <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">200</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	GroupId <span style="color: #999900; font-weight: bold;">SMALLINT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #FF00FF;">&#40;</span>PersonId<span style="color: #000033;">,</span> GroupId<span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span>
PARTITION BY RANGE <span style="color: #FF00FF;">&#40;</span>GroupId<span style="color: #FF00FF;">&#41;</span>
PARTITION p0 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
PARTITION p1 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">200</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
PARTITION p2 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">300</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
PARTITION p3 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">400</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
PARTITION p4 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">500</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
PARTITION p5 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">600</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
PARTITION p6 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">700</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>The above statement is not syntactically correct but the error thrown in this case is not particularly helpful. All that is missing here is a couple of braces around the partition range definitions. The below DDL statement is correct.</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('p815code50'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81550"><td class="code" id="p815code50"><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> People
<span style="color: #FF00FF;">&#40;</span>
	PersonId <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</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>
	DateOfBirth <span style="color: #999900; font-weight: bold;">DATE</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	Telephone <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">30</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	Email <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">200</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	GroupId <span style="color: #999900; font-weight: bold;">SMALLINT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #FF00FF;">&#40;</span>PersonId<span style="color: #000033;">,</span> GroupId<span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span>
PARTITION BY RANGE <span style="color: #FF00FF;">&#40;</span>GroupId<span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#40;</span>
	PARTITION p0 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	PARTITION p1 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">200</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	PARTITION p2 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">300</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	PARTITION p3 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">400</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	PARTITION p4 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">500</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	PARTITION p5 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">600</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	PARTITION p6 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">700</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><map name='google_ad_map_815_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/815?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_815_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=815&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ffor-range-partitions-each-partition-must-be-defined%2F815' title="For RANGE partitions each partition must be defined" alt=" For RANGE partitions each partition must be defined" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815">For RANGE partitions each partition must be defined</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
