<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SQL Premier Field Engineering</title><link>http://blogs.msdn.com/menzos/default.aspx</link><description /><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Index Maintenance across all Databases in an Instance</title><link>http://blogs.msdn.com/menzos/archive/2009/07/30/index-maintenance-across-all-databases-in-an-instance.aspx</link><pubDate>Thu, 30 Jul 2009 11:48:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9853073</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/menzos/comments/9853073.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=9853073</wfw:commentRss><description>&lt;p&gt;I needed to make a job for a customer that rebuilds all indexes in an instance that have a fragmentation of more than 10%. Usually we use a maintenance plan for rebuilding indexes. Although that is a fine approach is has one disadvantage: it rebuilds all indexes in a database regardless of the level of fragmentation. I do not like that because it takes up resources that are not necessary. It also takes far longer to complete than my job.
&lt;/p&gt;&lt;p&gt;As a basis I used the code from the BOL on the sys.dm_db_index_physical_stats page. Here you can find the following piece of code:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:green; font-family:Lucida Console"&gt;-- Ensure a USE &amp;lt;databasename&amp;gt; statement has been executed first.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;SET&lt;/span&gt;
			&lt;span style="color:blue"&gt;NOCOUNT&lt;/span&gt;
			&lt;span style="color:blue"&gt;ON&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @objectid &lt;span style="color:blue"&gt;int&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @indexid &lt;span style="color:blue"&gt;int&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @partitioncount &lt;span style="color:blue"&gt;bigint&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @schemaname &lt;span style="color:blue"&gt;nvarchar&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;130&lt;span style="color:gray"&gt;);&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @objectname &lt;span style="color:blue"&gt;nvarchar&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;130&lt;span style="color:gray"&gt;);&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @indexname &lt;span style="color:blue"&gt;nvarchar&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;130&lt;span style="color:gray"&gt;);&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @partitionnum &lt;span style="color:blue"&gt;bigint&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @partitions &lt;span style="color:blue"&gt;bigint&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @frag &lt;span style="color:blue"&gt;float&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @command &lt;span style="color:blue"&gt;nvarchar&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;4000&lt;span style="color:gray"&gt;);&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:green; font-family:Lucida Console"&gt;-- Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function 
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:green; font-family:Lucida Console"&gt;-- and convert object and index IDs to names.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue; font-family:Lucida Console"&gt;SELECT
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:fuchsia"&gt;object_id&lt;/span&gt;
			&lt;span style="color:blue"&gt;AS&lt;/span&gt; objectid&lt;span style="color:gray"&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;    index_id &lt;span style="color:blue"&gt;AS&lt;/span&gt; indexid&lt;span style="color:gray"&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;    partition_number &lt;span style="color:blue"&gt;AS&lt;/span&gt; partitionnum&lt;span style="color:gray"&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;    avg_fragmentation_in_percent &lt;span style="color:blue"&gt;AS&lt;/span&gt; frag
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;INTO&lt;/span&gt; #work_to_do
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;FROM&lt;/span&gt;
			&lt;span style="color:green"&gt;sys&lt;/span&gt;&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:green"&gt;dm_db_index_physical_stats&lt;/span&gt;&lt;span style="color:blue"&gt;
			&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;&lt;span style="color:fuchsia"&gt;DB_ID&lt;/span&gt;&lt;span style="color:gray"&gt;(),&lt;/span&gt;
			&lt;span style="color:gray"&gt;NULL,&lt;/span&gt;
			&lt;span style="color:gray"&gt;NULL&lt;/span&gt;
			&lt;span style="color:gray"&gt;,&lt;/span&gt;
			&lt;span style="color:gray"&gt;NULL,&lt;/span&gt;
			&lt;span style="color:red"&gt;'LIMITED'&lt;/span&gt;&lt;span style="color:gray"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;WHERE&lt;/span&gt; avg_fragmentation_in_percent &lt;span style="color:gray"&gt;&amp;gt;&lt;/span&gt; 10.0 &lt;span style="color:gray"&gt;AND&lt;/span&gt; index_id &lt;span style="color:gray"&gt;&amp;gt;&lt;/span&gt; 0&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:green; font-family:Lucida Console"&gt;-- Declare the cursor for the list of partitions to be processed.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt;
			&lt;span style="color:green"&gt;partitions&lt;/span&gt;
			&lt;span style="color:blue"&gt;CURSOR&lt;/span&gt;
			&lt;span style="color:blue"&gt;FOR&lt;/span&gt;
			&lt;span style="color:blue"&gt;SELECT&lt;/span&gt;
			&lt;span style="color:gray"&gt;*&lt;/span&gt;
			&lt;span style="color:blue"&gt;FROM&lt;/span&gt; #work_to_do&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:green; font-family:Lucida Console"&gt;-- Open the cursor.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;OPEN&lt;/span&gt;
			&lt;span style="color:green"&gt;partitions&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:green; font-family:Lucida Console"&gt;-- Loop through the partitions.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;WHILE &lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;1&lt;span style="color:gray"&gt;=&lt;/span&gt;1&lt;span style="color:gray"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;BEGIN&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;FETCH&lt;/span&gt;
			&lt;span style="color:blue"&gt;NEXT
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;FROM&lt;/span&gt;
			&lt;span style="color:green"&gt;partitions
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;INTO&lt;/span&gt; @objectid&lt;span style="color:gray"&gt;,&lt;/span&gt; @indexid&lt;span style="color:gray"&gt;,&lt;/span&gt; @partitionnum&lt;span style="color:gray"&gt;,&lt;/span&gt; @frag&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;IF&lt;/span&gt;
			&lt;span style="color:fuchsia"&gt;@@FETCH_STATUS&lt;/span&gt;
			&lt;span style="color:gray"&gt;&amp;lt;&lt;/span&gt; 0 &lt;span style="color:blue"&gt;BREAK&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;SELECT&lt;/span&gt; @objectname &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:fuchsia"&gt;QUOTENAME&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;o&lt;span style="color:gray"&gt;.&lt;/span&gt;name&lt;span style="color:gray"&gt;),&lt;/span&gt; @schemaname &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:fuchsia"&gt;QUOTENAME&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;s&lt;span style="color:gray"&gt;.&lt;/span&gt;name&lt;span style="color:gray"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;FROM&lt;/span&gt;
			&lt;span style="color:green"&gt;sys&lt;/span&gt;&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:green"&gt;objects&lt;/span&gt;
			&lt;span style="color:blue"&gt;AS&lt;/span&gt; o
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:gray"&gt;JOIN&lt;/span&gt;
			&lt;span style="color:green"&gt;sys&lt;/span&gt;&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:green"&gt;schemas&lt;/span&gt;
			&lt;span style="color:blue"&gt;as&lt;/span&gt; s &lt;span style="color:blue"&gt;ON&lt;/span&gt; s&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:fuchsia"&gt;schema_id&lt;/span&gt;
			&lt;span style="color:gray"&gt;=&lt;/span&gt; o&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:fuchsia"&gt;schema_id
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;WHERE&lt;/span&gt; o&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:fuchsia"&gt;object_id&lt;/span&gt;
			&lt;span style="color:gray"&gt;=&lt;/span&gt; @objectid&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;SELECT&lt;/span&gt; @indexname &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:fuchsia"&gt;QUOTENAME&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;name&lt;span style="color:gray"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;FROM&lt;/span&gt;
			&lt;span style="color:green"&gt;sys&lt;/span&gt;&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:green"&gt;indexes
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;WHERE&lt;/span&gt;
			&lt;span style="color:fuchsia"&gt;object_id&lt;/span&gt;
			&lt;span style="color:gray"&gt;=&lt;/span&gt; @objectid &lt;span style="color:gray"&gt;AND&lt;/span&gt; index_id &lt;span style="color:gray"&gt;=&lt;/span&gt; @indexid&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;SELECT&lt;/span&gt; @partitioncount &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:fuchsia"&gt;count&lt;/span&gt;&lt;span style="color:blue"&gt;
			&lt;/span&gt;&lt;span style="color:gray"&gt;(*)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;FROM&lt;/span&gt;
			&lt;span style="color:green"&gt;sys&lt;/span&gt;&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:green"&gt;partitions
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;WHERE&lt;/span&gt;
			&lt;span style="color:fuchsia"&gt;object_id&lt;/span&gt;
			&lt;span style="color:gray"&gt;=&lt;/span&gt; @objectid &lt;span style="color:gray"&gt;AND&lt;/span&gt; index_id &lt;span style="color:gray"&gt;=&lt;/span&gt; @indexid&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:green; font-family:Lucida Console"&gt;-- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;IF&lt;/span&gt; @frag &lt;span style="color:gray"&gt;&amp;lt;&lt;/span&gt; 30.0
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;SET&lt;/span&gt; @command &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:red"&gt;N'ALTER INDEX '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @indexname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' ON '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @schemaname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N'.'&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @objectname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' REORGANIZE'&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;IF&lt;/span&gt; @frag &lt;span style="color:gray"&gt;&amp;gt;=&lt;/span&gt; 30.0
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;SET&lt;/span&gt; @command &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:red"&gt;N'ALTER INDEX '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @indexname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' ON '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @schemaname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N'.'&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @objectname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' REBUILD'&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;IF&lt;/span&gt; @partitioncount &lt;span style="color:gray"&gt;&amp;gt;&lt;/span&gt; 1
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;SET&lt;/span&gt; @command &lt;span style="color:gray"&gt;=&lt;/span&gt; @command &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' PARTITION='&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:fuchsia"&gt;CAST&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;@partitionnum &lt;span style="color:blue"&gt;AS&lt;/span&gt;
			&lt;span style="color:blue"&gt;nvarchar&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;10&lt;span style="color:gray"&gt;));
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;EXEC &lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;@command&lt;span style="color:gray"&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;PRINT&lt;/span&gt;
			&lt;span style="color:red"&gt;N'Executed: '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @command&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;
			&lt;span style="color:blue"&gt;END&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:green; font-family:Lucida Console"&gt;-- Close and deallocate the cursor.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;CLOSE&lt;/span&gt;
			&lt;span style="color:green"&gt;partitions&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DEALLOCATE&lt;/span&gt;
			&lt;span style="color:green"&gt;partitions&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:green; font-family:Lucida Console"&gt;-- Drop the temporary table.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DROP&lt;/span&gt;
			&lt;span style="color:blue"&gt;TABLE&lt;/span&gt; #work_to_do&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue; font-family:Lucida Console"&gt;GO
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;This code runs in only one database so I needed to add a cursor to run through all databases on the instance. The code above also differentiates partitions in an index. I didn't need that, so I stripped it out. I als stripped out the reorganize code. On an enterprise edition I prefer online rebuilds.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;I came up with the following code:
&lt;/p&gt;&lt;p&gt;&lt;span style="color:green; font-family:Lucida Console"&gt;-- Index rebuild online on all databases on an instance
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;SET&lt;/span&gt;
			&lt;span style="color:blue"&gt;NOCOUNT&lt;/span&gt;
			&lt;span style="color:blue"&gt;ON
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @db  &lt;span style="color:blue"&gt;as&lt;/span&gt;
			&lt;span style="color:blue"&gt;sysname&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @db_id &lt;span style="color:blue"&gt;as&lt;/span&gt;
			&lt;span style="color:blue"&gt;int&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @cmd1 &lt;span style="color:blue"&gt;as&lt;/span&gt;
			&lt;span style="color:blue"&gt;nvarchar&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;1000&lt;span style="color:gray"&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @objectid &lt;span style="color:blue"&gt;int&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @indexid &lt;span style="color:blue"&gt;int&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @schemaname &lt;span style="color:blue"&gt;nvarchar&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;130&lt;span style="color:gray"&gt;);&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @objectname &lt;span style="color:blue"&gt;nvarchar&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;130&lt;span style="color:gray"&gt;);&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @indexname &lt;span style="color:blue"&gt;nvarchar&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;130&lt;span style="color:gray"&gt;);&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @partitionnum &lt;span style="color:blue"&gt;bigint&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @command &lt;span style="color:blue"&gt;nvarchar&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;4000&lt;span style="color:gray"&gt;);&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @errornumber &lt;span style="color:blue"&gt;int&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;    
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; DatabaseList &lt;span style="color:blue"&gt;CURSOR&lt;/span&gt;
			&lt;span style="color:blue"&gt;FAST_FORWARD&lt;/span&gt;
			&lt;span style="color:blue"&gt;FOR&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;SELECT&lt;/span&gt; [name]&lt;span style="color:gray"&gt;,&lt;/span&gt; [database_id] &lt;span style="color:blue"&gt;FROM&lt;/span&gt;
			&lt;span style="color:green"&gt;sys&lt;/span&gt;&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:green"&gt;databases
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;WHERE&lt;/span&gt; [name] &lt;span style="color:gray"&gt;NOT&lt;/span&gt;
			&lt;span style="color:gray"&gt;IN&lt;/span&gt;&lt;span style="color:blue"&gt;
			&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;&lt;span style="color:red"&gt;'tempdb'&lt;/span&gt;&lt;span style="color:gray"&gt;,&lt;/span&gt;&lt;span style="color:red"&gt;'model'&lt;/span&gt;&lt;span style="color:gray"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:gray"&gt;AND&lt;/span&gt; state_desc &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:red"&gt;'ONLINE'
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;ORDER&lt;/span&gt;
			&lt;span style="color:blue"&gt;BY&lt;/span&gt; [name]
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;OPEN&lt;/span&gt; DatabaseList
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;    &lt;span style="color:blue"&gt;FETCH&lt;/span&gt;
			&lt;span style="color:blue"&gt;NEXT&lt;/span&gt;
			&lt;span style="color:blue"&gt;FROM&lt;/span&gt; DatabaseList &lt;span style="color:blue"&gt;INTO&lt;/span&gt; @db&lt;span style="color:gray"&gt;,&lt;/span&gt; @db_id
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;        &lt;span style="color:blue"&gt;WHILE &lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;&lt;span style="color:fuchsia"&gt;@@fetch_status&lt;/span&gt;
			&lt;span style="color:gray"&gt;=&lt;/span&gt; 0&lt;span style="color:gray"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;            &lt;span style="color:blue"&gt;BEGIN
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:blue"&gt;PRINT&lt;/span&gt;
			&lt;span style="color:red"&gt;N' '&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:blue"&gt;PRINT&lt;/span&gt;
			&lt;span style="color:red"&gt;N'Database: '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @db&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:green"&gt;-- Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function 
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:green"&gt;-- and convert object and index IDs to names.
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:blue"&gt;EXEC &lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;&lt;span style="color:red"&gt;'USE '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @db &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;';
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                SELECT    p.object_id AS objectid
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                ,        p.index_id AS indexid
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                ,        o.name AS objectName
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                ,        s.name AS schemaName
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                ,        i.name AS indexName
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                INTO ##work_to_do
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:red"&gt;                FROM sys.dm_db_index_physical_stats ('&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @DB_ID &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;', NULL, NULL , NULL, ''LIMITED'') AS p
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                    INNER JOIN sys.objects as o
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                        ON p.object_id = o.object_id 
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                    INNER JOIN sys.schemas as s
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                        ON s.schema_id = o.schema_id 
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                    INNER JOIN sys.indexes i
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                        ON p.object_id = i.object_id 
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                        AND i.index_id = p.index_id 
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                WHERE p.avg_fragmentation_in_percent &amp;gt; 10.0 AND p.index_id &amp;gt; 0
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                AND p.index_id &amp;lt; 25000
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:red"&gt;                AND p.page_count &amp;gt; 1000;'&lt;/span&gt;&lt;span style="color:gray"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:green"&gt;-- Declare the cursor for the list of fragmented indexes to be processed.
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; FragmentedIndexes &lt;span style="color:blue"&gt;CURSOR&lt;/span&gt;
			&lt;span style="color:blue"&gt;FOR&lt;/span&gt;
			&lt;span style="color:blue"&gt;SELECT&lt;/span&gt;
			&lt;span style="color:gray"&gt;*&lt;/span&gt;
			&lt;span style="color:blue"&gt;FROM&lt;/span&gt; ##work_to_do&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:green"&gt;-- Open the cursor.
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:blue"&gt;OPEN&lt;/span&gt; FragmentedIndexes&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:green"&gt;-- Loop through the indexes.
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:blue"&gt;WHILE &lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;1&lt;span style="color:gray"&gt;=&lt;/span&gt;1&lt;span style="color:gray"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                    &lt;span style="color:blue"&gt;BEGIN&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                        &lt;span style="color:blue"&gt;FETCH&lt;/span&gt;
			&lt;span style="color:blue"&gt;NEXT
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                           &lt;span style="color:blue"&gt;FROM&lt;/span&gt; FragmentedIndexes
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                           &lt;span style="color:blue"&gt;INTO&lt;/span&gt; @objectid&lt;span style="color:gray"&gt;,&lt;/span&gt; @indexid&lt;span style="color:gray"&gt;,&lt;/span&gt; @objectname&lt;span style="color:gray"&gt;,&lt;/span&gt; @schemaname&lt;span style="color:gray"&gt;,&lt;/span&gt; @indexname&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                        &lt;span style="color:blue"&gt;IF&lt;/span&gt;
			&lt;span style="color:fuchsia"&gt;@@FETCH_STATUS&lt;/span&gt;
			&lt;span style="color:gray"&gt;&amp;lt;&lt;/span&gt; 0 &lt;span style="color:blue"&gt;BREAK&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                        &lt;span style="color:green"&gt;-- Check for invalid datatypes for online rebuild
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                        &lt;span style="color:green"&gt;-- If those datatypes are found, do normal rebuild
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                        &lt;span style="color:blue"&gt;BEGIN&lt;/span&gt;
			&lt;span style="color:blue"&gt;TRY
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                            &lt;span style="color:blue"&gt;SET&lt;/span&gt; @command &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:red"&gt;N'USE '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @db &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;'; ALTER INDEX '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @indexname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' ON '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @schemaname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N'.'&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @objectname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' REBUILD WITH (ONLINE=ON)'&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                            &lt;span style="color:blue"&gt;EXEC &lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;@command&lt;span style="color:gray"&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                            &lt;span style="color:blue"&gt;PRINT&lt;/span&gt;
			&lt;span style="color:red"&gt;N'Executed: '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @command&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                        &lt;span style="color:blue"&gt;END&lt;/span&gt;
			&lt;span style="color:blue"&gt;TRY
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                        &lt;span style="color:blue"&gt;BEGIN&lt;/span&gt;
			&lt;span style="color:blue"&gt;CATCH
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                            &lt;span style="color:blue"&gt;SELECT&lt;/span&gt; @errornumber &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:fuchsia"&gt;ERROR_NUMBER&lt;/span&gt;&lt;span style="color:gray"&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                            &lt;span style="color:blue"&gt;IF&lt;/span&gt; @errornumber &lt;span style="color:gray"&gt;=&lt;/span&gt; 2725
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                                &lt;span style="color:blue"&gt;BEGIN
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                                    &lt;span style="color:blue"&gt;SET&lt;/span&gt; @command &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:red"&gt;N'USE '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @db &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;'; ALTER INDEX '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @indexname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' ON '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @schemaname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N'.'&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @objectname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' REBUILD'&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                                    &lt;span style="color:blue"&gt;EXEC &lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;@command&lt;span style="color:gray"&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                                    &lt;span style="color:blue"&gt;PRINT&lt;/span&gt;
			&lt;span style="color:red"&gt;N'Executed: '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @command&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                                &lt;span style="color:blue"&gt;END
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                        &lt;span style="color:blue"&gt;END&lt;/span&gt;
			&lt;span style="color:blue"&gt;CATCH
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                        
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                    &lt;span style="color:blue"&gt;END&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:green"&gt;-- Close and deallocate the cursor.
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:blue"&gt;CLOSE&lt;/span&gt; FragmentedIndexes&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:blue"&gt;DEALLOCATE&lt;/span&gt; FragmentedIndexes&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:green"&gt;-- Drop the temporary table.
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:blue"&gt;DROP&lt;/span&gt;
			&lt;span style="color:blue"&gt;TABLE&lt;/span&gt; ##work_to_do&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;                &lt;span style="color:blue"&gt;FETCH&lt;/span&gt;
			&lt;span style="color:blue"&gt;NEXT&lt;/span&gt;
			&lt;span style="color:blue"&gt;FROM&lt;/span&gt; DatabaseList &lt;span style="color:blue"&gt;INTO&lt;/span&gt; @db&lt;span style="color:gray"&gt;,&lt;/span&gt; @db_id
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;            &lt;span style="color:blue"&gt;END
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;            
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;CLOSE&lt;/span&gt; DatabaseList&lt;span style="color:gray"&gt;;&lt;/span&gt;            
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DEALLOCATE&lt;/span&gt; DatabaseList&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;h2&gt;Explanation of the code:
&lt;/h2&gt;&lt;p&gt;I start with creating a cursor for all databases on the instance:
&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; DatabaseList &lt;span style="color:blue"&gt;CURSOR&lt;/span&gt;
			&lt;span style="color:blue"&gt;FAST_FORWARD&lt;/span&gt;
			&lt;span style="color:blue"&gt;FOR&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;SELECT&lt;/span&gt; [name]&lt;span style="color:gray"&gt;,&lt;/span&gt; [database_id] &lt;span style="color:blue"&gt;FROM&lt;/span&gt;
			&lt;span style="color:green"&gt;sys&lt;/span&gt;&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:green"&gt;databases
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;WHERE&lt;/span&gt; [name] &lt;span style="color:gray"&gt;NOT&lt;/span&gt;
			&lt;span style="color:gray"&gt;IN&lt;/span&gt;&lt;span style="color:blue"&gt;
			&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;&lt;span style="color:red"&gt;'tempdb'&lt;/span&gt;&lt;span style="color:gray"&gt;,&lt;/span&gt;&lt;span style="color:red"&gt;'model'&lt;/span&gt;&lt;span style="color:gray"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:gray"&gt;AND&lt;/span&gt; state_desc &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:red"&gt;'ONLINE'
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;ORDER&lt;/span&gt;
			&lt;span style="color:blue"&gt;BY&lt;/span&gt; [name]
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;Of course I exclude the tempdb and the model database. On model I don't expect indexes and tempdb is obvious.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;After that I open the database cursor and start processing the databases one at the time.
&lt;/p&gt;&lt;p&gt;First I collect the fragmented indexes:
&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;EXEC &lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;&lt;span style="color:red"&gt;'USE '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @db &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;';
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                SELECT    p.object_id AS objectid
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                ,        p.index_id AS indexid
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                ,        o.name AS objectName
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                ,        s.name AS schemaName
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                ,        i.name AS indexName
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                INTO ##work_to_do
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:red"&gt;                FROM sys.dm_db_index_physical_stats ('&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @DB_ID &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;', NULL, NULL , NULL, ''LIMITED'') AS p
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                    INNER JOIN sys.objects as o
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                        ON p.object_id = o.object_id 
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                    INNER JOIN sys.schemas as s
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                        ON s.schema_id = o.schema_id 
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                    INNER JOIN sys.indexes i
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                        ON p.object_id = i.object_id 
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                        AND i.index_id = p.index_id 
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                WHERE p.avg_fragmentation_in_percent &amp;gt; 10.0 AND p.index_id &amp;gt; 0
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Lucida Console"&gt;                AND p.index_id &amp;lt; 25000
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:red"&gt;                AND p.page_count &amp;gt; 1000;'&lt;/span&gt;&lt;span style="color:gray"&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;I use sys.dm_db_index_physical_stats for finding the fragmented indexes. In order to get the name, schemaname and objectname I join the sys.dm_db_index_physical_stats with sys.objects, sys.schemas and sys.indexes.
&lt;/p&gt;&lt;p&gt;The result I insert in a &lt;strong&gt;global&lt;/strong&gt; temp table. Global because the exec statement runs on another thread so I can't use a local temp table. I can't see it in the rest of the procedure if I did that.
&lt;/p&gt;&lt;p&gt;In the where clause I check on fragmentation over 10%. I also check if the index_id is smaller than 25000. If it is over it is either a XML index or a Spatial index. 
&lt;/p&gt;&lt;p&gt;The last check I do is on page_count of the index. SQL Server will not rebuild indexes that are smaller than 1000 pages(8MB). It will not do seeks on such small indexes, only scans. So fragmentation won't matter.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;With the fragmented indexes in the temp table I can build the statement necessary to defragment the index:
&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;BEGIN&lt;/span&gt;
			&lt;span style="color:blue"&gt;TRY
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 35pt"&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;SET&lt;/span&gt; @command &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:red"&gt;N'USE '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @db &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;'; ALTER INDEX '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @indexname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' ON '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @schemaname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N'.'&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @objectname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' REBUILD WITH (ONLINE=ON)'&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;    &lt;span style="color:blue"&gt;EXEC &lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;@command&lt;span style="color:gray"&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;    &lt;span style="color:blue"&gt;PRINT&lt;/span&gt;
			&lt;span style="color:red"&gt;N'Executed: '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @command&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;END&lt;/span&gt;
			&lt;span style="color:blue"&gt;TRY
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;BEGIN&lt;/span&gt;
			&lt;span style="color:blue"&gt;CATCH
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;    &lt;span style="color:blue"&gt;SELECT&lt;/span&gt; @errornumber &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:fuchsia"&gt;ERROR_NUMBER&lt;/span&gt;&lt;span style="color:gray"&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;    &lt;span style="color:blue"&gt;IF&lt;/span&gt; @errornumber &lt;span style="color:gray"&gt;=&lt;/span&gt; 2725
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;        &lt;span style="color:blue"&gt;BEGIN
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 106pt"&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;SET&lt;/span&gt; @command &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:red"&gt;N'USE '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @db &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;'; ALTER INDEX '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @indexname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' ON '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @schemaname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N'.'&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @objectname &lt;span style="color:gray"&gt;+&lt;/span&gt;
			&lt;span style="color:red"&gt;N' REBUILD'&lt;/span&gt;&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;            &lt;span style="color:blue"&gt;EXEC &lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;@command&lt;span style="color:gray"&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;            &lt;span style="color:blue"&gt;PRINT&lt;/span&gt;
			&lt;span style="color:red"&gt;N'Executed: '&lt;/span&gt;
			&lt;span style="color:gray"&gt;+&lt;/span&gt; @command&lt;span style="color:gray"&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;        &lt;span style="color:blue"&gt;END
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Lucida Console"&gt;&lt;span style="color:blue"&gt;END&lt;/span&gt;
			&lt;span style="color:blue"&gt;CATCH
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;Since the customer had an enterprise edition I do the rebuild online. That was necessary since the customer had a 24*7 system. Online rebuilds do have limitations: you can't rebuild an index online if the index contains blob datatypes like text, ntext, xml, varchar(max), nvarchar(max), image, etc….
&lt;/p&gt;&lt;p&gt;If those datatypes are used in the underlying table you can't do an online rebuild either. That's why I check for errornumber 2725. If that's returned I build the rebuild statement without the online option.
&lt;/p&gt;&lt;p&gt;The rest of the code is cleanup code. We should drop the temp table and close and deallocate the cursors(very important!)
&lt;/p&gt;&lt;p&gt;I want to add some more errorhandling later on. The temptable should always be dropped. But that's the only thing missing in the code.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;If you have any remarks on my code, I'll be delighted to hear it!
&lt;/p&gt;&lt;p&gt;Regards,
&lt;/p&gt;&lt;p&gt;Menzo&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9853073" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/menzos/archive/tags/SQL+Server+Concepts/default.aspx">SQL Server Concepts</category><category domain="http://blogs.msdn.com/menzos/archive/tags/Indexing/default.aspx">Indexing</category></item><item><title>Reading PerfMon Counters in T-SQL with CLR</title><link>http://blogs.msdn.com/menzos/archive/2009/03/25/reading-perfmon-counters-in-t-sql-with-clr.aspx</link><pubDate>Wed, 25 Mar 2009 16:08:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9507364</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/menzos/comments/9507364.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=9507364</wfw:commentRss><description>&lt;p&gt;A collegue of mine asked me if it is possible to read out PerfMon counters that are not in the &lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:lime"&gt;sys&lt;/span&gt;&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:lime"&gt;dm_os_performance_counters &lt;/span&gt;&lt;/span&gt;DMV. I searched everywhere but the only possibility I could think of is by using a CLR user defined function.
&lt;/p&gt;&lt;p&gt;So I wrote an assembly (I'm still a VB programmer, I never bothered to learn C#. And I'm never going to as long as VB.Net exists &lt;span style="font-family:Wingdings"&gt;J&lt;/span&gt; ):
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;The code is fairly simple:
&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;Imports&lt;/span&gt; System
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;Imports&lt;/span&gt; System.Data
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;Imports&lt;/span&gt; System.Data.SqlClient
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;Imports&lt;/span&gt; System.Data.SqlTypes
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;Imports&lt;/span&gt; Microsoft.SqlServer.Server
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;Partial&lt;/span&gt;
			&lt;span style="color:blue"&gt;Public&lt;/span&gt;
			&lt;span style="color:blue"&gt;Class&lt;/span&gt; UserDefinedFunctions
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;    &amp;lt;Microsoft.SqlServer.Server.SqlFunction()&amp;gt; _
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;    &lt;span style="color:blue"&gt;Public&lt;/span&gt;
			&lt;span style="color:blue"&gt;Shared&lt;/span&gt;
			&lt;span style="color:blue"&gt;Function&lt;/span&gt; ReadPerfMonCounter(&lt;span style="color:blue"&gt;ByVal&lt;/span&gt; Category &lt;span style="color:blue"&gt;As&lt;/span&gt;
			&lt;span style="color:blue"&gt;String&lt;/span&gt;, &lt;span style="color:blue"&gt;ByVal&lt;/span&gt; Counter &lt;span style="color:blue"&gt;As&lt;/span&gt;
			&lt;span style="color:blue"&gt;String&lt;/span&gt;, &lt;span style="color:blue"&gt;ByVal&lt;/span&gt; Instance &lt;span style="color:blue"&gt;As&lt;/span&gt;
			&lt;span style="color:blue"&gt;String&lt;/span&gt;) &lt;span style="color:blue"&gt;As&lt;/span&gt; SqlString
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;        &lt;span style="color:green"&gt;'******************************************************************************
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;        &lt;span style="color:green"&gt;'* Disclaimer: I'm a lousy programmer!!!!                                     *
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;        &lt;span style="color:green"&gt;'* ****************************************************************************
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;        &lt;span style="color:blue"&gt;Dim&lt;/span&gt; sTemp &lt;span style="color:blue"&gt;As&lt;/span&gt;
			&lt;span style="color:blue"&gt;String
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;        &lt;span style="color:blue"&gt;Try
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;            &lt;span style="color:blue"&gt;Dim&lt;/span&gt; perfCounter &lt;span style="color:blue"&gt;As&lt;/span&gt;
			&lt;span style="color:blue"&gt;New&lt;/span&gt; System.Diagnostics.PerformanceCounter(Category, Counter, Instance, &lt;span style="color:#a31515"&gt;"localhost"&lt;/span&gt;)
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;            &lt;span style="color:green"&gt;'Somehow it won't return a result on the first NextValue call.
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;            &lt;span style="color:green"&gt;'Never mind: we just call it 2 times :).
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;            sTemp = perfCounter.NextValue().ToString
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;            &lt;span style="color:green"&gt;'And wait a little while, but now she's ready to go.
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;            Threading.Thread.Sleep(1000)
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;            &lt;span style="color:blue"&gt;Return&lt;/span&gt;
			&lt;span style="color:blue"&gt;New&lt;/span&gt; SqlString(perfCounter.NextValue.ToString)
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;        &lt;span style="color:blue"&gt;Catch&lt;/span&gt; ex &lt;span style="color:blue"&gt;As&lt;/span&gt; Exception
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;            &lt;span style="color:blue"&gt;Return&lt;/span&gt;
			&lt;span style="color:blue"&gt;New&lt;/span&gt; SqlString(ex.Message)
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;        &lt;span style="color:blue"&gt;End&lt;/span&gt;
			&lt;span style="color:blue"&gt;Try
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;    &lt;span style="color:blue"&gt;End&lt;/span&gt;
			&lt;span style="color:blue"&gt;Function
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;End&lt;/span&gt;
			&lt;span style="color:blue"&gt;Class
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;The only problem I had was the problem that the NextValue method of the perfCounter didn't return anything but 0 on its first call. That's why I call it 2 times now.
&lt;/p&gt;&lt;p&gt;Once compiled you can deploy this assembly on your server. If you don't have .Net but want the assembly, just mail me. 
&lt;/p&gt;&lt;p&gt;I deployed it to the AdventureWorksTest database, which is a copy of the regular AdventureWorks database.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;If you deploy it make sure your database has TRUSTWORTHY ON because I didn't sign the assembly. This assembly needs unrestricted access. That's unsafe on SQL 2005.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;When you have done all that you can use a select statement like:
&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;SELECT&lt;/span&gt;
			&lt;span style="color:teal"&gt;AdventureWorksTest&lt;/span&gt;&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:teal"&gt;dbo&lt;/span&gt;&lt;span style="color:gray"&gt;.&lt;/span&gt;&lt;span style="color:teal"&gt;ReadPerfMonCounter&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;&lt;span style="color:red"&gt;'PhysicalDisk'&lt;/span&gt;&lt;span style="color:gray"&gt;,&lt;/span&gt;&lt;span style="color:red"&gt;'% Idle Time'&lt;/span&gt;&lt;span style="color:gray"&gt;,&lt;/span&gt;&lt;span style="color:red"&gt;'0 C: D: E:'&lt;/span&gt;&lt;span style="color:gray"&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;That's all. Error handling could be better by the way. When you make a typo in your counter you'll wait forever instead of getting an error.
