<?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>Point41 &#187; DBCC CHECKIDENT</title>
	<atom:link href="http://www.point41.com/index.php/tag/dbcc-checkident/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.point41.com</link>
	<description>IT Tips &#38; Tricks</description>
	<lastBuildDate>Thu, 13 Aug 2009 21:24:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Reseeding Identity Columns for Replication</title>
		<link>http://www.point41.com/index.php/2009/08/13/reseeding-identity-columns-for-replication/</link>
		<comments>http://www.point41.com/index.php/2009/08/13/reseeding-identity-columns-for-replication/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 21:24:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[DBCC CHECKIDENT]]></category>

		<guid isPermaLink="false">http://www.point41.com/?p=100</guid>
		<description><![CDATA[As a software developer/architect in this era of downsizing and people becoming Jacks of all Trades, I frequently find myself in a situation where I have to do DBA stuff. In one particular case, I am maintaining two databases which are peer-to-peer replicated. The databases support a web application for a primary site and a [...]]]></description>
			<content:encoded><![CDATA[<p>As a software developer/architect in this era of downsizing and people becoming Jacks of all Trades, I frequently find myself in a situation where I have to do DBA stuff. In one particular case, I am maintaining two databases which are peer-to-peer replicated. The databases support a web application for a primary site and a disaster recovery (DR) site.</p>
<p>Though already running in production mode, the web application is still changing because new features are being added on a daily basis. As a result of this, schema changes are pushed to the database almost weekly and I just don&#8217;t have the time to maintain the SQL scripts required to keep replication in sync with the schema changes. It is much easier to just: 1) take down replication and delete the DR database, 2) update the schema on the primary database, 3) restore a new copy of the second DR from a backup of the primary, 4) reseed identity columns on the DR database, 5) set up replication from scratch.</p>
<p>It may sound stupid, but whatever. Setting up replication from scratch through the UI works out to be easier than maintaining the change scripts required to keep several hundred tables properly replicated while new tables are columns are being constantly added. The only pain-in-the-ass part of this method is that the identity columns of hundreds of tables in the DR database need to be reseeded each time. Fortunately this can be done quickly.</p>
<p>Our rule is that indentity columns of type <strong>int</strong> should have values in the range of 1 &#8211; 1073741823 on the main database, and on the second database the range should be 1073741824 and up. For <strong>bigint</strong> it should be 1 &#8211; 10737418239 on the main database and 10737418240 and up on the second database.</p>
<p>So for a table tbl_abc which has an identity column of type <strong>int</strong> to run the following 2 SQL statements on the DR database:</p>
<div style="background-color: #dfdfdf; padding-bottom: 16px;"><code><br />
dbcc checkident('tbl_abc', RESEED, 1073741824)<br />
dbcc checkident('tbl_abc', RESEED)<br />
 </code></div>
<p>The first sets the seed value to 1073741824. However, the application might have previously failed over to the DR database and there may already be rows with identity columns having a value of 1073741824 and up. The second statement above correcly sets the seed to the next available value, if 1073741824 is already used.</p>
<p>The output of following SQL script will generate the pair DBCC statements for all tables in the database named &#8216;tbl_%&#8217;.</p>
<div style="background-color: #dfdfdf; padding-bottom: 16px;"><code><br />
select<br />
'dbcc checkident(''' + st.name + ''',RESEED,' + case when sc.system_type_id = 56 then '1073741824)' else '10737418240)' end<br />
from sys.tables st<br />
inner join sys.columns sc on sc.object_id = st.object_id<br />
where st.name like 'tbl_%'<br />
and sc.is_identity = 1</p>
<p>select<br />
'dbcc checkident(''' + st.name + ''',RESEED)'<br />
from sys.tables st<br />
inner join sys.columns sc on sc.object_id = st.object_id<br />
where st.name like 'tbl_%'<br />
and sc.is_identity = 1</p>
<p></code></div>
<p>
The output needs to be captured as text and cleaned up a little (remove headers, etc.), before being resumbitted as a SQL command. Note that if the table has many rows (over several hundred thousand), it could take a while for DBCC CHECKIDENT to complete.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.point41.com/index.php/2009/08/13/reseeding-identity-columns-for-replication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
