<?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>Scriptstar &#187; SQL Server</title>
	<atom:link href="http://www.scriptstar.co.uk/tag/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scriptstar.co.uk</link>
	<description></description>
	<lastBuildDate>Fri, 23 Jul 2010 22:41:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Identity column starts from zero instead of one</title>
		<link>http://www.scriptstar.co.uk/identity-column-starts-from-zero-instead-of-one/</link>
		<comments>http://www.scriptstar.co.uk/identity-column-starts-from-zero-instead-of-one/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 14:00:10 +0000</pubDate>
		<dc:creator>Narendra</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.scriptstar.co.uk/?p=17</guid>
		<description><![CDATA[I was experiencing a strange problem. Identity field of a table is set to [1,1] by generating script. Every time, when I drop the whole database, and then re-create it by running script in SQL Server 2005 Management Studio, and then insert a record into that table programmatically, SQL Server set identity field value of [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.scriptstar.co.uk%2Fidentity-column-starts-from-zero-instead-of-one%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.scriptstar.co.uk%2Fidentity-column-starts-from-zero-instead-of-one%2F&amp;style=normal&amp;service=bit.ly&amp;service_api=scriptstar%3AR_991057c9b732a05f226e1bff49cdbf3a&amp;hashtags=SQL+Server,T-SQL&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I was experiencing a strange problem. Identity field of a table is set to [1,1] by generating script. Every time, when I drop the whole database, and then re-create it by running script in SQL Server 2005 Management Studio, and then insert a record into that table programmatically, SQL Server set identity field value of that first record to 0 instead of 1!</p>
<p>I was gone mad until I have comeup with a small piece of code like this. The code will delete all the rows from all the tables in your database. So use it cautiously.</p>
<p><code><br />
CREATE PROCEDURE [dbo].[ReSeed] AS<br />
BEGIN<br />
EXEC sp_MSForEachTable '<br />
IF OBJECTPROPERTY(object_id(''?''), ''TableHasIdentity'') = 1<br />
BEGIN TRY<br />
TRUNCATE TABLE ?<br />
DBCC CHECKIDENT (''?'', RESEED, 1)<br />
END TRY<br />
BEGIN CATCH<br />
DELETE FROM ?<br />
DBCC CHECKIDENT (''?'', RESEED, 0)<br />
END CATCH<br />
'<br />
END<br />
</code></p>
<p>Enjoy!
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.scriptstar.co.uk%2Fidentity-column-starts-from-zero-instead-of-one%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.scriptstar.co.uk%2Fidentity-column-starts-from-zero-instead-of-one%2F&amp;style=normal&amp;service=bit.ly&amp;service_api=scriptstar%3AR_991057c9b732a05f226e1bff49cdbf3a&amp;hashtags=SQL+Server,T-SQL&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<div class="shr-publisher-17"></div>
	<p>
	    <a rev="vote-for" href="http://www.sharepointsidekick.com/Submit?url=http%3A%2F%2Fwww.scriptstar.co.uk%2Fidentity-column-starts-from-zero-instead-of-one%2F&borderColor=909090&textBackColor=336699&textForeColor=ffffff&countBackColor=ededed&countForeColor=000000&title=Identity+column+starts+from+zero+instead+of+one">
	<img alt="sidekick it" src="http://www.sharepointsidekick.com/image.axd?url=http%3A%2F%2Fwww.scriptstar.co.uk%2Fidentity-column-starts-from-zero-instead-of-one%2F&borderColor=909090&textBackColor=336699&textForeColor=ffffff&countBackColor=ededed&countForeColor=000000" style="border:0px"/>
	   </a>
	</p>
	
	]]></content:encoded>
			<wfw:commentRss>http://www.scriptstar.co.uk/identity-column-starts-from-zero-instead-of-one/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