&lt;/p&gt;&lt;p&gt;If you don't like the unsafe setting on the assembly you could transfer that part to a class library and reference it from your .NET stored procedure. Plenty opportunities left….
&lt;/p&gt;&lt;p&gt;I didn't bother on execution context and all that. On my machine the service account is a local admin. You might need some impersonification code if not.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;Regards,
&lt;/p&gt;&lt;p&gt;Menzo
&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9507364" width="1" height="1"&gt;</description></item><item><title>Back again!</title><link>http://blogs.msdn.com/menzos/archive/2009/03/25/back-again.aspx</link><pubDate>Wed, 25 Mar 2009 13:37:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9507117</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/menzos/comments/9507117.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=9507117</wfw:commentRss><description>&lt;p&gt;I didn't blog for a long time. For various reasons but the main reason is because I have been quite busy.
&lt;/p&gt;&lt;p&gt;So what did I do the last half year?
&lt;/p&gt;&lt;ul style="margin-left: 54pt"&gt;&lt;li&gt;A lot of Performance Tuning and Optimization Workshops Plus on SQL 2005. This is in my opinion one of the best workshops we offer. There's really good stuff in it on Windows and SQL internals and architecture. I delivered the workshops to both DBA's and developers and both groups were enthusiastic about the workshop.
&lt;/li&gt;&lt;li&gt;A lot of SQLRAP's. RAP's are fun to do because you never know what to expect. You see quite a wide array of problems on SQL and it's never the same.
&lt;/li&gt;&lt;li&gt;Ramping up on SQL 2008. Although SQL 2008 uses more or less the same engine as SQL 2005 it takes me a lot of time. The new features are not all that easy or could be used (and are!) in complex scenarios. 
&lt;/li&gt;&lt;li&gt;Ramping up on the SQL Server BI stack. I was proficient in SSIS and SSRS, but I didn't know much about SSAS. That's going to change this year. A lot of our customers use SSAS or want to use it, and I want to be able to help them with that. Since SSAS is completely different from the engine this will take time…
&lt;/li&gt;&lt;li&gt;Next to all SQL stuff I do like to spend some time with my family every now and then……
&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;Despite the economic climate, my agenda is still more than full. But no complains about that&lt;span style="font-family:Wingdings"&gt;J&lt;/span&gt;.
&lt;/p&gt;&lt;p&gt;I &lt;span style="text-decoration:underline"&gt;&lt;strong&gt;will &lt;/strong&gt;&lt;/span&gt;blog more in the future.
&lt;/p&gt;&lt;p&gt;
 &lt;/p&gt;&lt;p&gt;Menzo&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9507117" width="1" height="1"&gt;</description></item><item><title>TechEd IT Proffesionals 2008 EMEA Day 2</title><link>http://blogs.msdn.com/menzos/archive/2008/11/04/teched-it-proffesionals-2008-emea-day-2.aspx</link><pubDate>Wed, 05 Nov 2008 00:26:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9041538</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/menzos/comments/9041538.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=9041538</wfw:commentRss><description>&lt;p&gt;Today I visited 4 presentations: 
