<?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>ALA-(GU)-KA-(NNA)-N &#187; MS SQL Server</title>
	<atom:link href="http://blogs.alagukannan.com/category/ms-sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.alagukannan.com</link>
	<description>Past dies as we speak Future comes as we speak present is what we speak</description>
	<lastBuildDate>Mon, 09 Jan 2012 20:22:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to identify and delete duplicate records from sql table</title>
		<link>http://blogs.alagukannan.com/2009/03/16/how-to-identify-and-delete-duplicate-records-from-sql-table/</link>
		<comments>http://blogs.alagukannan.com/2009/03/16/how-to-identify-and-delete-duplicate-records-from-sql-table/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 08:12:35 +0000</pubDate>
		<dc:creator>toalakan-web</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[MS SQL Server 2000]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Codes]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blogs.alagukannan.com/?p=195</guid>
		<description><![CDATA[<a href="http://blogs.alagukannan.com/2009/03/16/how-to-identify-and-delete-duplicate-records-from-sql-table/" title="How to identify and delete duplicate records from sql table"></a>Well I&#8217;m sure any SQL developer out there would have encountered a problem where the duplicate records in a table have to eliminated for some reasons. I recently encountered a similar situation where I had to remove the duplicate user &#8230;<p class="read-more"><a href="http://blogs.alagukannan.com/2009/03/16/how-to-identify-and-delete-duplicate-records-from-sql-table/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://blogs.alagukannan.com/2009/03/16/how-to-identify-and-delete-duplicate-records-from-sql-table/" title="How to identify and delete duplicate records from sql table"></a><p>Well I&#8217;m sure any SQL developer out there would have encountered a problem where the duplicate records in a table have to eliminated for some reasons.</p>
<p>I recently encountered a similar situation where I had to remove the duplicate user name from a table with just one simple query . Here is how I did it:</p>
<p>Here is my tbl_username table having the following columns UID( auto increment primary key), uname (varchar(100)), fname (varchar(100)):</p>
<blockquote>
<table style="height: 112px;" border="1" width="200">
<tbody>
<tr>
<td><strong>UID</strong></td>
<td><strong>Uname</strong></td>
<td><strong>fname</strong></td>
</tr>
<tr>
<td>1</td>
<td>xyz</td>
<td>xyz</td>
</tr>
<tr>
<td>2</td>
<td>xyz</td>
<td>xyz</td>
</tr>
<tr>
<td>3</td>
<td>abc</td>
<td>abc</td>
</tr>
<tr>
<td>4</td>
<td>qwerty</td>
<td>qwerty</td>
</tr>
<tr>
<td>5</td>
<td>qwerty</td>
<td>qwerty</td>
</tr>
</tbody>
</table>
</blockquote>
<p>We would have to remove the duplicate user names in order to do that we first build a sub query that picks up the uname and max id of the duplicate records.</p>
<blockquote><p>Select uname, max(UID) as latestUID from tbl_username group by uname having count(uname) &gt; 1</p>
<p>Result:</p>
<table border="1" width="200">
<tbody>
<tr>
<td><strong>Uname</strong></td>
<td><strong>latestUID</strong></td>
</tr>
<tr>
<td>xyz</td>
<td>2</td>
</tr>
<tr>
<td>qwerty</td>
<td>5</td>
</tr>
</tbody>
</table>
</blockquote>
<p>By using the max function we can pick the latest entry to be deleted if you like to pick the lowest use the min() function.</p>
<p>Well if you have got this then its pretty much simple to get the build another sub query on top of this to get the unique IDs and then delete it as usual.</p>
<blockquote><p>delete from tbl_username<br />
where UID in<br />
(<br />
select latestUID from (<br />
Select uname, max(UID) as latestUID from tbl_username group by uname having count(uname) &gt; 1<br />
)<br />
)</p></blockquote>
<p>Now the table has the following data in it</p>
<blockquote>
<table style="height: 112px;" border="1" width="200">
<tbody>
<tr>
<td><strong>UID</strong></td>
<td><strong>Uname</strong></td>
<td><strong>fname</strong></td>
</tr>
<tr>
<td>1</td>
<td>xyz</td>
<td>xyz</td>
</tr>
<tr>
<td>3</td>
<td>abc</td>
<td>abc</td>
</tr>
<tr>
<td>4</td>
<td>qwerty</td>
<td>qwerty</td>
</tr>
</tbody>
</table>
</blockquote>
<p>Well this way of deleting would only work if the table uses an integer as a primary key.</p>
<p>This is the cure but prevention of this kind of situation can be resolved by just using unique keys on the Uname field.</p>
<p>Prevention is always better than cure so grab a SQL book if you have done this mistake.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.alagukannan.com/2009/03/16/how-to-identify-and-delete-duplicate-records-from-sql-table/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to get Current DB name and user in MS SQL server?</title>
		<link>http://blogs.alagukannan.com/2009/02/17/how-to-get-db-name-and-current-user-in-s/</link>
		<comments>http://blogs.alagukannan.com/2009/02/17/how-to-get-db-name-and-current-user-in-s/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 08:19:58 +0000</pubDate>
		<dc:creator>toalakan-web</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Codes]]></category>
		<category><![CDATA[ms sql 2000]]></category>

		<guid isPermaLink="false">http://blogs.alagukannan.com/?p=152</guid>
		<description><![CDATA[<a href="http://blogs.alagukannan.com/2009/02/17/how-to-get-db-name-and-current-user-in-s/" title="How to get Current DB name and user in MS SQL server?"></a>Most of the time I forget the command to get the DB name and current user when working. So decided to make post about it no better way to find the info fast. Here are the 2 commands that basically &#8230;<p class="read-more"><a href="http://blogs.alagukannan.com/2009/02/17/how-to-get-db-name-and-current-user-in-s/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://blogs.alagukannan.com/2009/02/17/how-to-get-db-name-and-current-user-in-s/" title="How to get Current DB name and user in MS SQL server?"></a><p>Most of the time I forget the command to get the DB name and current user when working. So decided to make post about it no better way to find the info fast.</p>
<p>Here are the 2 commands that basically gets the info.</p>
<ul>
<li>Select DB_name() ;</li>
<li>Select Current_user;</li>
</ul>
<p>Here is a post that my friend wrote on <span id="dnn_ctr368_ArticleDetails_lblTitle" class="Head"><a title="Insert Values into an Identity Column" href="http://www.thysite.com/Articles/tabid/55/ctl/Details/mid/368/ItemID/1/Default.aspx" target="_blank">How to Insert Values into an Identity Column</a> which I didn&#8217;t know to do before. </span></p>
<p><span id="dnn_ctr368_ArticleDetails_lblTitle" class="Head">BTW he is Microsoft guy not CF <img src='http://blogs.alagukannan.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /><br />
</span></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.alagukannan.com/2009/02/17/how-to-get-db-name-and-current-user-in-s/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

