<?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; SQL Server</title>
	<atom:link href="http://www.point41.com/index.php/category/sql-server/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>
		<item>
		<title>Script to Re-index SQL Server Database</title>
		<link>http://www.point41.com/index.php/2009/08/09/script-to-re-index-sql-server-database/</link>
		<comments>http://www.point41.com/index.php/2009/08/09/script-to-re-index-sql-server-database/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 22:31:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.point41.com/?p=66</guid>
		<description><![CDATA[The following SQL Server script enumerates the tables in a database and calls DBBC REINDEX on each one (with a fill factor of 80%). It then calls sp_updatestats too update the index statistics.



USE &#60;database_name&#62;
DECLARE @TableName varchar(255) 
DECLARE TableCursor CURSOR FOR
SELECT table_name FROM information_schema.tables
WHERE table_type = &#8216;base table&#8217; 
OPEN TableCursor 
FETCH NEXT FROM TableCursor INTO @TableName
WHILE [...]]]></description>
			<content:encoded><![CDATA[<p>The following SQL Server script enumerates the tables in a database and calls DBBC REINDEX on each one (with a fill factor of 80%). It then calls sp_updatestats too update the index statistics.
</p>
<div style="background-color: #dfdfdf; padding: 1px 16px 1px 16px; font-family: courier; font-size:10px;">
<p>
USE <i>&lt;database_name&gt;</i></p>
<p>DECLARE @TableName varchar(255) </p>
<p>DECLARE TableCursor CURSOR FOR<br />
SELECT table_name FROM information_schema.tables<br />
WHERE table_type = &#8216;base table&#8217; </p>
<p>OPEN TableCursor </p>
<p>FETCH NEXT FROM TableCursor INTO @TableName<br />
WHILE @@FETCH_STATUS = 0<br />
BEGIN<br />
Print @TableName<br />
DBCC DBREINDEX(@TableName,&#8217; &#8216;,80)<br />
FETCH NEXT FROM TableCursor INTO @TableName<br />
END </p>
<p>CLOSE TableCursor </p>
<p>EXEC sp_updatestats
</p>
</div>
<p>To avoid your queries from running under non-optimal execution plans, the script should be run periodically, typically weekly or nightly, when the the database is not in use.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.point41.com/index.php/2009/08/09/script-to-re-index-sql-server-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Replicating large text / ntext fields in SQL Server 2005, 2008</title>
		<link>http://www.point41.com/index.php/2009/08/09/replicating-large-text-ntext-fields-in-sql-server-2005-2008/</link>
		<comments>http://www.point41.com/index.php/2009/08/09/replicating-large-text-ntext-fields-in-sql-server-2005-2008/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 15:24:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://www.point41.com/?p=37</guid>
		<description><![CDATA[Scenario &#8211; You have successfully setup replicated SQL Server databases with ntext fields and things were going fine initially. One day you get this error in the Event Log:

Server: Msg 7139, Level 16, State 1, Line 1
Length of text, ntext, or image data ([some number larger than 65536]) to be replicated exceeds configured maximum 65536.
The [...]]]></description>
			<content:encoded><![CDATA[<p><b>Scenario</b> &#8211; You have successfully setup replicated SQL Server databases with ntext fields and things were going fine initially. One day you get this error in the Event Log:</p>
<div style="background-color: #dfdfdf; padding: 1px 16px 1px 16px;">
<p style="font-family: Courier;font-size: 10pt;">Server: Msg 7139, Level 16, State 1, Line 1<br />
Length of text, ntext, or image data ([some number larger than 65536]) to be replicated exceeds configured maximum 65536.<br />
The statement has been terminated.</p>
</div>
<p>This is happeming because <b>Max Text Replication Size</b> by default is set to 65536 and SQL Server has attempted to replicate a row where the data in a column is greater than this size. From the SQL Server Management Studio you can view and change this setting under Server Properties | Advanced dialog.
</p>
<p><img src="http://www.point41.com/wp-content/uploads/2009/08/MaxTextReplSize.gif" alt="max text repl size" /></p>
<p>Increase this number to a higher value &#8211; up to <b>2147483647</b>. Make this change on all SQL Servers involved in the replication. If you don&#8217;t like UI stuff, you can also make this change via a SQL Query.</p>
<div style="background-color: #dfdfdf; padding: 1px 16px 1px 16px;font-family: Courier;font-size: 10pt;">
<p>
EXEC sp_configure &#8216;max text repl size&#8217;, 2147483647<br />
GO</p>
<p>RECONFIGURE<br />
GO </p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.point41.com/index.php/2009/08/09/replicating-large-text-ntext-fields-in-sql-server-2005-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