&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Next generation analysis services, Gemini
&lt;/li&gt;&lt;li&gt;Windows 2008 Clustering
&lt;/li&gt;&lt;li&gt;Policy Based Management
&lt;/li&gt;&lt;li&gt;&lt;div&gt;Migrating to SQL Server.
&lt;/div&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;The best session was on Policy Based Management by Buck Woody. He made PBM look very simple and showed cool things like using SQL to get values in conditions. And he showed some central management stuff. With central management you can evaluate policies over several instances.
&lt;/p&gt;&lt;p&gt;He also demonstrated PBM with powershell. I don't know much about powershell but I should learn it. It looked powerful and with powershell you can evaluate your policies on SQL 2005 and SQL 2000 as well. Partly because not everything is supported but that doesn't matter that much.
&lt;/p&gt;&lt;p&gt;The Gemini session was also remarkable. Handling 20.000.000 rows in seconds in Excel was quite impressive. I'm not really into Analysis Services but this looked great.
&lt;/p&gt;&lt;p&gt;The migrating session had an interesting session on the SQL Server Migration Assistant Application. They did a migration from Oracle to SQL Server. I had some experience with the tool but they showed features I had never used. PL/SQL conversion is not without problems: build in packages are extensively used by Oracle developers and they are not easily migrated by the tool. 
&lt;/p&gt;&lt;p&gt;The most important thing I learned in the clustering session was the validation tool. Every time you change something on the cluster: run the validation tool and you get a report whether or not your cluster is still in mint condition. Clustering is a lot simpler in Windows 2008. I do meet a lot of people that are scared of clustering. I hope Windows 2008 makes them more comfortable with clustering. 
&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;Tomorrow I don't have to man the booth so I can go to sessions all day.
&lt;/p&gt;&lt;p style="margin-left: 27pt"&gt; 
 &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9041538" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/menzos/archive/tags/TechEd+2008/default.aspx">TechEd 2008</category></item><item><title>TechEd IT Proffesionals 2008 EMEA Day 1</title><link>http://blogs.msdn.com/menzos/archive/2008/11/04/teched-it-proffesionals-2008-emea-day-1.aspx</link><pubDate>Wed, 05 Nov 2008 00:04:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9041452</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/menzos/comments/9041452.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=9041452</wfw:commentRss><description>&lt;p&gt;For the next two weeks I will be attending Teched in the beautiful city of Barcelona. I attended 2 sessions today. The first one was about the high availability tools of SQL Server 2008. It was presented by Andrew Fryer, a technology evangelist of Microsoft UK. I saw a good presentation that emphasized on mirroring but also covered clustering and log-shipping. Personally I prefer clustering over mirroring. With mirroring there is always more administration with logins and other stuff that isn't mirrored. On the other hand: the transparent failover capabilities of mirroring are impressive.
&lt;/p&gt;&lt;p&gt;Andrew also covered combinations of mirroring and log-shipping. I did see this at a few client sites and it provides extra availability at a low cost.
&lt;/p&gt;&lt;p&gt;The second session was on storage in SQL 2008. It emphasized on the compression features of SQL 2008. He had a demo on back-up compression and he did some compression on tables. What impressed me was that on a partitioned table you can choose different compression methods on all partitions.  
&lt;/p&gt;&lt;p&gt;There are two levels of compression:
&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Row level: this one uses variable lengths for fixed length data types.&lt;span style="font-family:Times New Roman; font-size:12pt"&gt;
			&lt;/span&gt;&lt;/li&gt;&lt;li&gt;Page level: this one compresses entire datapages by looking for data that is on the page multiple times. This data is only stored once. In rows where the data originally was the data is replaced by a pointer which takes up less space. Page level compression includes row level by the way. Page level compression will save you more space.
&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;On the downside: this feature is only available in the enterprise and developer edition.
&lt;/p&gt;&lt;p&gt;He also touched sparse column support briefly. Personally I like this feature a lot. You can create columns that contain lots of nulls as sparse. SQL will convert all sparse columns on a row to a XML string.  But clients don't notice anything and you can save a lot of space.
&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;Tomorrow I'm have to man the Ask The Expert booth in the morning. In the afternoon I'm going to some sessions.
&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9041452" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/menzos/archive/tags/TechEd+2008/default.aspx">TechEd 2008</category></item><item><title>The SQL 2008 Resource Governor</title><link>http://blogs.msdn.com/menzos/archive/2008/07/02/the-sql-2008-resource-governor.aspx</link><pubDate>Wed, 02 Jul 2008 23:30:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8682150</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/menzos/comments/8682150.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=8682150</wfw:commentRss><description>&lt;p style="margin-left: 1pt"&gt;The client I work for today asked me to come up with a quick overview of the Resource Governor. I'm in luck these days because I have lots of assignments on SQL 2008. Usually PFE's deal with production environments. More often then not  this is still SQL 2000. I think 80% of my assignments are on SQL 2005 and 20% are on SQL 2000. 
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;I have no previous experience with the Resource Governor. I only saw it in action during a presentation of Bob Beauchemin of SQLSkills on TechEd 2007 in Barcelona. Excellent presentation by the way. Presentations of Bob Beauchemin are always excellent.
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;strong&gt;What is it?
&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;It's a mechanism to limit resource consumption on incoming requests.
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;strong&gt;How does it work?
&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;It's got 3 components
&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;div&gt;Resource Pools
&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;div&gt;A resource pool represents the physical resources of a server. You can  set:
&lt;/div&gt;&lt;ul&gt;&lt;li&gt;MIN and MAX for CPU
&lt;/li&gt;&lt;li&gt;MIN and MAX for memory
&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;The sum of the MIN values across all pools cannot exceed 100.
&lt;/li&gt;&lt;li&gt;There's an Internal Pool. SQL Server needs this one for its own processes. You cannot change this pool.
&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;div&gt;Workload Groups
&lt;/div&gt;&lt;ul&gt;&lt;li&gt;In a workload group you can group session requests that are more or less equal to each other. You can group them with a classification stored procedure.
&lt;/li&gt;&lt;li&gt;On the group you can set some advanced settings.
&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;div&gt;Classifier Function to point logins to workload groups.
&lt;/div&gt;&lt;p style="margin-left: 27pt"&gt; 
 &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;To illustrate the pools and groups I took the following screenshot:
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;img src="http://blogs.msdn.com/blogfiles/menzos/070208_2030_TheSQL2008R1.png" alt=""/&gt;&lt;span style="font-family:Times New Roman; font-size:12pt"&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="color:#666666; font-family:Tahoma; font-size:8pt"&gt;Screen clipping taken: 2-7-2008; 22:00
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;This is the form in which you manage the pools and groups. It's a standard master-detail form. When you select a pool, you get the groups belonging to it in the bottom datagridview.  Options for the groups:
&lt;/p&gt;&lt;div style="margin-left: 52pt"&gt;&lt;table style="border-collapse:collapse" border="0"&gt;&lt;colgroup&gt;&lt;col style="width:139px"/&gt;&lt;col style="width:425px"/&gt;&lt;/colgroup&gt;&lt;tbody valign="top"&gt;&lt;tr&gt;&lt;td style="padding-top: 5px; padding-left: 5px; padding-bottom: 5px; padding-right: 5px; border-top:  solid #a3a3a3 1.0pt; border-left:  solid #a3a3a3 1.0pt; border-bottom:  solid #a3a3a3 1.0pt; border-right:  solid #a3a3a3 1.0pt"&gt;&lt;p&gt;Importance&lt;/p&gt;&lt;/td&gt;&lt;td style="padding-top: 5px; padding-left: 5px; padding-bottom: 5px; padding-right: 5px; border-top:  solid #a3a3a3 1.0pt; border-left:  none; border-bottom:  solid #a3a3a3 1.0pt; border-right:  solid #a3a3a3 1.0pt"&gt;&lt;p&gt;High, medium or low. Is taken into account while scheduling requests&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="padding-top: 5px; padding-left: 5px; padding-bottom: 5px; padding-right: 5px; border-top:  none; border-left:  solid #a3a3a3 1.0pt; border-bottom:  solid #a3a3a3 1.0pt; border-right:  solid #a3a3a3 1.0pt"&gt;&lt;p&gt;Maximum Requests&lt;/p&gt;&lt;/td&gt;&lt;td style="padding-top: 5px; padding-left: 5px; padding-bottom: 5px; padding-right: 5px; border-top:  none; border-left:  none; border-bottom:  solid #a3a3a3 1.0pt; border-right:  solid #a3a3a3 1.0pt"&gt;&lt;p&gt;Maximum number of simultaneous request in the group&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="padding-top: 5px; padding-left: 5px; padding-bottom: 5px; padding-right: 5px; border-top:  none; border-left:  solid #a3a3a3 1.0pt; border-bottom:  solid #a3a3a3 1.0pt; border-right:  solid #a3a3a3 1.0pt"&gt;&lt;p&gt;CPU Time&lt;/p&gt;&lt;/td&gt;&lt;td style="padding-top: 5px; padding-left: 5px; padding-bottom: 5px; padding-right: 5px; border-top:  none; border-left:  none; border-bottom:  solid #a3a3a3 1.0pt; border-right:  solid #a3a3a3 1.0pt"&gt;&lt;p&gt;Maximum CPU time a request can use&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="padding-top: 5px; padding-left: 5px; padding-bottom: 5px; padding-right: 5px; border-top:  none; border-left:  solid #a3a3a3 1.0pt; border-bottom:  solid #a3a3a3 1.0pt; border-right:  solid #a3a3a3 1.0pt"&gt;&lt;p&gt;Memory Grant %&lt;/p&gt;&lt;/td&gt;&lt;td style="padding-top: 5px; padding-left: 5px; padding-bottom: 5px; padding-right: 5px; border-top:  none; border-left:  none; border-bottom:  solid #a3a3a3 1.0pt; border-right:  solid #a3a3a3 1.0pt"&gt;&lt;p&gt;Maximum memory a request can take from the pool&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="padding-top: 5px; padding-left: 5px; padding-bottom: 5px; padding-right: 5px; border-top:  none; border-left:  solid #a3a3a3 1.0pt; border-bottom:  solid #a3a3a3 1.0pt; border-right:  solid #a3a3a3 1.0pt"&gt;&lt;p&gt;Grant Time-out&lt;/p&gt;&lt;/td&gt;&lt;td style="padding-top: 5px; padding-left: 5px; padding-bottom: 5px; padding-right: 5px; border-top:  none; border-left:  none; border-bottom:  solid #a3a3a3 1.0pt; border-right:  solid #a3a3a3 1.0pt"&gt;&lt;p&gt;Maximum amount of time a query will wait on a resource before it will fail&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="padding-top: 5px; padding-left: 5px; padding-bottom: 5px; padding-right: 5px; border-top:  none; border-left:  solid #a3a3a3 1.0pt; border-bottom:  solid #a3a3a3 1.0pt; border-right:  solid #a3a3a3 1.0pt"&gt;&lt;p&gt;Degree of Parallelism&lt;/p&gt;&lt;/td&gt;&lt;td style="padding-top: 5px; padding-left: 5px; padding-bottom: 5px; padding-right: 5px; border-top:  none; border-left:  none; border-bottom:  solid #a3a3a3 1.0pt; border-right:  solid #a3a3a3 1.0pt"&gt;&lt;p&gt;DOP for parallel requests&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;p style="margin-left: 55pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;When you got your pools and groups setup you have to create a classifier function:
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;CREATE&lt;/span&gt;
			&lt;span style="color:blue"&gt;FUNCTION&lt;/span&gt; [dbo]&lt;span style="color:gray"&gt;.&lt;/span&gt;[rgClassifier]&lt;span style="color:gray"&gt;()&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;RETURNS&lt;/span&gt;
			&lt;span style="color:blue"&gt;sysname&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;WITH&lt;/span&gt;
			&lt;span style="color:blue"&gt;SCHEMABINDING&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="color:blue; font-family:Courier New; font-size:10pt"&gt;AS
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="color:blue; font-family:Courier New; font-size:10pt"&gt;BEGIN
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="color:green; font-family:Courier New; font-size:10pt"&gt;-- Define the return sysname variable for the function
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;DECLARE&lt;/span&gt; @grp_name &lt;span style="color:blue"&gt;AS&lt;/span&gt;
			&lt;span style="color:blue"&gt;sysname&lt;/span&gt;&lt;span style="color:gray"&gt;;&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;SET&lt;/span&gt; @grp_name &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:red"&gt;'default'&lt;/span&gt;&lt;span style="color:gray"&gt;;&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="color:green; font-family:Courier New; font-size:10pt"&gt;-- Specify the T-SQL statements for mapping session information
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="color:green; font-family:Courier New; font-size:10pt"&gt;-- with Workload Groups defined for the Resource Governor.
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;IF &lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;&lt;span style="color:fuchsia"&gt;IS_SRVROLEMEMBER&lt;/span&gt;&lt;span style="color:blue"&gt;
			&lt;/span&gt;&lt;span style="color:gray"&gt;(&lt;/span&gt;&lt;span style="color:red"&gt;'sysadmin'&lt;/span&gt;&lt;span style="color:gray"&gt;)&lt;/span&gt;
			&lt;span style="color:gray"&gt;=&lt;/span&gt; 1&lt;span style="color:gray"&gt;)&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;SET&lt;/span&gt; @grp_name &lt;span style="color:gray"&gt;=&lt;/span&gt;
			&lt;span style="color:red"&gt;'Main'&lt;/span&gt;&lt;span style="color:gray"&gt;;&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 28pt"&gt;&lt;span style="color:green; font-family:Courier New; font-size:10pt"&gt;-- TODO
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 28pt"&gt;&lt;span style="color:green; font-family:Courier New; font-size:10pt"&gt;-- Put additional classification logic here
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;RETURN&lt;/span&gt; @grp_name&lt;span style="color:gray"&gt;;&lt;/span&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;span style="color:blue; font-family:Courier New; font-size:10pt"&gt;END
&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;Above a very simple example of a classifier function. You can filter requests on roles, usernames and whatever else you can think of. Every new session goes through this procedure and is assigned to a workload group.
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;That is a very simple explanation of the Resource Governor
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;strong&gt;Is it useful?
&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;It can be useful on systems with a heavy load and lots of processes that are not that important. With the Resource Governor you can guarantee that your OLTP performance is not influenced by some crap SSIS package that is taking all CPU's up to 100%.
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;strong&gt;But?
&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;It is quite complex to set up. There are lots of options average DBA's do not use or know of. If you make an error in the setup you could end up with an unresponsive server. It'll take skilled DBA's to set this up and manage properly. I'll hope the training kits of the new certifications will delve into this. Btw, if you mess up your setup: you can always correct things with the Dedicated Administration Account(DAC). That one is on the Internal pool and always has access to resources.
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;strong&gt;Conclusion
&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;Promising feature, but hard to set up and manage. For mission critical databases very useful. You need to alter your ways of administering an instance. You do not notice any performance problems when a bad query on a low priority workload is taking forever to complete. 
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;In multi-instance environments things get even more complicated since the Resource Governor is not instance aware.
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;strong&gt;Other Stuff on the Resource Governor
&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;I saw a few samples on Internet with nice Perfmon diagrams, showing the CPU and memory usage of workloads.
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;The RC0 BOL contains quite a lot of info on the Resource Governor. When you read everything you'll be an expert.
&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8682150" width="1" height="1"&gt;</description></item><item><title>Sync Framework</title><link>http://blogs.msdn.com/menzos/archive/2008/07/02/sync-framework.aspx</link><pubDate>Wed, 02 Jul 2008 12:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8680970</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/menzos/comments/8680970.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=8680970</wfw:commentRss><description>&lt;P&gt;Today I'm working for a customer who wants to implement the Sync Framework. As you probably know the Sync Framework is still beta but RTM is expected somewhere this fall. &lt;/P&gt;
&lt;P&gt;The client has a typical problem. They have 7000 field engineers roaming the globe. They need to synchronize a lot of date before and after an assignment. Till now the client had implemented merge replication in SQL 2005 for this. They ran into problems because for merge replication you need to be in the dbo group. Since service engineers are not part of that group they had to edit system stored procedures in order to get this solution to work. &lt;/P&gt;
&lt;P&gt;They asked us for a solution. I first tested merge replication in SQL 2008 without success. I heard vaguely about the Sync Framework and decided to give it a try. &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Sync Framework is more .Net then SQL Server. Since I'm not a .Net expert I was a bit worried in the beginning. Luckily implementing the Sync Framework is very easy and doesn't require lots of coding. Here's the graphical explanation in VB.Net(I still don't like C# so I always program in VB.Net): &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a Windows Forms application in Visual Studio 2008 &lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;Add a Local Database Cache to your project. &lt;/DIV&gt;
&lt;P&gt;&lt;IMG alt="" src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo1.png" mce_src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo1.png"&gt;&lt;SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman"&gt; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;Fill in the wizard screen. &lt;/DIV&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo2.png" mce_src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo2.png"&gt;&lt;SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman"&gt; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;I connected to a SQL 2008 database so I can use the new change tracking features. This saves your data model because if you don't use change tracking you have to use triggers and tombstone-tables and they will seriously clutter your data model. &lt;/P&gt;
&lt;P&gt;The client connection is a SQL Server Compact 3.5. SQL Server Compact is very small(2MB) and uses very little memory; even with pretty large datasets like 10.000 rows. Of course we can't expect miracles of it so loading up a 2GB database is useless. I did try it but performance was not optimal :). &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you press Ok the wizard will generate scripts and alter your development database. The scripts come in handy when you want to deploy your solution to the production database. &lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo3.png" mce_src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo3.png"&gt;&lt;SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman"&gt; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;Create a dataset of your generated local data cache. &lt;/DIV&gt;
&lt;P&gt;The wizard proceeds with the dataset screen: &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo4.png" mce_src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo4.png"&gt;&lt;SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman"&gt; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;When you are done creating the dataset, bring up the data sources view in visual studio. Now drag a table from your dataset and drop it on the empty form. &lt;/DIV&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo5.png" mce_src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo5.png"&gt;&lt;SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman"&gt; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you drag a datatable on your form Visual Studio adds all necessary components and code. So without programming you end up with a fully functional form. &lt;/P&gt;
&lt;P&gt;I docked the datagridview in the form to make it look better. &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;Now its time to add syncing. First add a button on the toolbar for executing syncing. In the code of the form you add the click event of the button. To obtain the sync code you go back to the local data cache designer &lt;/DIV&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo6.png" mce_src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo6.png"&gt;&lt;SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman"&gt; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you press the &lt;SPAN style="COLOR: blue"&gt;&lt;STRONG&gt;Show Code Example&lt;/STRONG&gt;&lt;/SPAN&gt; link you'll get the code you need. Just paste this code in the click event of the button. &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the event: &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;Private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; cmdSync_Click(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; sender &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Object&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; e &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; System.EventArgs) &lt;SPAN style="COLOR: blue"&gt;Handles&lt;/SPAN&gt; cmdSync.Click &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;' Call SyncAgent.Synchronize() to initiate the synchronization process. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;' Synchronization only updates the local database, not your project's data source. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; syncAgent &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; SampleDataCacheSyncAgent = &lt;SPAN style="COLOR: blue"&gt;New&lt;/SPAN&gt; SampleDataCacheSyncAgent() &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; syncStats &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; Microsoft.Synchronization.Data.SyncStatistics = syncAgent.Synchronize() &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: Courier New"&gt;' TODO: Reload your project data source from the local database (for example, call the TableAdapter.Fill method). &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;Me&lt;/SPAN&gt;.SyncDataSet.Parameters.Merge(&lt;SPAN style="COLOR: blue"&gt;Me&lt;/SPAN&gt;.ParametersTableAdapter.GetData()) &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;The merge line I added myself. The form is ready now. &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;Now we move on with the sync class itself to choose what kind of syncing we want to have. &lt;/DIV&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG alt="" src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo7.png" mce_src="http://blogs.msdn.com/blogfiles/menzos/070208_0957_SyncFramewo7.png"&gt;&lt;SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman"&gt; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now you can choose the syncdirection that fits your needs. &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;The last step is to add conflict management during syncing: &lt;/DIV&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;Partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; SampleDataCacheServerSyncProvider &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;Private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; SampleDataCacheServerSyncProvider_ApplyChangeFailed(&lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; sender &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Object&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;ByVal&lt;/SPAN&gt; e &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; Microsoft.Synchronization.Data.ApplyChangeFailedEventArgs) &lt;SPAN style="COLOR: blue"&gt;Handles&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Me&lt;/SPAN&gt;.ApplyChangeFailed &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; clientChange &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; DataTable = e.Conflict.ClientChange &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;Dim&lt;/SPAN&gt; serverChange &lt;SPAN style="COLOR: blue"&gt;As&lt;/SPAN&gt; DataTable = e.Conflict.ServerChange &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;e.Action = Microsoft.Synchronization.Data.ApplyAction.RetryWithForceWrite &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Sub&lt;/SPAN&gt; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;End&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Class&lt;/SPAN&gt; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;I choose a simple RetryWithForceWrite here but you can code here what you want. &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Conclusion &lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;I build a fully functional sync sample in a few minutes. That's not too bad. Of course this is a simple scenario where complete tables are synchronized. If you want to synchronize on queries you have to write a lot of code yourself. But it is possible. I can think of lots scenarios where this framework could be very useful. Especially with all the smartphones and all that. &lt;/P&gt;
&lt;P&gt;Deploying this solution was something else. I ran into lots of problems. There is no installer for Sync Services yet and I didn't succeed in deploying it manually. For now you have to install the complete .Net framework 3.5 SDK and the beta service packs for VS 2008 and .Net 3.5. The installer and the Sync Framework itself are expected to go RTM this fall. So we have to wait a little bit for proper installers. &lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8680970" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/menzos/archive/tags/Sync+Framework/default.aspx">Sync Framework</category></item><item><title>Table Partitioning Conclusions</title><link>http://blogs.msdn.com/menzos/archive/2008/07/01/table-partitioning-conclusions.aspx</link><pubDate>Tue, 01 Jul 2008 12:14:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8676087</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/menzos/comments/8676087.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=8676087</wfw:commentRss><description>&lt;p style="margin-left: 1pt"&gt;Today I tested all my code and made changes where I made mistakes. And I made quite a few! My biggest mistake was with the GetDate() function. I made partitions with milliseconds which was not my intention! I changed the code in my last post where necessary. If you still encounter bugs, please contact me.
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;A positive conclusion is the management studio of SQL 2008. Great tool! I used the scripts of the partitioning wizard quite a lot but the best feature it offered was the debugger. Finally T-SQL developers have T-SQL debugging without installing Visual Studio. Here's a screenshot:
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;&lt;img src="http://blogs.msdn.com/blogfiles/menzos/070108_0913_TablePartit1.jpg" alt=""/&gt;&lt;span style="font-family:Times New Roman; font-size:12pt"&gt;
		&lt;/span&gt;&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;The debugger saved me a lot of time because you immediately know where your bugs are when you can step through your code. No more output messages and all that old-school techniques. Of course intellisense also saves time while coding. I'm impressed by the new management studio. 
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;Btw: you can't use the debugger on a SQL 2005 instance.
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt; 
 &lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;The wizards in the SQL 2008 management studio only support standard scenarios. I hope I pointed you in some directions what you can do with partitioning when you want something else. This was quite a complex scenario but didn't require that much code.
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;The code I produced is not nearly finished. Robust error handling should be added before this is installed on production databases. For me this assignment is over though. The DBA's from the client can take it from here.
&lt;/p&gt;&lt;p style="margin-left: 1pt"&gt;My next assignments will be on the Sync framework and on the Resource governor of SQL 2008. My next blogs will be on those subjects.
&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8676087" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/menzos/archive/tags/SQL+2008/default.aspx">SQL 2008</category><category domain="http://blogs.msdn.com/menzos/archive/tags/SQL+Server+Concepts/default.aspx">SQL Server Concepts</category><category domain="http://blogs.msdn.com/menzos/archive/tags/Table+Partitioning/default.aspx">Table Partitioning</category></item><item><title>Table Partitioning Sliding Window Case</title><link>http://blogs.msdn.com/menzos/archive/2008/06/30/table-partitioning-sliding-window-case.aspx</link><pubDate>Mon, 30 Jun 2008 23:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8672894</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/menzos/comments/8672894.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=8672894</wfw:commentRss><description>&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;As I mentioned in my last post I'm currently working on a complex partitioning project for a customer. They need partitioning in order to speed up inserts on the table. Lots of inserts take place on this table. Right now they have around 2 million inserts per day&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;and this will grow quickly to 4 million inserts/day. The table is heavily queried too, so there are a lot of indexes on the table. I know this isn't the most desirable design but that's how it is at the moment and at Premier Field Engineering our projects are usually not long enough to make judgments on architecture and design. Initial testing with partitioning looked promising so now we will implement it.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Let me get started. We start with the partition function.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;First we set boundaries. &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l0 level2 lfo1; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;They want partitions for each day of the current week. Weeks start on Monday.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l0 level2 lfo1; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;They want partitions for the last four weeks.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l0 level2 lfo1; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;They want partitions for the last&amp;nbsp;6 months.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l0 level2 lfo1; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;For the rest of the data they want one big partition.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;There desired boundaries have some problems:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l0 level2 lfo1; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Last four weeks do usually not end on the first of the month. Do we add these rows to the last month partition or do we make a partition for this month. I choose the latter because sooner or later this month would become a partition anyway.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l0 level2 lfo1; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;The archive partition is going to be huge! Maybe its better to move it to an archive-table. I have to discuss this with the customer, for now it's ok.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;The table is partitioned at the moment with a &lt;SPAN style="COLOR: blue"&gt;RANGE RIGHT&lt;/SPAN&gt; function. You can do 2 things when you have a table on a partition scheme you don't want:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l0 level2 lfo2; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;You can drop the clustered index and recreate it with the &lt;SPAN style="COLOR: blue"&gt;MOVE TO partitionscheme(partitionColumn)&lt;/SPAN&gt; command.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l0 level2 lfo3; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;You can drop the table and recreate it on the partition scheme you want.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;I choose number 2 because with SSIS you can quickly move data around and recreating a clustered index on a big table takes forever, especially with the &lt;SPAN style="COLOR: blue"&gt;MOVE TO&lt;/SPAN&gt; directive.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;I start with the initial situation in the partition function:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;I'll load up the SQL2008 management studio and generate the scripts with the wizard. The wizard is also available when you connect to a SQL 2005 instance.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Here's the script I got:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;BEGIN&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;TRANSACTION&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;CREATE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; [pfEvent]&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;datetime&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;AS&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;RANGE&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;LEFT&lt;/SPAN&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;FOR&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;VALUES &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;N'2008-07-06T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-07-05T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-07-04T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-07-03T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-07-02T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-07-01T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-06-30T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-06-23T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-06-16T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-06-09T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-06-02T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-06-01T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-05-31T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-04-30T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-03-31T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-02-29T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2008-01-31T00:00:00'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;N'2007-12-31T00:00:00'&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;CREATE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; SCHEME [psEvent] &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;AS&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; [pfEvent] &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;TO &lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;[PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [PRIMARY]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [ARCHIVE]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [ARCHIVE]&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;CREATE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;CLUSTERED&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;INDEX&lt;/SPAN&gt; [ClusteredIndex_on_psEvent_633504211030911271] &lt;SPAN style="COLOR: blue"&gt;ON&lt;/SPAN&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[Event] &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;[EvtDatTim]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;)&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;ON&lt;/SPAN&gt; [psEvent]&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;[EvtDatTim]&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;DROP&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;INDEX&lt;/SPAN&gt; [ClusteredIndex_on_psEvent_633504211030911271] &lt;SPAN style="COLOR: blue"&gt;ON&lt;/SPAN&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[Event] &lt;SPAN style="COLOR: blue"&gt;WITH &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;ONLINE&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;OFF&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;COMMIT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;TRANSACTION&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;I'll execute the script and the empty recreated table will be partitioned.&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Now I can recreate all indexes again and load the data back with SSIS. After that foreign keys can be restored. &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Now the table is partitioned I have to implement a mechanism to automate the sliding window. I'm going to implement this with stored procedures that are executed in jobs.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;A couple of stored procedures are needed:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level2 lfo4; tab-stops: list 72.0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Calibri size=3&gt;1.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;A stored procedure, run every Sunday, that creates the daily partitions of next week.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level2 lfo5; tab-stops: list 72.0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Calibri size=3&gt;2.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;A stored procedure that merges the daily partitions of last week into 1 week partition. Also run every Sunday.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level2 lfo6; tab-stops: list 72.0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Calibri size=3&gt;3.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;A stored procedure that merges the fifth week partition into the corresponding month partitions. Also run every Sunday.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level2 lfo7; tab-stops: list 72.0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Calibri size=3&gt;4.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;A stored procedure that merges the seventh month partition into the archive partition. For simplicity I run this one every Sunday as well. Otherwise I need a second job.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Off course I could do all this in one stored procedure but I like small procedures. That debugs a lot easier!&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;1 &amp;amp; 2 are easy, 3 &amp;amp; 4 are a bit more complicated. Let's get going.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 22.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level1 lfo8; tab-stops: list 36.0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Calibri size=3&gt;1.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;You can't simply create partitions with GetDate(). You would get the current time down to milliseconds into the boundary range. That's why I created a function to prepare the date:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;CREATE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; MSPartitionPrepareDate &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;@Date &lt;SPAN style="COLOR: blue"&gt;SmallDateTime&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;RETURNS&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;SmallDateTime&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;AS&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;DECLARE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @Result &lt;SPAN style="COLOR: blue"&gt;SmallDateTime&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @Result &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;CAST&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;CONVERT&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;Varchar&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;23&lt;SPAN style="COLOR: gray"&gt;),&lt;/SPAN&gt;@Date&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt;101 &lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;SmallDateTime&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;RETURN&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @Result&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;The procedure that creates partitions for the days of the next week:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;CREATE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PROCEDURE&lt;/SPAN&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[MSPartitioningAddDays] &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;AS&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;DECLARE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @SplitDate &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DateTime&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; SCHEME psEvent&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;NEXT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; USED [PRIMARY]&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @SplitDate &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;MSPartitionPrepareDate&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GetDate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; 1&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; pfEvent&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;SPLIT &lt;SPAN style="COLOR: blue"&gt;RANGE &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@SplitDate&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; SCHEME psEvent&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;NEXT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; USED [PRIMARY]&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @SplitDate &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;MSPartitionPrepareDate&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GetDate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; 2&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; pfEvent&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;SPLIT &lt;SPAN style="COLOR: blue"&gt;RANGE &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@SplitDate&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; SCHEME psEvent&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;NEXT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; USED [PRIMARY]&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @SplitDate &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;MSPartitionPrepareDate&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GetDate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; 3&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; pfEvent&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;SPLIT &lt;SPAN style="COLOR: blue"&gt;RANGE &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@SplitDate&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; SCHEME psEvent&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;NEXT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; USED [PRIMARY]&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @SplitDate &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;MSPartitionPrepareDate&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GetDate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; 4&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; pfEvent&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;SPLIT &lt;SPAN style="COLOR: blue"&gt;RANGE &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@SplitDate&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; SCHEME psEvent&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;NEXT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; USED [PRIMARY]&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @SplitDate &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;MSPartitionPrepareDate&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GetDate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; 5&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; pfEvent&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;SPLIT &lt;SPAN style="COLOR: blue"&gt;RANGE &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@SplitDate&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; SCHEME psEvent&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;NEXT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; USED [PRIMARY]&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @SplitDate &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;MSPartitionPrepareDate&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GetDate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; 6&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; pfEvent&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;SPLIT &lt;SPAN style="COLOR: blue"&gt;RANGE &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@SplitDate&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; SCHEME psEvent&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;NEXT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; USED [PRIMARY]&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @SplitDate &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;MSPartitionPrepareDate&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GetDate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; 7&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; pfEvent&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;SPLIT &lt;SPAN style="COLOR: blue"&gt;RANGE &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@SplitDate&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;That's not too hard. Very simple and very readable. First you add room on the partition scheme and then you create the new day partition on the function. Do that 7 times and you're done.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 22.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level1 lfo9; tab-stops: list 36.0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Calibri size=3&gt;2.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Now we have to merge the data of the partitions of last week into one week partition. You can do that in 2 ways: Either you merge all days except Sunday, or you switch out all data to a staging table and switch it back in after you split out the days. Since a day is a relatively small partition I merge them directly.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;CREATE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PROCEDURE&lt;/SPAN&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[MSPartitioningMergeDaysInWeek] &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;AS&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Merge the last 7 days partitions in one week partition.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Last Monday&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;EXEC&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[MSPartitioningMergeDay]&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;1&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Last Tuesday &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;EXEC&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[MSPartitioningMergeDay]&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;2&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Last Wednesday&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;EXEC&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[MSPartitioningMergeDay]&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;3&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Last Thursday&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;EXEC&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[MSPartitioningMergeDay]&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;4&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Last Friday&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;EXEC&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[MSPartitioningMergeDay]&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;5&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Last Saturday&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;EXEC&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[MSPartitioningMergeDay]&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;6&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;END&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;CREATE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PROCEDURE&lt;/SPAN&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[MSPartitioningMergeDay]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;@MinusDays &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;AS&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;DECLARE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @MergeDate &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DateTime&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @MergeDate &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;MSPartitionPrepareDate&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GetDate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;-&lt;/SPAN&gt; @MinusDays&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; pfEvent&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;MERGE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;RANGE &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@MergeDate&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;END&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;2 simple stored procedures that get this job done. I work from 1 to 6 in order to merge the smallest set. If I work from 6 to 1 the set to merge would be larger after every merge.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 22.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level1 lfo10; tab-stops: list 36.0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Calibri size=3&gt;3.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;The simple stuff is done now. After you run stored procedure 2 you have 5 week partitions. The oldest needs to be merged with the already existing month partition. If no partition exists yet one has to be created.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Simple design:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level2 lfo11; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;First find the oldest week partition. GetDate() - 35 will get you there.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level2 lfo11; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Switch out this partition&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level2 lfo11; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Look if a new month starts between GetDate() - 35 and GetDate() - 28&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level2 lfo11; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;If yes create new month partition that ends on GetDate() - 28&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level2 lfo11; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;If no alter the existing month partition so that it ends on GetDate() - 28&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 49.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level2 lfo11; tab-stops: list 72.0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Switch partition back in.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Here's the code:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;CREATE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PROCEDURE&lt;/SPAN&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[MSPartitioningMergeOldestWeekPartition] &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;AS&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;DECLARE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @PartitionNumber &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;DECLARE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @Boundary &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DateTime&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;DECLARE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @DateDiff &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;DECLARE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @SplitDate &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DateTime&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;DECLARE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @ErrorNumber &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Partition number and boundary value&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @PartitionNumber &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; boundary_id&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;@Boundary &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;CAST&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;value &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DateTime&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;FROM&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: green"&gt;sys&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;partition_range_values&lt;/SPAN&gt; rv&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;INNER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: gray"&gt;JOIN&lt;/SPAN&gt; &lt;SPAN style="COLOR: green"&gt;sys&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;partition_functions&lt;/SPAN&gt; pf&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ON&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; rv&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;function_Id &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; pf&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;function_id&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;WHERE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; pf&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;name &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: red"&gt;'pfEvent'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;AND&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: fuchsia"&gt;CAST&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;value &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DateTime&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;CAST&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;CAST&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;DATEPART&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;"YEAR"&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;getdate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()-&lt;/SPAN&gt;35&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Varchar&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;4&lt;SPAN style="COLOR: gray"&gt;))&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 265.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;+&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;'-'&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;Right(&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;'0'&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;Convert&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;VarChar&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;2&lt;SPAN style="COLOR: gray"&gt;),&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;Month&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GetDate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()-&lt;/SPAN&gt;35&lt;SPAN style="COLOR: gray"&gt;)),&lt;/SPAN&gt; 2&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 265.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;+&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;'-'&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;Right(&lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;'0'&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;Convert&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;VarChar&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;2&lt;SPAN style="COLOR: gray"&gt;),&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;Day&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GetDate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()-&lt;/SPAN&gt;35&lt;SPAN style="COLOR: gray"&gt;)),&lt;/SPAN&gt; 2&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 265.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;+&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;' 00:00:00.000'&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DateTime&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;))&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Switching out.....&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;TABLE&lt;/SPAN&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[Event]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;SWITCH &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; @PartitionNumber&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;TO&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; Staging_Event_Proto&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;Print&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: red"&gt;'Geswitched'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;--Getting rid of the now empty partition....&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @SplitDate &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;MSPartitionPrepareDate&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GetDate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;-&lt;/SPAN&gt; 35&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; pfEvent&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;MERGE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;RANGE &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@SplitDate&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @DateDiff &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;DATEDIFF&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;"mm"&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;getdate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()-&lt;/SPAN&gt;35&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GETDATE&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()-&lt;/SPAN&gt;28&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;IF&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @DateDiff &lt;SPAN style="COLOR: gray"&gt;&amp;lt;&amp;gt;&lt;/SPAN&gt; 0&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Find last day of the month and make partitionboundary of it&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; SCHEME psEvent&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;NEXT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; USED [PRIMARY]&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; pfEvent&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;SPLIT &lt;SPAN style="COLOR: blue"&gt;RANGE &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;DATEADD&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;"dd"&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;-&lt;/SPAN&gt;1&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;CAST&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;CAST&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;DATEPART&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;"YYYY" &lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;Getdate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()-&lt;/SPAN&gt;30&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Varchar&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;4&lt;SPAN style="COLOR: gray"&gt;))&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: red"&gt;'-'&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;CAST&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;DATEPART&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;"mm"&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;Getdate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()-&lt;/SPAN&gt;28&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;Varchar&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;2&lt;SPAN style="COLOR: gray"&gt;))&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: red"&gt;'-01'&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DATETIME&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;)));&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Starting day of next partition is endday of this partition.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;BEGIN&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;TRY&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 130.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; SCHEME psEvent&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 130.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;NEXT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; USED [PRIMARY]&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 130.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @SplitDate &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;MSPartitionPrepareDate&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: fuchsia"&gt;GetDate&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;-&lt;/SPAN&gt; 28&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 130.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; pfEvent&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 130.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;SPLIT &lt;SPAN style="COLOR: blue"&gt;RANGE &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@SPlitDate&lt;SPAN style="COLOR: gray"&gt;);&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;END&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;TRY&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;BEGIN&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;CATCH&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 130.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Partition might be there already&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 130.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @ErrorNumber &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;ERROR_NUMBER&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;();&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 130.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;IF&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @ErrorNumber &lt;SPAN style="COLOR: gray"&gt;&amp;lt;&amp;gt;&lt;/SPAN&gt; 7721 &lt;SPAN style="COLOR: green"&gt;-- Errornumber of overlapping partitions&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 157.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 184.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 211.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: fuchsia; FONT-FAMILY: 'Courier New'"&gt;ERROR_NUMBER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;()&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; ErrorNumber&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 211.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: fuchsia; FONT-FAMILY: 'Courier New'"&gt;ERROR_SEVERITY&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;()&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; ErrorSeverity&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 211.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: fuchsia; FONT-FAMILY: 'Courier New'"&gt;ERROR_STATE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;()&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; ErrorState&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 211.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: fuchsia; FONT-FAMILY: 'Courier New'"&gt;ERROR_PROCEDURE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;()&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; ErrorProcedure&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 211.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: fuchsia; FONT-FAMILY: 'Courier New'"&gt;ERROR_LINE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;()&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; ErrorLine&lt;SPAN style="COLOR: gray"&gt;,&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 211.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: fuchsia; FONT-FAMILY: 'Courier New'"&gt;ERROR_MESSAGE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;()&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; ErrorMessage&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 157.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 103.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;END&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;CATCH&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Switching in....&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Can't use the switch command since the parititions don't match.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;BEGIN&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;TRANSACTION&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;INSERT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;INTO&lt;/SPAN&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[Event]&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: gray"&gt;*&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FROM&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;Staging_Event_Proto&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;TRUNCATE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;TABLE&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;Staging_Event_Proto&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;COMMIT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;TRANSACTION&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;END&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;That's quite a piece of code. I have tested it now and it works. If it doesn't on your machine give me a shout.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 22.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -18pt; mso-list: l1 level1 lfo12; tab-stops: list 36.0pt"&gt;&lt;SPAN style="mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Calibri size=3&gt;4.&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;For the last procedure I take the easy way and just merge the partition. The partition is not queried a lot and it can run in the background without bothering anybody.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Here we go:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;CREATE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PROCEDURE&lt;/SPAN&gt; [dbo]&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;[MSPartitioningMergeOldestMonth] &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;AS&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;DECLARE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @Boundary &lt;SPAN style="COLOR: blue"&gt;DateTime&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;-- Boundary value: it's always partion 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; @Boundary &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: fuchsia"&gt;CAST&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;value &lt;SPAN style="COLOR: blue"&gt;AS&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;DateTime&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;FROM&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: green"&gt;sys&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;partition_range_values&lt;/SPAN&gt; rv&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;INNER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: gray"&gt;JOIN&lt;/SPAN&gt; &lt;SPAN style="COLOR: green"&gt;sys&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;partition_functions&lt;/SPAN&gt; pf&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 76.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ON&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; rv&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;function_Id &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; pf&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;function_id&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;WHERE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; pf&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;name &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: red"&gt;'pfEvent'&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;AND&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; rv&lt;SPAN style="COLOR: gray"&gt;.&lt;/SPAN&gt;boundary_id &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;--Merge this partition....&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;ALTER&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;PARTITION&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;FUNCTION&lt;/SPAN&gt; pfEvent&lt;SPAN style="COLOR: gray"&gt;()&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;MERGE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt; &lt;SPAN style="COLOR: blue"&gt;RANGE &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;@Boundary&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 49.45pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;END&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'"&gt;;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;So that's it. Now we only have to make a job with a weekly schedule to run it on Sunday and we're done. Tomorrow I'm going to test all this code. This case took me almost 7 hours and a lot of energy :). But it was great fun. I usually don't code all day long anymore but every once and while it's great fun!&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Expect a post on testing soon.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 0pt 22.45pt"&gt;&lt;SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;I know I'm not the best T-SQL Coder there is: suggestions to optimize the code are therefore welcome.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8672894" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/menzos/archive/tags/SQL+Server+Concepts/default.aspx">SQL Server Concepts</category><category domain="http://blogs.msdn.com/menzos/archive/tags/Table+Partitioning/default.aspx">Table Partitioning</category></item><item><title>Table Partitioning Basics(2)</title><link>http://blogs.msdn.com/menzos/archive/2008/06/29/table-partitioning-basics-2.aspx</link><pubDate>Sun, 29 Jun 2008 23:06:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8668332</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/menzos/comments/8668332.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=8668332</wfw:commentRss><description>&lt;p&gt;In my last blog I looked at &lt;span style="color:blue"&gt;SPLIT&lt;/span&gt; and &lt;span style="color:blue"&gt;MERGE&lt;/span&gt; operations on partition functions. &lt;span style="color:blue"&gt;SPLIT&lt;/span&gt; and &lt;span style="color:blue"&gt;MERGE&lt;/span&gt; are nice functions but I use them with care.  A merge is not the best performing function there is…. And that is completely logical when you think about everything that is happening during execution:
&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Data needs to be transferred from one partition to another.
&lt;/li&gt;&lt;li&gt;When the new partition is on another filegroup data needs to be transferred physically.
&lt;/li&gt;&lt;li&gt;All storage aligned indexes(and all indexes need to be storage aligned!) must be updated.
&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;All very time-consuming tasks. Not things you want to do every day. A split is usually an easy operation because you usually add an partition at the end of the range. If you split existing data the same rules apply as to a merge.
&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;So what is a good idea then to move data around in an partitioned table? When you need to move complete partitions the SWITCH operator is a good choice.
&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue"&gt;ALTER TABLE&lt;/span&gt; Test
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue"&gt;SWITCH PARTITION&lt;/span&gt; 1
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue"&gt;TO&lt;/span&gt; StagingTable
&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;This statement moves all data in partition 1 to the staging table StageTest. The staging table needs to have exactly the same schema as the partitioned table. It also needs to have the same clustered and non-clustered indexes and the same constraints. It also needs to be empty. This is a bit of a pain but if you don't do this you'll get errors:
&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Courier New; font-size:10pt"&gt;Msg 4947, Level 16, State 1, Line 1
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Courier New; font-size:10pt"&gt;ALTER TABLE SWITCH statement failed. There is no identical index in source table Test.dbo.StagingTable' for the index 'IX_Date' in target table 'Test.dbo.PartitionSample'.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Courier New; font-size:10pt"&gt;Msg 4982, Level 16, State 1, Line 1
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Courier New; font-size:10pt"&gt;ALTER TABLE SWITCH statement failed. Check constraints of source table 'Test.dbo.StagingTable' allow values that are not allowed by range defined by partition 4 on target table 'Test.dbo.PartitionSample'.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Courier New; font-size:10pt"&gt;Msg 4905, Level 16, State 1, Line 1
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:red; font-family:Courier New; font-size:10pt"&gt;ALTER TABLE SWITCH statement failed. The target table 'Test.dbo.StagingTable' must be empty.
&lt;/span&gt;&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;Switching out takes seconds instead of hours so the choice is easy. 
&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;You can switch in data as well with the following statement:
&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;&lt;span style="color:blue"&gt;ALTER TABLE&lt;/span&gt; StagingTable
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue"&gt;SWITCH TO &lt;/span&gt;Test&lt;span style="color:blue"&gt; PARTITION&lt;/span&gt; 4
&lt;/p&gt;&lt;p&gt;&lt;span style="color:blue"&gt;TO&lt;/span&gt; Test
&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;This is also very fast, especially when the partition is empty.
&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;So concluding: when you want to merge to big partitions you switch out the partition to merge to a staging table. Then you remove the boundary point with the &lt;span style="color:blue"&gt;ALTER PARTITION FUNCTION…MERGE&lt;/span&gt; command. After that you switch the data back in to the partitioned table.
&lt;/p&gt;&lt;p&gt; 
 &lt;/p&gt;&lt;p&gt;This week I'm going to implement a complicated sliding window scenario at one of our customers. The scenario includes a partition function with daily partitions for the current week, weekly partitions for the last four weeks and monthly partitions for older data. I'll blog about that later this week.
&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8668332" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/menzos/archive/tags/SQL+Server+Concepts/default.aspx">SQL Server Concepts</category><category domain="http://blogs.msdn.com/menzos/archive/tags/Table+Partitioning/default.aspx">Table Partitioning</category></item><item><title>Table Partitioning Basics</title><link>http://blogs.msdn.com/menzos/archive/2008/06/28/table-partitioning-basics.aspx</link><pubDate>Sat, 28 Jun 2008 23:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8664623</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/menzos/comments/8664623.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=8664623</wfw:commentRss><description>&lt;P style="MARGIN-LEFT: 5pt"&gt;Partioning objects in SQL Server starts with defining Partition Functions and Partition Schema's. The functions split your data and the schema's place it somewhere. A function can be used by multiple schema's. You start out by creating a function: &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;CREATE PARTITION FUNCTION&lt;/SPAN&gt; [pfSample](&lt;SPAN style="COLOR: blue"&gt;datetime&lt;/SPAN&gt;) &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;AS RANGE LEFT &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;FOR VALUES&lt;/SPAN&gt; (1, 100, 1000); &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;A function must cover all data: from minus infinity to infinity. &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;You have to choose between &lt;SPAN style="COLOR: blue"&gt;RANGE RIGHT &lt;/SPAN&gt;and&lt;SPAN style="COLOR: blue"&gt; RANGE LEFT. RANGE LEFT &lt;/SPAN&gt;is default if you don't specify it. &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;RANGE RIGHT:&lt;/SPAN&gt; &lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 5pt"&gt;
&lt;TABLE class="" style="BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 75px"&gt;
&lt;COL style="WIDTH: 64px"&gt;
&lt;COL style="WIDTH: 112px"&gt;
&lt;COL style="WIDTH: 131px"&gt;
&lt;COL style="WIDTH: 72px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: #a3a3a3 1pt solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: #a3a3a3 1pt solid; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&lt;STRONG&gt;Partition&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: #a3a3a3 1pt solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: #a3a3a3 1pt solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: #a3a3a3 1pt solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: #a3a3a3 1pt solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&lt;STRONG&gt;4&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: medium none; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: #a3a3a3 1pt solid; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&lt;STRONG&gt;Values&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: medium none; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&amp;lt;1&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: medium none; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&amp;gt;= 1 AND &amp;lt; 100&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: medium none; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&amp;gt;=100 AND &amp;lt; 1000&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: medium none; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&amp;gt;= 1000&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;RANGE LEFT: &lt;/SPAN&gt;&lt;/P&gt;
&lt;DIV style="MARGIN-LEFT: 5pt"&gt;
&lt;TABLE class="" style="BORDER-COLLAPSE: collapse" border=0&gt;
&lt;COLGROUP&gt;
&lt;COL style="WIDTH: 75px"&gt;
&lt;COL style="WIDTH: 64px"&gt;
&lt;COL style="WIDTH: 112px"&gt;
&lt;COL style="WIDTH: 131px"&gt;
&lt;COL style="WIDTH: 68px"&gt;&lt;/COLGROUP&gt;
&lt;TBODY vAlign=top&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: #a3a3a3 1pt solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: #a3a3a3 1pt solid; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&lt;STRONG&gt;Partition&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: #a3a3a3 1pt solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: #a3a3a3 1pt solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: #a3a3a3 1pt solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: #a3a3a3 1pt solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&lt;STRONG&gt;4&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: medium none; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: #a3a3a3 1pt solid; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&lt;STRONG&gt;Values&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: medium none; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&amp;lt;=1&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: medium none; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&amp;gt; 1 AND &amp;lt;= 100&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: medium none; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&amp;gt;100 AND &amp;lt;= 1000&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: #a3a3a3 1pt solid; PADDING-RIGHT: 5px; BORDER-TOP: medium none; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: medium none; PADDING-TOP: 5px; BORDER-BOTTOM: #a3a3a3 1pt solid"&gt;
&lt;P&gt;&amp;gt; 1000&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;Things to notice here: &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You always have 1 partition more than values you put in the Partition Function. &lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;Range Left/Right does nothing more putting the boundary on a side. &lt;/DIV&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Range Left/Range Right is an important decision to make. Most people favor &lt;SPAN style="COLOR: blue"&gt;Range Left&lt;STRONG&gt;. &lt;/STRONG&gt;&lt;/SPAN&gt;If you use a sliding window scenario &lt;SPAN style="COLOR: blue"&gt;Range Left&lt;/SPAN&gt; feels more natural because with Range Right your first partition will be empty. Let me clarify this with the following example: &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;You want to partition a table on a field that contains a date. You want to make partitions for the last four months. The rest of the data you switch out to an archive table. So you have partitions for March, April, May and June. Your first partition on a &lt;SPAN style="COLOR: blue"&gt;Range Right&lt;/SPAN&gt; will be dates before March. So your first partition will be empty… The same scenario with a&lt;SPAN style="COLOR: blue"&gt; Range Left&lt;/SPAN&gt; will make your last partition empty. That's not so bad and could even be useful if you didn't slide your window on time. &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;When you are satisfied with your partition function you can proceed with the partition schema: &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;CREATE PARTITION SCHEME&lt;/SPAN&gt; [psSample] &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;AS PARTITION&lt;/SPAN&gt; [pfSample] &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;TO&lt;/SPAN&gt; ([PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY]) &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;Schema's tell the function where to put the partitions. In this case, on my laptop, I put all partitions on the primary filegroup. In production environments you will put them on different filegroups of course. &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;Now you have everything in place to partition a table that has a datetime column. In SQL 2008 you can partition an existing table with a wizard. No such thing in SQL 2005. You have to rename your table, recreate a new on on the partition scheme, and move the data back…..Or you just remove the clustered index and recreate that on the partition scheme &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;ALTER TABLE Test &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;ADD CONSTRAINT&lt;/SPAN&gt; PK_Test &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 32pt"&gt;&lt;SPAN style="COLOR: blue"&gt;PRIMARY KEY CLUSTERED(&lt;/SPAN&gt;TestDate&lt;SPAN style="COLOR: blue"&gt;)&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 32pt"&gt;&lt;SPAN style="COLOR: blue"&gt;ON &lt;/SPAN&gt;psSample(TestDate) &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;Make sure that one of the columns in your clustered key matches the partitioning column otherwise: &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: red"&gt;Msg 1908, Level 16, State 1, Line 1 &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: red"&gt;Column 'datum' is partitioning column of the index 'PK_Test2'. Partition columns for a unique index must be a subset of the index key. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;On tables with lots of rows partitioning the clustered key might take a while. Think of hours on really big tables. Don't try this on peak hours! &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;Now you have your partitioned table up and running you have a few commands at your disposal to administer the thing. &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;First of all you can&lt;SPAN style="COLOR: blue"&gt; SPLIT&lt;/SPAN&gt; or &lt;SPAN style="COLOR: blue"&gt;MERGE&lt;/SPAN&gt; a partition. &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;SPLIT&lt;/SPAN&gt; adds a new partition. For example: &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;ALTER PARTITION FUNCTION&lt;/SPAN&gt; pfSample() &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;SPLIT RANGE&lt;/SPAN&gt;(2000); &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;This piece of T-SQL will create an extra partition that contains rows &amp;gt; 2000. If you get the following error you have to alter your Partition Scheme: &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: red"&gt;Msg 7707, Level 16, State 1, Line 1 &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: red"&gt;The associated partition function 'pfSample' generates more partitions than there are file groups mentioned in the scheme 'psSample'. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;The message says it all: you are out of places to put partitions on in your scheme. So let's add one to the scheme: &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;ALTER PARTITION SCHEMA&lt;/SPAN&gt; psSample &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;NEXT USED&lt;/SPAN&gt; [Primary]; &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;Now you have added an extra partition to your scheme. The split will work now. &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;Sometimes you want to merge two partitions. That you do with the&lt;SPAN style="COLOR: blue"&gt; MERGE &lt;/SPAN&gt;statement: &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;ALTER PARTITION FUNCTION&lt;/SPAN&gt; pfSample() &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&lt;SPAN style="COLOR: blue"&gt;MERGE RANGE&lt;/SPAN&gt;(1); &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;When this completes you have one partition less. &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;That's it for tonight. I'll do one more post on partitioning and then I cover swithing in and switching out staging tables. &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 5pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8664623" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/menzos/archive/tags/SQL+Server+Concepts/default.aspx">SQL Server Concepts</category><category domain="http://blogs.msdn.com/menzos/archive/tags/Table+Partitioning/default.aspx">Table Partitioning</category></item><item><title>Synonyms and other Oracle stuff</title><link>http://blogs.msdn.com/menzos/archive/2008/06/26/synonyms-and-other-oracle-stuff.aspx</link><pubDate>Thu, 26 Jun 2008 21:50:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8657705</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/menzos/comments/8657705.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=8657705</wfw:commentRss><description>&lt;P&gt;Every now and then I meet Oracle specialists at customers sites. Sometimes they start a good discussion on the differences between the Oracle engine and the SQL Server engine. I don't mind a good discussion on the differences but sadly lots of Oracle guys compare 11G to SQL Server 6.5. (btw don't get me wrong : Oracle has a great engine, but we definitely closed the gap!) &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Two features they always refer to are schema's and synonyms. Schema's are very important in Oracle. Basically Oracle uses schema's like SQL Server uses databases. When you install an Oracle you have 1 database and you can create schema's in there to separate different data. Since SQL 2005 we also have schema's. Nothing will stop you for using them like you did in Oracle. But usually they are used to group related objects in 1 database. I must say I do not see many databases where they are used at all. Too bad, because it's a great way to group objects which share the same security. You only have to give permissions on schema's and not on the separate objects anymore. &lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Synonyms is another thing. In Oracle you create synonyms to use objects from 1 schema in another schema. In SQL Server you can also create synonyms. Normal practice is to create synonyms in the model database. That way, every new database created after that already has the synonym. After a restart it's even in the tempDB. In SQL Server synonyms make objects available across databases and schema's. Instead of using the 3 part name like &lt;SPAN style="COLOR: blue"&gt;&lt;STRONG&gt;AdventureWorks.Person.Address &lt;/STRONG&gt;&lt;/SPAN&gt;you can use the name of the synonym. For synonyms goes the same as for schema's: I almost never see them in customer databases. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8657705" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/menzos/archive/tags/SQL+Server+Concepts/default.aspx">SQL Server Concepts</category></item><item><title>Table Partitioning in SQL2008(2)</title><link>http://blogs.msdn.com/menzos/archive/2008/06/26/table-partitioning-in-sql2008-2.aspx</link><pubDate>Thu, 26 Jun 2008 17:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8656994</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/menzos/comments/8656994.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=8656994</wfw:commentRss><description>&lt;P style="MARGIN-LEFT: 1pt"&gt;Today I proceeded with partitioning a large table in SQL 2008. The objective is to build a partition function with a sliding window. Usually you use staging tables to get to this. This customer wants to keep all his data in one table so staging tables cannot be used. That's a pity because then we can't use all the new wizards in SQL 2008. &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 1pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 1pt"&gt;Since I do want to show you what this wizard looks like here's the screenshot: &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 1pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 1pt"&gt;&lt;IMG alt="" src="http://blogs.msdn.com/blogfiles/menzos/062608_1447_TablePartit1.png" mce_src="http://blogs.msdn.com/blogfiles/menzos/062608_1447_TablePartit1.png"&gt;&lt;SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman"&gt; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 1pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 1pt"&gt;You can switch in a staging table or switch out to a staging table. Pretty straightforward. It's not really automating things but merely making administration easier. &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 1pt"&gt;For automating you still have to create a stored procedure to do your merging and splitting. And a job to execute it regularly. &lt;/P&gt;
&lt;P style="MARGIN-LEFT: 1pt"&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8656994" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/menzos/archive/tags/SQL+2008/default.aspx">SQL 2008</category><category domain="http://blogs.msdn.com/menzos/archive/tags/SQL+Server+Concepts/default.aspx">SQL Server Concepts</category><category domain="http://blogs.msdn.com/menzos/archive/tags/Table+Partitioning/default.aspx">Table Partitioning</category></item><item><title>Table Partitioning in SQL 2008</title><link>http://blogs.msdn.com/menzos/archive/2008/06/25/table-partitioning-in-sql-2008.aspx</link><pubDate>Wed, 25 Jun 2008 23:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8653194</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/menzos/comments/8653194.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=8653194</wfw:commentRss><description>&lt;UL style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in; MARGIN-LEFT: 0.02in; DIRECTION: ltr; unicode-bidi: embed"&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;At the moment I'm working for a client that wants to partition some tables. They need to do that because the tables have grown to immense proportions. One table has 800.000.000 rows. Obviously a lot of inserts are happening on this tables and they hope to improve the performance of these inserts by partitioning the table. At the moment there production servers are running SQL 2005 but we are going to try it on SQL2008 as well. SQL 2008 has some cool gui functions for partitions with sliding windows which you have to program yourself otherwise. Let's have a look:&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;When you right-click a table you'll see the following menu. In the &lt;SPAN style="FONT-WEIGHT: bold; COLOR: blue"&gt;Storage &lt;/SPAN&gt;option you can create and manage partitions. Since we didn't define any partitions yet the &lt;SPAN style="FONT-WEIGHT: bold; COLOR: blue"&gt;Manage Partition &lt;/SPAN&gt;option is disabled.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0in"&gt;&lt;IMG src="http://blogs.msdn.com/photos/menzos/images/8653155/original.aspx" mce_src="http://blogs.msdn.com/photos/menzos/images/8653155/original.aspx"&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;We go for &lt;SPAN style="FONT-WEIGHT: bold; COLOR: blue"&gt;Create Partition&lt;/SPAN&gt;. First you'll get an introduction screen but after that you'll get this screen in which you can choose the partitioning&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;column and you can align the indexes. This is great because all this involved some serious scripting in SQL 2005.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0in"&gt;&lt;IMG style="WIDTH: 600px; HEIGHT: 501px" height=501 src="http://blogs.msdn.com/photos/menzos/images/8653156/original.aspx" width=600 mce_src="http://blogs.msdn.com/photos/menzos/images/8653156/original.aspx"&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Now you'll get some screens where you can choose or create new partition functions and schemes. When you opt for create you'll get this screen in which you can set the boundaries in a graphical manner.&lt;/P&gt;
&lt;P style="MARGIN: 0in"&gt;&lt;IMG style="WIDTH: 600px; HEIGHT: 503px" height=503 src="http://blogs.msdn.com/photos/menzos/images/8653158/original.aspx" width=600 mce_src="http://blogs.msdn.com/photos/menzos/images/8653158/original.aspx"&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Eventually you'll end up in this screen where you can execute the partition scripts or save them. The nice thing of the scripts is that they also run on SQL 2005. So if you need to do some partitioning on your 2005 server it pays of to install a SQL2008 server somewhere with your database and get some scripts :).&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0in"&gt;&lt;IMG style="WIDTH: 601px; HEIGHT: 501px" height=501 src="http://blogs.msdn.com/photos/menzos/images/8653145/original.aspx" width=601 mce_src="http://blogs.msdn.com/photos/menzos/images/8653145/original.aspx"&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/UL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8653194" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/menzos/archive/tags/SQL+2008/default.aspx">SQL 2008</category><category domain="http://blogs.msdn.com/menzos/archive/tags/SQL+Server+Concepts/default.aspx">SQL Server Concepts</category><category domain="http://blogs.msdn.com/menzos/archive/tags/Table+Partitioning/default.aspx">Table Partitioning</category></item><item><title>Introduction</title><link>http://blogs.msdn.com/menzos/archive/2008/06/24/introduction.aspx</link><pubDate>Tue, 24 Jun 2008 20:57:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8648259</guid><dc:creator>Menzo Steinhorst</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/menzos/comments/8648259.aspx</comments><wfw:commentRss>http://blogs.msdn.com/menzos/commentrss.aspx?PostID=8648259</wfw:commentRss><description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I'm Menzo Steinhost. I'm a SQL Server PFE based in the Netherlands.&lt;/P&gt;
&lt;P&gt;Before I came to Microsoft I worked for Deloitte Consulting. I did lots of different things there ranging from Oracle development to project audits. At some point I picked up SQL Server and I decided to specialize myself in it. I started way back in 1998 with SQL Server 6.5 and have experience with every version since. A few days ago I started a project on SQL 2008 and I'm very enthousiastic about it. I will tell you more about this in my next post.&lt;/P&gt;
&lt;P&gt;My main focus is SQL Server performance tuning. But I also do Risk Assesments and other things around SQL Server. I used to be a developer so I'm also interested in typical developer stuff.&lt;/P&gt;
&lt;P&gt;I will mainly blog about performance related issues on SQL Server. Performance tuning is a passion of mine and sometimes a rewarding job. You can make spectacular improvements, especially on poor indexed databases.&lt;/P&gt;
&lt;P&gt;As a PFE&amp;nbsp; I visit many premier accounts and on every site I see problems that you can&amp;nbsp;prevent&amp;nbsp;very&amp;nbsp;easy. Expect very practical information on this issues on this blog. There are also some neat tools available from Microsoft and third party vendors that might be useful in your day to day life. I will elaborate on these as well.&lt;/P&gt;
&lt;P&gt;Enjoy and I hope to hear from you!&lt;BR&gt;Menzo&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8648259" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/menzos/archive/tags/Introduction/default.aspx">Introduction</category></item></channel></rss>